0% found this document useful (0 votes)
2 views23 pages

chapter 3 flow control

The document discusses flow control in programming, specifically focusing on decision-making and looping in C++. It explains the use of if statements, including their syntax, examples, and the concept of nested if-else statements, as well as the dangling else problem. Additionally, it introduces the conditional operator as a shorthand for if-else statements.

Uploaded by

kalabshewadege
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
2 views23 pages

chapter 3 flow control

The document discusses flow control in programming, specifically focusing on decision-making and looping in C++. It explains the use of if statements, including their syntax, examples, and the concept of nested if-else statements, as well as the dangling else problem. Additionally, it introduces the conditional operator as a shorthand for if-else statements.

Uploaded by

kalabshewadege
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 23

Program Flow Control

CHAPTER 3
Lecture 1
Flow of Control
 As we already have seen in the discussion on
algorithms, programs, like humans, decide what to
do in response to changing circumstances.

 Do one thing if some condition is met or another if


not (Decisions)

 Do a certain action repeatedly based on a condition


(Looping)

 C++ defines a set of flow-of-control statements


that allow statements to be executed either
conditionally or repeatedly
Decisions and Branching

 A program may need to make a onetime


decision which alters the sequential flow of
execution and causes a jump to another part of
the program (branching).
 C++ provides us with the if statement and the
switch statement for these purposes.

11/13/09
If statements
An if statement has two forms: one with an else
branch and one without
If(expression)

statement

Or
 If(expression)

statement1

else

statement2
If statement
A test expression that
The if statement evaluates to a Boolean
value (either true or false)

Syntax:
A single statement or a
if (condition) compound statement, to
be executed if
statement condition evaluates to
true.
If the condition is true, the statement is executed,
otherwise it is skipped.
Example 1
#include<iostream>
Using namespace std;
Int main()
{
unsigned short age;
cout<<“Enter your age: “;
cin>>age;
if (age < 17)
cout<<“You are too young!\n”;
cout<<“Thank you.”<<endl;
return 0;
}
Note: the if statement in this example, executed only a single
statement when the expression is true

11/13/09
Example 2
In order to execute multiple statements, we can use a block
e.g.
int val;
cout<<“Enter an integer value: “;
cin>>val;
if (val%2 == 0)
{
int quotient = val/2;
cout<<val<<“ is divisible by 2.”<<endl;
cout<<“The quotient is ”<<quotient<<endl;
cout<<“Thank you.”<<endl;
}
return 0;
}
The else clause
Syntax: If condition evaluates
to true, this statement
if (condition) will be executed

statement1

else
If condition evaluates
statement2 to false, this statement
will be executed

statement1 and statement2 can be compound


statements.
Example 1

Note: the if and else statements executed a single statement if the condition is true or false
Example 2

In order to execute multiple statements we can use a block


Flowchart equivalents
Nested if …else statements
If(expression)

statement 1

else

statement 2

Note that: statement 1 and statement 2 can be any


statement including if… else statement!
Cont…
e.g. when statement 2 itself is again another if…else statement
Example 2
#include<iostream>
using namespace std;
int main(){
unsigned short age;
cout<<"Enter your age: ";
cin>>age;
if (age < 17)
cout<<"You are too young!\n";
else if (age < 40)
cout<<"You are still young!\n";
else if (age < 70)
cout<<"Young are not young!\n";
else
cout<<"Are you sure?\n";
cout<<"Thank you."<<endl;
return 0;
}
If …else
structure….conclusion
Convenient form for nested if…else statements
if (condition1)
statement1
else if (condition2)
statement2
...

else if (conditionN-1)
statementN-1
else
statementN
Nested if …else statements
E.G. When a statement (in the definition of If…Else)
is it self another If……Else statement

The above program introduces a source of potential


ambiguity called a dangling else problem
Dangling else problem
Is the else statement in the above program matched
up with the outer or inner if statement?
The answer is that an else statement is paired up with
the last unmatched if statement in the same block
Dangling else problem
(the above program can be written without ambiguity like this)
Dangling else problem
However, if we want the else to attach to the outer if statement,
we can do this explicitly by enclosing the inner if statement in a
block

11/13/09
Dangling else problem…Summary
The use of a block tells the compiler that the else
statement should attach to the if statement before the
block
Without the block, the else statement would attach to
the nearest unmatched if statement, which would be
the inner if statement
If statements can be used for error checking
The Conditional Operator
A short-hand method of expressing if…else
statements
syntax:
condition ? expression1 : expression2;

An Expression to Expression to
expressio execute if execute if
n that condition condition
evaluates evaluates to evaluates to
to a true. false.
boolean.
Example 1
(x < y) ? x = 10 : y = 10;

//this is the same as

if (x < y)

x = 10;

else

y = 10;

You might also like