Chapter 3 C++++++++++++++++++
Chapter 3 C++++++++++++++++++
CONTROL STATEMENTS
CONTROL OF FLOW
Sequence o Selection
Programs executed
• Programs executed
sequentially by default
dependent on a
… ...
condition being
satisfied.
Statement 1
True False
Condition?
Statement 2
Statement A Statement B
… ...
BASIC STRUCTURES IN PROGRAMMING LANGUAGES (2)
Loop
True
Condition? Statement
False
Statement
True
Condition?
False
1. CONDITIONAL OR SELECTION STATEMENTS
if (Boolean_expression)
{
statement(s);
}
}
else
{
statement(s);
}
EXAMPLE
#include<iostream.h>
int main()
{
int grade;
cout<<“Enter the student grade: ”;
cin>>grade;
if ( grade >= 50 )
cout << “\n Passed“;
else
cout << “\n Failed“;
return 0;
}
// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
if (number >= 0)
{
cout << "You entered a positive integer: " << number << endl;
}
else
{
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}
1.2.1. THE MULTI-WAY IF-ELSE
STATEMENT
The syntax of the if...else if...else statement is:
if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else {
// code block 3
}
}
return 0;
}
1.3 NESTED IF STATEMENT
An if statement can be inside another if
statement to form a nested if statement.
Its syntax is:
// outer if statement
if (condition1)
{
// statements
// inner if statement
if (condition2)
{
// statements
}
}
NESTED IF STATEMENT
if (i > k)
{
if (j > k)
cout << "i and j are greater than k" <<
endl;
}
else
cout << "i is less than or equal to k" <<
endl;
The switch
switch (condition)
{
case value1:
Statement statement11;
...
Multiple selection break;
case value2:
structure statement21;
...
Performs different actions break;
for different test
expression values ...
Less general than nested case valueN:
if-else statement statementN1;
...
integer test expression only break;
test for equality only
default:
statementD;
...
}
THE SWITCH STATEMENT FLOWCHART
(CONT’D)
if (x > 0) y = x>
0 ? 1 : -1;
y = 1; Ø You might want to assign a variable
else a value that is restricted by certain
y = -1; conditions.
E.g. int max = (a>b) ? a : b;
QUIZ
#include <iostream.h>
int main()
{
int counter;
for (counter = 1; counter <= 10; counter++)
cout << counter << endl;
return(0);
}
(CONT’D)
#include<iostream>
using namespace std;
int main(){
for (int i=1;i<=100;i++){
if(i %2==0){
cout<<i <<endl;
}
}
}
2.2. THE WHILE LOOP
Syntax: while (conditional expression)
statements;
Ø This implies the statement following it will be carried out as
long as the conditional expression evaluates true, i.e. it is
more than zero.
E.g. let variables n, i and Sum are declared as integers.
The number n is initialized as 10, Sum as 0 and i as 0.
while (i <= n)
Sum += i++;
cout << “Sum 0 to 10 = ” << Sum <<endl;
Ø The process is repeated again and again till i =10. So Sum
will become 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55.
Ø After the loop is over, Sum 0 to 10 = 55 will be printed.
(CONT)
#include <iostream.h>
int main()
{
float average;
int Number,counter=0,sum=0;
while(counter<5)
{
cout <<“\n Enter the number”;
cin>>Number;
sum=sum+Number;
counter++;
}
average=sum/5;
cout<<“\n The average is:”<<average;
return 0;
}
WHAT WILL BE THE OUTPUT?
counter = 0;
while (counter < 5)
{
cout << "\nI love ice cream!";
counter++;
}
while
keyword condition
(enclosed in must
parenthesis) Have ;
(CONT’D)
false
EX. THE SUM OF ANY NUMBER OF INTEGERS
ENTERED BY THE USER.
int number,sum=0;
char loop_response;
do
{
cout << "\n Enter the number? ";
cin >> number;
sum+=number;
cout << "\nDo you want to do it again? y or
n ";
cin >> loop_response;
} while (loop_response == 'y');
cout<<“\n The sum is :”<<sum;
// print lowercase alphabet.
char ch = `a';
do
{
cout << ch << ` `;
ch++;
} while ( ch <= `z' );
FLOW OF CONTROL
counter = 1; counter = 1;
while (counter <= 10) do
{ {
cout << counter << endl; cout << counter << endl;
counter++; counter++;
} } while (counter <= 10);
return(0); return(0);
} }
3. JUMPING STATEMENTS
int main() {
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << "\n";
}
return 0;
}
3.3. THE CONTINUE STATEMENT
• continue statement causes the loop to skip the
rest of its body and immediately retest its
condition prior to reiterating.
• For the for loop, continue causes the conditional
test and increment portions of the loop to
execute.
• For the while and do...while loops, program
control passes to the conditional tests.
• It is an error to use the continue statement
outside a loop.
E.G. C++ PROGRAM TO DISPLAY INTEGER FROM 1 TO 10 EXCEPT 6
AND 9.
BREAK VS. CONTINUE
The continue statement works somewhat like the break
statement. Instead of forcing termination, however,
continue forces the next iteration of the loop to take place,
skipping any code in between.
QUIZ