University of Juba
Programming with C++
Practical Training
Lecture 9: Control Statements (Conditional Statements)
1. Introduction
• Conditional statements, in simple terms, are statements
that may or may not be executed based on a condition
we specify.
• They are called selection statements because they
determine which statements to execute based on a
condition.
1. Introduction
• Conditional statements are divided into two main categories:
Branching Statements: These statements execute one or more statements while
ignoring others based on a specific condition. They include:
• If Statement
• Switch Statement
Iterative Statements: These statements repeat the execution of one or more
statements for a specific number of times or until a certain condition is
met. They are divided into:
Fixed Loops:
• For Loop
Variable Loops:
• While Loop
• Do...While Loop
1. The If Statement
• The If statement is used to execute one or more statements
based on a specific condition.
• It has three forms:
• Simple If Statement
• If/Else Statement
• If/Else If Statement
Simple If Statement
• This is the simplest form of the If statement.
• It tests a condition, and if the condition is true, it executes the
statement(s) following it.
• If the condition is false, it skips the statement(s) and continues
with the rest of the program.
Simple If Statement:
The general syntax is as follows:
In this syntax, we notice the use of curly braces { } to indicate the beginning
and end of the block of statements that belong to the If statement. If the
condition is true, the statements inside the block are executed. If the condition
is false, the program skips the block and continues.
Example
In this program, the user is prompted to enter their age. If the age
entered is less than 60, the program prints "You are pretty young!!".
If the age is 60 or older, nothing is printed.
If/Else Statement
• This form of the If statement is slightly different from the
simple If statement.
• If the condition is not met, an alternative action is taken. In
other words, if the condition is false, the program executes the
statements in the Else block.
If/Else Statement
The general syntax is as follows:
Example
In this example, if the age entered is less than 60, the program prints "You are
pretty young!". If the age is 60 or older, the program prints "You are old".
If/Else If Statement
• This form allows for multiple conditions to be checked sequentially.
• If the first condition is not met, the program checks the next condition,
and so on.
• If none of the conditions are met, the Else block is executed.
If/Else If Statement
The general syntax is as follows:
Example
In this program, the user is prompted to enter a mark. Based on the mark entered, the program
prints the corresponding grade. If the mark is greater than 90, it prints "EXCELLENT". If the
mark is between 81 and 90, it prints "VERY GOOD", and so on. If the mark is less than or
equal to 60, it prints "FAIL" and "TRY ANOTHER TIME".
2. The Switch Statement
The Switch statement is another conditional statement
similar to the If statement, but it differs in that it tests the
value of a single variable rather than a condition. In other
words, the Switch statement executes a specific block of
code based on the value of a variable.
2. The Switch Statement
The general syntax is as follows:
Example
Example
In this program, the user is prompted to enter a number
representing a day of the week. The Switch statement checks the
value of the variable day and prints the corresponding day. If the
number entered is not between 1 and 7, the program prints "Wrong
day number".
Note: It is important to include the break statement after each case
to prevent the program from executing the next case. If
the break statement is omitted, the program will continue executing
the next case, which may lead to unexpected results.
Conditions for Using the Switch Statement
1. The variable in the Switch statement must be of integer or
character type.
2. The variable in the Switch statement cannot be a mathematical
or logical expression.
3. The variable in the Switch statement and the case values must
be of the same type.
4. The case values must be constant and not variables.
Example:
Write a program that reads a mathematical operator and two numbers, then performs
the corresponding operation based on the operator entered.