0% found this document useful (0 votes)
219 views

Computer Fundamentals and Programming - Module 03

This document provides information about a course on computer fundamentals and programming. It includes: - The course title, code, credit units, instructor details, and intended learning outcomes which involve understanding computer applications and components, and learning basic C++ coding. - A weekly course schedule that covers topics like computer components, introduction to programming in C++, operators, conditional statements, and arrays over 5 modules. - Descriptions of arithmetic, relational, and logical operators in C++ and examples of conditional statements like if, if-else that allow choosing which code to execute.

Uploaded by

Joel Manacmul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
219 views

Computer Fundamentals and Programming - Module 03

This document provides information about a course on computer fundamentals and programming. It includes: - The course title, code, credit units, instructor details, and intended learning outcomes which involve understanding computer applications and components, and learning basic C++ coding. - A weekly course schedule that covers topics like computer components, introduction to programming in C++, operators, conditional statements, and arrays over 5 modules. - Descriptions of arithmetic, relational, and logical operators in C++ and examples of conditional statements like if, if-else that allow choosing which code to execute.

Uploaded by

Joel Manacmul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Module 03

Computer Fundamentals
and Programming

ENGR. JOEL D. MANACMUL


Department of Engineering and Architecture
Bataan Heroes College

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

Computer Fundamentals and Programming Page 2 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

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. .

Intended Learning Outcomes


 Identify various computer applications
 Familiarize with the operations or process in computer from raw facts to useful output
 Explain the characteristics of input/output and storage components of computer
 Know the difference and relevance of software and hardware

Computer Fundamentals and Programming Page 3 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

Course Schedule

Week Topic

 Basic Components of Computer


o Definition of computer
Week 1 o Computer Application
Module 1 o Operations in computer
o Components of computer
 Hardware and Software
 Introduction to Programming
Week 2 o What is C++
Module 2 o Installation
o Coding
MIDTERM ASSESSMENT
Week 3  Operators in C++
Module 3  Conditional Statements
Week 4
 Loop Statements
Module 4
Week 5
 Array
Module 5
FINAL ASSESSMENT

Computer Fundamentals and Programming


Objectives:
Working on this module should help you to:
 Code basic C++ language.
 Demonstrate familiarity with major events in computer history and apply their lessons to
current and future technological and social developments.
 Apply problem-solving and critical-thinking skills in collaborative contexts.
 Utilize technology in pursuit of intellectual growth and efficacious human interaction.
 Explore and research applications of computers and computing in their daily lives.

Computer Fundamentals and Programming Page 4 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

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

Module 3: Operators and Conditional Statement


Operators in C++
Operator - A symbol that tells the compiler to perform specific mathematical of logical
manipulations.
Operators are used to perform operations on variables and values.

A. Arithmetic Operators - are used to perform common mathematical operations.


There are following arithmetic operators supported by C++ language –
Assume variable A holds 10 and variable B holds 20, then –

Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10

* Multiplies both operands A * B will give 200

/ Divides numerator by de-numerator B / A will give 2

% Modulus Operator and remainder of after an integer B % A will give 0


division

++ Increment operator, increases integer value by one A++ will give 11

-- Decrement operator, decreases integer value by one A-- will give 9

Computer Fundamentals and Programming Page 5 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

B. Relational Operators - used to compare two values


Here are following relational operators supported by C++ language
Assume variable A holds 10 and variable B holds 20, then −

Operator Description Example

== Checks if the values of two operands are equal or (A == B) is not true.


not, if yes then condition becomes true.

!= Checks if the values of two operands are equal or (A != B) is true.


not, if values are not equal then condition becomes
true.

> 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.

C. Logical Operators - used to determine the logic between variables or values


There are following logical operators supported by C++ language.
Assume variable A holds 1 and variable B holds 0, then –

Computer Fundamentals and Programming Page 6 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

Operator Description Example

&& Called Logical AND operator. If both the operands are (A && B) is false.
non-zero, then condition becomes true.

|| Called Logical OR Operator. If any of the two operands (A || B) is true.


is 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
}

Computer Fundamentals and Programming Page 7 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

Sample Code #1 using if statement

Sample Outputs – Output 1 and Output 2

Let’s explain the code.


int number; - We initialized variable named number and its data type is int.
cout << "Enter an integer: "; - The program display the word Enter an integer:
cin >> number; - The program ask the user to input an integer and it will store in variable
named number.
if ( number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}
We compare the value of number variable in 0. If the value of number variable is greater
than 0, then the condition inside the parenthesis becomes true, so the statement inside the
body of if statement will execute (the statement inside the curly braces under the if
statement. Take a look our first output, the user inputted 5, 5 stored in number variable,
so the value of number is 5. Since 5 is greater than 0 the condition inside the parenthesis
became true, so the statement under the body of if which is “You entered a positive
integer” together with the value of number which is 5 displayed on the screen. On the other

Computer Fundamentals and Programming Page 8 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

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.

Sample Code # 2 using if 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.

Computer Fundamentals and Programming Page 9 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

Syntax:

if (condition)
{
// statements
}
else
{

// statement
}

Sample Code #1 using if-else Statement

Explanation of the code # 1


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;
}

Computer Fundamentals and Programming Page 10 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

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.

Sample Code #2 using if-else Statement Output:

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.

Computer Fundamentals and Programming Page 11 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

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

Computer Fundamentals and Programming Page 12 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

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.

else if (number >0)


{
cout<<”You entered a negative integer: “ << number <<endl;

}
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

Computer Fundamentals and Programming Page 13 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

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
{

cout<<”You entered a negative integer: “ << number <<endl;


}

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.

Computer Fundamentals and Programming Page 14 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

Sample Code #2 using Nested if-else Statement Output

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.

1. Switch Statement – used instead of nested if else statement.


 It is a multiple branch decision statement of C++.
 Test a variable with list of values of equivalence each value called case.

Computer Fundamentals and Programming Page 15 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

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

Computer Fundamentals and Programming Page 16 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

Explanation of the code


This program is a conversion of number from 1 to 5 into words.
int num; - We initialized variable named number and its data type is int.
cout << "Enter an integer: "; - The program display the word Enter an integer:
cin >> num; - The program ask the user to input an integer and it will store in variable
named number.
switch (num)
{
- This is the start of switch statement. We will compare the constant of the cases in the value of
num variable .

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.

Computer Fundamentals and Programming Page 17 of 18


Engr. Joel D. Manacmul
Bataan Heroes College

Sample Code #2 using Switch Statement Output

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.

Computer Fundamentals and Programming Page 18 of 18


Engr. Joel D. Manacmul

You might also like