Chapter 3 Controls & Decisions3
Chapter 3 Controls & Decisions3
Statement n return 0;
Tuesday, May 20, 2025 2
Flow of Control
• A program is usually not limited to a linear
sequence of instructions. During its process it
may repeat code (Looping) or take
decisions/select (selection . For that purpose,
C++ provides control structures that serve to
specify what and how have to perform our
program.
If(expression or condition)
{ A single statement or a
//code/statement(s) compound statement, to be
} executed if condition
evaluates to true.
Syntax:
If condition evaluates
If(expression or condition) to true, this statement
{ will be executed
//code1/statement1
}
else
{ If condition evaluates
to false, this statement
//code2/statement2 will be executed
}
Tuesday, May 20, 2025 8
Example 1
Note: the if and else statements executed a single statement if the condition is
true or false
else if (conditionN-1)
statementN-1
else
statementN
Tuesday, May 20, 2025 16
Example :
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 at heart!\n”;
else
cout<<“Are you sure?\n”;
cout<<“Thank you.”<<endl;
Tuesday, May 20, 2025 17
//This program illustrates a bug that occurs when independent if/else statements are used to
assign a letter grade to a numeric test score.
#include <iostream>
using namespace std;
int main()
{
int testScore; char grade;
cout << "Enter your numeric test score and I will\n "tell you the letter grade you earned:
";
cin >> testScore;
if (testScore < 60)
Program Output with Example Input Shown in
grade = 'F'; Bold
if (testScore < 70) Enter your numeric test score and I will tell you
grade = 'D'; the letter grade you earned: 40[Enter]
if (testScore < 80) Your grade is A.
grade = 'C';
if (testScore < 90)
grade = 'B';
if (testScore <= 100)
grade = 'A';
cout << "Your grade is " << grade << ".\n";
Tuesday, May 20, 2025
return 0; 18
}
//It can be corrected like this
#include <iostream>
using namespace std;
int main()
{
int testScore; char grade;
cout << "Enter your numeric test score and I will\n "tell you the letter grade you earned:
";
cin >> testScore;
if (testScore < 60)
grade = 'F'; Program Output with Example Input Shown in
Bold
else if (testScore < 70)
Enter your numeric test score and I will tell you
grade = 'D';
the letter grade you earned: 40[Enter]
else if (testScore < 80) Your grade is F.
grade = 'C';
else if (testScore < 90)
grade = 'B';
else if (testScore <= 100)
grade = 'A';
cout << "Your grade is " << grade << ".\n";
return 0; 20, 2025
Tuesday, May 19
Dangling else/ matching else problem
Without the break statements, the program would execute all of the lines from the matching
case statement to the end of the block or break statement. The break statement causes the
program to exit the switch statement
// This program demonstrates how a switch statement works if there are no break statements.
#include <iostream>
using namespace std;
int main()
{
char choice;
cout << "Enter A, B, or C: ";
cin >> choice;
switch (choice) // The ff switch statement is missing its break statements!
{
case 'A': cout << "You entered A.\n";
case 'B': cout << "You entered B.\n";
case 'C': cout << "You entered C.\n";
default : cout << "You did not enter A, B, or C!\n";
}
return 0;
31
} Tuesday, May 20, 2025
Break Statement
• When a case is matched (or the default is executed), execution begins at the first statement
following that label and continues until one of the following conditions is true:
1) The end of the switch block is reached
2) A break statement occurs
Note that if none of these conditions are met, cases will overflow into other cases! The program
“falls through” all of the statements below the one with matching case expression
E.g.
switch (2)
{
case 1: // Does not match -- skipped
cout << 1 << endl;
case 2: // Match! Execution begins at the next statement
cout << 2 << endl; // Execution begins here 2
case 3: 3
cout << 3 << endl; // This is also executed 4
case 4: 5
cout << 4 << endl; // This is also executed
// Break;
default:
cout << 5 << endl; // This is also executed
}
Tuesday, May 20, 2025 32
Break Statement
• Sometimes, you may want the program to “fall-through” statements.
• It is possible to have multiple case labels refer to the same statements.
• E.g.
char feedGrade;
cout << "Our dog food is available in three grades:\n A, B, and C. Which do you want pricing for? ";
cin >> feedGrade;
switch(feedGrade)
{
case 'a':
case 'A':
cout << "30 cents per pound.\n";
break;
case 'b':
case 'B': cout << "20 cents per pound.\n";
break;
case 'c':
case 'C': cout << "15 cents per pound.\n";
break;
default :
Tuesday, May 20, <<
cout 2025
"That is an invalid choice.\n"; 33
2. Looping
• A loop is a control structure that causes a
statement or group of statements to repeat.
• C++ has three looping control structures: the
while loop, the do-while loop, and the for
loop.
• The difference between each of these is how
they control the repetition.
Principle:
Condition of while loop is first executed (pretest loop)and if
condition is TRUE, statement is executed .Then ,condition is
evaluated again & if TRUE, statement is repeated. It loops until
the condition comes to be FALSE. when it ‘s FALSE, it leaves
the loop. 35
Tuesday, May 20, 2025
While Loop Statement
int i = 0;
while(i<10)
{
cout<<i<<” “;
i++;
}
cout<<”done!”
Tuesday, May 20, 2025 42
Infinite Loop
• Another common pitfall with loops is accidentally using
the = operator when you intend to use the == operator.
int i;
for(i=0;i<100;i+=2) int i;
cout<<i<<endl; for(i=100;i>0;i--)
return 0; cout<<i<<endl;
} return 0;
}
Tuesday, May 20, 2025 53
Multiple Declarations
• Although for loops typically iterate over only
one variable, sometimes for loops need to
work with multiple variables.
• When this happens, the programmer can
make use of the comma operator in order to
initialize or change the value of multiple
variables: E.g.
for (int i=0, j=9; i < 10; i++, j--)
cout << i << " " << j << endl;
Tuesday, May 20, 2025 54
Multiple Declarations
• The previous program produces the result
}
Tuesday, May 20, 2025 58
iii)goto statement :
Allows to make an absolute jump to another point in the program, the destination
point is identified by label(valid identifier followed by a colon(:).
Example:
#include<iostream.h>
int main ( )
{
int n =10;
loop:
cout << n << “ “;
n--;
if (n>0) goto loop;
cout<<”End”;
return 0;
}
10 ,9 ,8 ,7, 6, 5,4,3,2,1
Tuesday, May 20, 2025 59
How to solve problem
Develop the algorithm.
Convert to cpp code.
Check the result.
Example:
1) Prime number;
2) Grading system;
3) Simple calculator; etc