Computer Fundamentals and Programming - Module 03
Computer Fundamentals and Programming - Module 03
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
> Checks if the value of left operand is greater than the (A > B) is not true.
value of right operand, if yes then condition becomes
true.
< Checks if the value of left operand is less than the (A < B) is true.
value of right operand, if yes then condition becomes
true.
>= Checks if the value of left operand is greater than or (A >= B) is not true.
equal to the value of right operand, if yes then
condition becomes true.
<= Checks if the value of left operand is less than or (A <= B) is true.
equal to the value of right operand, if yes then
condition becomes true.
&& Called Logical AND operator. If both the operands are (A && B) is false.
non-zero, then condition becomes true.
! Called Logical NOT Operator. Use to reverses the !(A && B) is true.
logical state of its operand. If a condition is true, then
Logical NOT operator will make false.
Conditional Statements
A conditional statement lets us choose which statement will be executed next
Therefore they are sometimes called selection statements
Conditional statements give us the power to make basic decisions
The C++ conditional statements are the:
1. if statement
2. if-else statement
3. Nested if-else statement
4. switch statement
1. if Statement - The if statement evaluates the test expression inside parenthesis. If test
expression is evaluated to true, statements inside the body of if is executed. If test
expression is evaluated to false, statements inside the body of if is skipped.
Syntax:
if (condition)
{
// statements
}
hand, in second sample output, the user input is 0. Since 0 is not greater than 0, it is equal
to 0, the condition inside the parenthesis becomes false, so the statement inside the body
of if will not be executed.
endl – The purpose of this code is to end or break the current line. Meaning if your program
has another string to be printed on the screen it will display on the next line right after the
first statement.
Output 1
Output 2
The program display the string Enter grade: and ask the user to input grade which will be
stored in variable named grade which has data type int, meaning user can input only whole
numbers. If the grade inputted by the user is greater than or equal to 75 the value of grade
will display together with the string is Passed. In Output 1 the user input is 80, the program
display the value of grade variable which is 80 followed by the string is Passed. In the
Output 2, the user inputted 70, this means that the condition inside the parenthesis of if
becomes false. Because the value of grade now is 70, 70 is not greater than or equal to 75.
Which means that the statement inside the body of if will not be executed because the
condition have not been met.
2. if – else Statement - The if else executes the codes inside the body of if statement if the
test expression is true and skips the codes inside the body of else. If the test expression is
false, it executes the codes inside the body of else statement and skips the codes inside the
body of if.
Syntax:
if (condition)
{
// statements
}
else
{
// statement
}
If the condition in if statement above will become false the flow of the program will jump to the
else statement and the syntax or code inside the body of else will be executed. For example in
output #2 the user entered -6, -6 doesn’t meet the condition in the if statement. Meaning the
condition inside the parenthesis of if becomes false, so the statement under the body of if will not
be executed and the one that will execute is the statement under the else statement.
Output 1
Output 2
Explanation:
The program display the string Enter grade: and ask the user to input grade which will be
stored in variable named grade which has data type int, meaning user can input only whole
numbers. If the grade inputted by the user is greater than or equal to 75 the value of grade will
display together with the string is Passed. If the user input is not greater than or equal to 75, the
program will skipped the statement inside the body of if and it will jump to the else statement. In
Output 1 the user input is 85, since the user input meet the condition of if statement the program
display the value of grade variable which is 85 followed by the string is Passed. In the Output 2,
the user inputted 72, this means that the condition inside the parenthesis of if becomes false.
Because the value of grade now is 72, 72 is not greater than 7or equal to 75. Which means that the
statement inside the body of if will not be executed and the program flow will jump to else and
execute the statement inside the body of else.
3. Nested if-else Statement - The if...else statement executes two different codes depending upon
whether the test expression is true or false. Sometimes, a choice has to be made from more than 2
possibilities. The nested if...else statement allows you to check for multiple test expressions and
execute different codes for more than two conditions.
Syntax:
if (condition 1)
{
// statements to be executed if condition 1 is true
}
else if (condition 2)
{
// statements to be executed if condition 1 is false and condition 2 is true
}
else if (condition 3)
{
// statements to be executed if condition 1 and condition 2 is false and condition 3 is true
}
else
{
// statements to be executed if all test expressions are false
}
Sample Code #1 using Nested if-else Statement
Output:
Output 1 Output 2
Output 3
Explanation:
int number – We declared a variable named number with the data type int.
cout<< “Enter an integer: “; - The program displayed the string inside the double quote
cin>>number – The program ask the user to input a number and it will store in number
variable.
if (number >0)
{
cout<<”You entered a positive integer: “ << number <<endl;
}
The program will compare the value of number variable to 0. If the value of number
variable is greater than 0 the statement inside the body of if will execute. Take a look our Output
1, the user input is 10, 10 will store in number variable, the value of number variable is now 10.
We will compare the value of number variable which is 10 in 0. Since 10 is greater than to 0 the
statement inside the body of if will be executed. The You entered a positive integer together
with value of number which is 10 will display on the screen. If the condition becomes false on the
if statement the program flow will jump to else if statement.
}
If the if condition was not met or become false, the program flow will jump on the else if
condition. For example in the Output 2, the user inputted -8, the value of number variable now
is -8. The program will compare the -8 if it is greater than to 0, which is based on the if condition.
Since the if condition becomes false, because -8 is not greater to 0, the program flow will now
jump to the else if condition. Under the else if condition, we will compare the value of number if
it is less than 0, since -6 is less than 0, the condition inside the parenthesis of else if becomes true,
so the statement under the else if will execute. Take note that you can add else if condition as
long as you needed.
else
{
cout<<”You entered a negative integer: “ << number <<endl;
If the condition in if statement above will become false the flow of the program will jump
to the else statement and the syntax or code inside the body of else will be executed. For example
in output #2 the user entered -6, -6 doesn’t meet the condition in the if statement. Meaning the
condition inside the parenthesis of if becomes false, so the statement under the body of if will not
be executed and the one that will execute is the statement under the else statement.
else
{
If the condition in if and else if statement above will become false the flow of the program will jump to the
else statement and the syntax or code inside the body of else will be executed. For example in output #3
the user entered 0, -0 doesn’t meet the condition both in the if and else-if statement. The if condition
compare the value of number variable if it greater than 0, since 0 is not greater than 0, the body if will
not execute. On the other hand the else if condition compare the number variable if it is less than to 0,
since 0, is not less than to 0, the else if statement becomes false also and will not be executed. Since the if
and else if condition have not or becomes false the else statement now will be executed. Meaning if all the
conditions have not been met, the statement under else will be executed.
Output 1
Output 2
Output 3
Explanation
The program display the string Enter grade: and ask the user to input grade which will be
stored in variable named grade which has data type int, meaning user can input only whole
numbers. If the grade inputted by the user is greater than or equal to 75 the value of grade will
display together with the string is Passed. The else if statement used logical operators (&&),
which means if the value of grade variable is less than 75 and greater than 70 the text is
Incomplete with the grade inputted will display. In Output 2, the user input is 73, 73 is not greater
than 75, so the if condition didn’t meet, the program jump to the else if condition, 73 is less than
75 and greater than 70, meaning the condition inside the parenthesis of else if becomes true, so
the statement inside the body of else if will be executed. And if the user input doesn’t meet all the
conditions above the program flow will jump to the else statement. In Output 3, the user inputted
65. 65 is not greater than 75, so the if condition will becomes false, also 65 is not less than 75
and not greater than 70 so the else if condition becomes false also. The else statement now will
execute.
break – always used with the switch statement to terminate the switch to prevent entry in the
other case.
default – can be used for performing a task when none of the cases is true.
Syntax:
switch (expression)
{
case constant1:
statement
break;
case constant2:
statement
break;
default:
statement
}
Sample Code #1 using Switch Statement Output
Output 1
Output 2
Output 3
case 1:
cout << "Number in word is one" << endl;
break;
- We compare the value of num variable if it is equal to 1. If it is equal to 1, the statement inside
the body of case 1 will execute. For example the user inputted 1, then the program will display
the statement the body of case 1 which is Number in word in 1.
case 2:
cout << "Number in word is two" << endl;
break;
- It is the same things with case 1, if the user input is 2, the program will skip the case 1 and the
statement inside the body of case 2 will execute. Same with case 3, 4, and 5.
default:
cout << "Wrong input";
break
- If all the condition or cases above becomes false the statement under default will execute. It is
the same thing with the else statement of if else. For example the user input 7, since our cases
are up to 5 constant only the default statement which is Wrong input will display.
Output 1
Output 2
Output 3
Output 4
Explanation: Output 3
The program ask the user to input a grade which will store in the variable named grade with the data type
char, meaning the user input must be a letter/character. If the user inputted A the statement under case ‘A’
will display which is the word Excellent. If the user inputted B and C the statement Well done will display.
The reason why case ‘B’ doesn’t have a statement below because case ‘B’ and case ‘C’ have the same
statement. If the user input is D the statement You Passed will display. If the user input is F the Better try
again will display. And if the user inputted letters that are not included the choices or in the cases the default
statement will execute which is the Invalid grade. After displaying the statement inside the case which
becomes true, the statement Your grade is together with the value of grade variable will display on the
screen. Take note that if used a character in cases it must be inside a single quote (‘ ’). If integer, no need
to used single quote.