Chapter 3 Control Statments
Chapter 3 Control Statments
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
}
Sunday, March 23, 2025 7
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
statement N
Sunday, March 23, 2025 13
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;
Sunday, March 23, 2025 14
//This program illustrates a bug that occurs when independent if/else statements are used to
assign a letter grade to a test score.
#include <iostream>
using namespace std;
int main()
{
int testScore; char grade;
cout << "Enter your test score and I will tell you \n” the letter grade you earned: ";
cin >> testScore;
if (testScore < 60)
grade = 'F'; Program Output with Example Input Shown in
if (testScore < 70) Bold
grade = 'D'; Enter your test score and I will tell you
if (testScore < 80) the letter grade you earned: 40[Enter]
Your grade is A.
grade = 'C';
if (testScore < 90)
grade = 'B';
if (testScore <= 100)
grade = 'A';
cout << "Your grade is " << grade << ".\n";
return 0; Sunday, March 23, 2025 15
}
//It can be corrected like this
#include <iostream>
using namespace std;
int main()
{
int testScore; char grade;
cout << "Enter your 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
else if (testScore < 70) Bold
grade = 'D'; Enter your test score and I will tell you
else if (testScore < 80) the letter grade you earned: 40[Enter]
grade = 'C'; Your grade is F.
else if (testScore < 90)
grade = 'B';
else (testScore <= 100)
grade = 'A';
cout << "Your grade is " << grade << ".\n";
return 0;
Sunday, March 23, 2025 16
}
Dangling else/ matching else problem
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.
31
Sunday, March 23, 2025
While Loop Statement
This outputs:
0 1 2 3 4 5 6 7 8 9 done!
Sunday, March 23, 2025 33
While Loop Statement
An important characteristic of the while loop is that
the loop will never iterate if the test expression is
false.
It is possible that a while loop statement executes 0
times.
int i = 15;
while(i<10)
{
cout<<i<<” “;
i++;
}
Sunday, March 23, 2025 34
Example
// Loop through every number between 1 and 50
int i= 1;
while (i<= 50)
{
int i = 0;
while(i<10)
{
cout<<i<<” “;
i++;
}
cout<<”done!”
int i; int i;
for(i=0;i<100;i+=2) for(i=100;i>0;i--)
cout<<i<<endl; cout<<i<<endl;
return 0;
return 0;
}
}
Sunday, March 23, 2025 49
Multiple Declarations
• 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;
i) break statement
when executed within for, while, do…while and switch, it causes an
immediate exit from the loop.
Example:
Out put
for(int i=1;i<=10;i++) 1
{ 2
3
if(i==5) 4
break ;
cout<<i<<endl;
}
Example:
for(int i=1;i<=10;i++) Out put
1
{ 2
3
if(i==5) 4
6
continue; 7
8
cout<<i<<endl; 9
} 10