0% found this document useful (0 votes)
261 views19 pages

C++ Decision Making Statements: Lecture Three

Download as pptx, pdf, or txt
0% found this document useful (0 votes)
261 views19 pages

C++ Decision Making Statements: Lecture Three

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

C++ decision making

statements

Lecture three
decision making statements
Sometimes we need to execute a block of
statements only when a particular condition is
met or not met. This is called decision making,
as we are executing a certain code after making
a decision in the program logic. For decision
making in C++,
C++ decision making statements
Decision making structures require that the
programmer specify one or more conditions to be
evaluated or tested by the program, along with a
statement or statements to be executed if the
condition is determined to be true, and optionally,
other statements to be executed if the condition is
determined to be false.
Following is the general form of a typical decision making
structure found in most of the programming languages −
C++ programming language provides
following types of decision making
statements.

 if statement
if...else statement
nested if statements
switch statement
If statement in C++

If statement consists a condition, followed by


statement or a set of statements as shown below:

if(condition){
 Statement(s);
}
If statement in C++(Cont’..)

The statements inside if parenthesis (usually


referred as if body) gets executed only when
the given condition is true. If the condition is
false then the statements inside if body are
completely ignored.
 if statement

An if statement consists of a boolean expression


followed by one or more statements.
Syntax
The syntax of an if statement in C++ is −
if(boolean_expression)
{
// statement(s) will execute if the boolean expression
is true
}
Flow Diagram
If else statement in C++
Sometimes you have a condition and you want to execute a block of
code if condition is true and execute another piece of code if the
same condition is false. This can be achieved in C++ using if-else
statement.

This is how an if-else statement looks:

if(condition)
{
Statement(s);
}
else
{
Statement(s);
}
If else statement in C++

The statements inside “if” would execute


if the condition is true, and the
statements inside “else” would execute if
the condition is false.
C++ if...else statement
An if statement can be followed by an optional else statement,
which executes when the boolean expression is false.
if(boolean_expression) {
 // statement(s) will execute if the boolean expression is true
} else {
 // statement(s) will execute if the boolean expression is false
}
If the boolean expression evaluates to true, then the if
block of code will be executed, otherwise else block of code
will be executed.
Flow Diagram
if-else-if Statement in C++
if-else-if statement is used when we need to check
multiple conditions. In this control structure we have
only one “if” and one “else”, however we can have
multiple “else if” blocks. This is how it looks:
Syntax
The syntax of an if...else if...else statement in C++ is −

if(boolean_expression 1) {
// Executes when the boolean expression 1 is true
} else if( boolean_expression 2) {
// Executes when the boolean expression 2 is true
} else if( boolean_expression 3) {
// Executes when the boolean expression 3 is true
} else {
// executes when the none of the above condition is
true.
}
Nested if statement in C++
When there is an if statement inside another if
statement then it is called the nested if statement.
The structure of nested if looks like this:

if(condition_1) {
Statement1(s);

if(condition_2) {
Statement2(s);
}
}
Switch Case statement in C++
with example
Switch case statement is used when we have multiple
conditions and we need to perform different action based
on the condition. When we have multiple conditions and
we need to execute a block of statements when a
particular condition is satisfied. In such case either we can
use lengthy if..else-if statement or switch case. The
problem with lengthy if..else-if is that it becomes complex
when we have several conditions.
The switch case is a clean and efficient method of
handling such scenarios.
The syntax of Switch
case statement:
The syntax for a switch statement in C++ is as follows −

switch(expression) {
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional

// you can have any number of case statements.


default : //Optional
statement(s);
}
Cont….
switch (variable or an integer expression)
{
case constant:
//C++ code
;
case constant:
//C++ code
;
default:
//C++ code
;
}

You might also like