0% found this document useful (0 votes)
16 views14 pages

CP2 Module 7 - Conditional Statements

This document presents an overview of conditional statements in computer programming. It defines conditional statements as using if, else if, and else to determine which code to execute based on whether a condition is true or false. Comparison operators that return true or false are introduced. The syntax for if, else if, and else statements is demonstrated. Coding exercises show how to print different outputs depending on the results of conditions comparing values of x and y. Examples are given of programs that could use conditional statements to check temperature, validate a password, or determine if a student passed based on their average grade. References for further learning about C++ programming are also provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

CP2 Module 7 - Conditional Statements

This document presents an overview of conditional statements in computer programming. It defines conditional statements as using if, else if, and else to determine which code to execute based on whether a condition is true or false. Comparison operators that return true or false are introduced. The syntax for if, else if, and else statements is demonstrated. Coding exercises show how to print different outputs depending on the results of conditions comparing values of x and y. Examples are given of programs that could use conditional statements to check temperature, validate a password, or determine if a student passed based on their average grade. References for further learning about C++ programming are also provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

COMPUTER PROGRAMMING 2

Conditional Statements
PRESENTED BY:
Jhaun Paul G. Enriquez
SHS ICT Faculty
1
LEARNING OUTCOMES:

1. Explain conditional statement as control structure in


computer programming
2. Identify .
3. Create a C++ program using different operators.

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

• The if-else 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

You might also like