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

Practical Programming -L09

This document provides an overview of control statements in C++, focusing on conditional statements such as If, If/Else, If/Else If, and Switch statements. It explains their syntax, usage, and provides examples to illustrate how these statements determine the flow of execution based on specified conditions. Additionally, it outlines the conditions for using the Switch statement and includes practical programming examples.

Uploaded by

Jaafar Abbakar
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)
3 views

Practical Programming -L09

This document provides an overview of control statements in C++, focusing on conditional statements such as If, If/Else, If/Else If, and Switch statements. It explains their syntax, usage, and provides examples to illustrate how these statements determine the flow of execution based on specified conditions. Additionally, it outlines the conditions for using the Switch statement and includes practical programming examples.

Uploaded by

Jaafar Abbakar
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/ 21

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy