Computer Fundamentals and Programming - Module 04
Computer Fundamentals and Programming - Module 04
Computer Fundamentals
and Programming
This module or any portion thereof may not be reproduced or used in any manner whatsoever without
the express written permission of the publisher except for educational purposes but with a citation to this
source.
For Permission: Contact Bataan Heroes College, Roman Super Hi-way, Balanga City, Bataan, Philippines
Course Information
Course Title : Computer Fundamentals and Programming
Program : Business & Technology
Course Code : Comp 101
Credit Units : 3 units
Pre-requisite/s :
Instructor Information
Name : Engr. Joel D. Manacmul
Contact Information
Contact Number : 09281421172
Facebook : Joel Manacmul
Email : hoel07@gmail.com
Course Description
In the course, students would understand the basic components of computers (hardware and
software), information systems, and electronic data processing. The course emphasizes on program
development, which includes use of pseudocode in problem-solving and formulation of
algorithms. High level language and programming applications like C++; computer solutions of
engineering problems. .
Course Schedule
Week Topic
Reference
Learn Computer Fundamentals basic of Computers. Tutorial Points. Retrieved July 29, 2020 from
https://www.tutorialspoint.com/computer_fundamentals/index.htm
Parson , J. Oja, D. (2011). New Pespective on Computer Cocpets 2012: Comprehensive (14th ed).
Massachusetts, USA: Course Technology
Example: Output
Explanation:
int a; - We declared a variable named a with the data type int, meaning it can handle only whole
numbers or integer. Note that if we declared a variable and we didn’t assigned a value its default
initial value is 0. But for example we declared like this int a=5; we declared a as integer with 5 as
its initial value.
while (a<=20)
{
Cout<<”Value of a: “ <<a<<endl;
a++;
}
We will display the string “Value of a” together with the value of a while its value is less than or
equal to 20. Meaning instead we print the text using cout in multiple times, we used loop
specifically while loop to print the string multiple times while the condition is true.
a++ - is the code that will increment the value of a by 1 as the statement loops or repeats.
B. Do – While Loop – It is like a while statement, except that it test the condition at the end of the
loop body.
Syntax:
do
{
Statement
}
while (condition)
Example: Output
int a; - We declared a variable named a with the data type int, meaning it can handle only whole
numbers or integer.
do
{
Cout<<”Value of a: “ <<a<<endl;
a++;
}
while(a>=20)
It’s the same thing with while condition, it has the same purpose which is to run a specific
statement or group of statements multiple times depending on the condition given. It’s just that in
do while we will execute first the statement (statement under do) before we check the condition
(condition inside wile).
We will also display the string “Value of a” together with the value of a while its value is less
than or equal to 20.
a++ - is the code that will increment the value of a by 1 as the statement loops or repeats.
C. For Loop – execute a sequence of statements multiple times and abbreviate the code that manages
the loop variable
Parts of For Loop
1. Int – execute first and inly once
- Allows you to declare and initialize any loop control variable
2. Condition – if it is true, the body of the loop will be executed
- If it is false, the body of the loop does not execute and flow of control jumps to the next
statement after the loop
3. Increment – After the body of the loop executes, the flow of control jumps back up to the
increment statement.
Syntax:
for (int; condition; increment)
{
Statement
}
Example: Output:
Explanation:
int a=1; - this is our init r initialization. We decalred a vriable with 1 as its initial value
a<=20; this is the condition of our loop. Meaning the statement inside the body of loop will
execute while this condition is true.
a++ - the value of variable will increment by 1 as the statement loops or repeats.
for (int a=1; a<=20; a++)
{
cout<<"Value of a: "<<a<<endl;
}
Meaning, we will print the string “Value of a” with the value of a, and the initial value of a is 1,
so when the string execute for the first time the first value of a is 1, when the string will execute
again the value of a now is 2, it is because we have incrimination in the for loop. The value of a
will increment by 1 as the statement loop while a is less than or equal to 20.
D. Goto Loop – When encountered, program flow jumps to the location specified by goto
- The goto requires a label
- Label is a valid C++ identifier followed by colon
Syntax:
loop_name:
statement;
Example: Output:
Explanation:
Int a= 1 – We declared a variable named a with initial value of 1.
loop:
cout<<"Value of a: "<< a <<endl;
a++;
Our loop/label name is loop, again you are the one who will name the label. Under our
loop we will display the string Value of a with the value of a. And as the loop repeats its
execution the value of a will increment by 1.
if(a<=20)
{
goto loop;
}
We used if statement to compare the value of a if it is less than or equal to 20. Since our loop
will increment by 1 as it repeats its execution, we compare the value of a to 20. If the value of a is
less than or equal to 20, the program flow will jump to the loop. If the value of a will become
greater than 20 the program will be terminated.
E. Nested Loop - You can use one or more loop inside any another loop.
Example: Output:
Explanation:
When the first for loop is executed, the value of i is 1 and "Table of 12" gets
printed. Now coming to the second loop, the value of j is 1 and thus 1*1 = 1 gets printed.
In the second iteration of the inner for loop, while the value of i is still 1, the value
of j becomes 2 and thus 1 * 2 = 2 gets printed. In the last iteration of the inner for loop,
the value of i is still 1 and the value of j becomes 10, thus printing 1 * 10 = 10.
Now after all the iterations of the inner for loop are complete, there will be the second
iteration of the outer for loop increasing the value of i to 2 and printing Table of 2. Again
the inner for loop will be iterated with i equals 2. And so on, and so on.
We can use any loop inside any other loop according to the requirement. In the above
example, we used one for loop inside another.
Infinite Loop - There may exist some loops which can iterate or occur infinitely. These are called Infinite
Loop. These loops occur infinitely because their condition is always true.