CP2 Module 7 - Conditional Statements
CP2 Module 7 - Conditional Statements
Conditional Statements
PRESENTED BY:
Jhaun Paul G. Enriquez
SHS ICT Faculty
1
LEARNING OUTCOMES:
COMPUTER PRORAMMING 2 2
What is a Conditional Statement?
• Statements used to determine what will happen next after
a condition is met or otherwise
• Conditional Statements:
• Use if to specify a block of code to be executed, if
a specified condition is true
• Use else to specify a block of code to be executed,
if the same condition is false
• Use else if to specify a new condition to test, if the
first condition is false
COMPUTER PROGRAMMING 2 3
Comparison Operators:
- The return value of a comparison is either true (1) or false (0)
COMPUTER PROGRAMMING 2 4
Source: https://www.w3schools.com/cpp/cpp_operators_assignment.asp
Conditional Statement Syntax:
• The if statement
COMPUTER PROGRAMMING 2 5
Conditional Statement Syntax:
• The else-if statement
COMPUTER PROGRAMMING 2 6
Coding Exercise:
- Print "Hello World" if x is greater than y.
COMPUTER PROGRAMMING 2 7
Source: https://www.w3schools.com/cpp/exercise.asp?filename=exercise_conditions1
Coding Exercise:
- Print "Hello World" if x is equal to y.
COMPUTER PROGRAMMING 2 8
Source: https://www.w3schools.com/cpp/exercise.asp?filename=exercise_conditions2
Coding Exercise:
- Print "Yes" if x is equal to y, otherwise print "No".
COMPUTER PROGRAMMING 2 9
Source: https://www.w3schools.com/cpp/exercise.asp?filename=exercise_conditions3
Coding Exercise:
- Print "1" if x is equal to y,
print "2" if x is greater than y,
otherwise print "3".
COMPUTER PROGRAMMING 2 10
Source: https://www.w3schools.com/cpp/exercise.asp?filename=exercise_conditions4
Conditional Statement Examples
1. Create a program that tells the user that it is hot if the
temperature is greater than 60oC.
2. Create a program that validates a password entered by
the user. It should display a string if the password is
correct or not.
3. Create a program that calculates the average grade and
displays whether the student has passed or failed. The
student has passed the subject if the average grade is
equal or greater than 75%.
COMPUTER PROGRAMMING 2 11
References:
• Pomperada, Jake. 2019. Beginner’s Guide to C++ Programming.
Manila: Mindshapers Co., Inc.
• Pepito, Copernicus. 2009. Introduction to C++ 2008 programming.
Manila: National Bookstore.
• https://www.codecademy.com/courses/learn-c-plus-plus
• https://www.w3schools.com/cpp/default.asp
• https://www.tutorialspoint.com/cplusplus/index.htm
COMPUTER PROGRAMMING 2 12
COMPUTER PROGRAMMING 2 13
COMPUTER PRORAMMING 2