Professional Documents
Culture Documents
Chapter 3 - 1
Chapter 3 - 1
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;
switch (condition)
The switch Statement {
case value1:
Multiple selection structure statement11;
...
Performs different actions for break;
different test expression values case value2:
Less general than nested statement21;
if-else ...
statement break;
integer test expression only
...
test for equality only
case valueN:
statementN1;
...
break;
default:
statementD;
...
}
THE SWITCH STATEMENT FLOWCHART
(CONT’D)
switch (expression)
{ The switch Statement (cont’d)
case value1:
statement11; Uses 4 keywords
... switch
break;
case value2: identifies start of switch statement
statement21;
... expression in parentheses (switch
break; expression) evaluated & its value
... compared to various alternative (case)
case valueN: values within compound statement
statementN1; that follows
...
break;
default:
statementD;
...
}
(CONT’D)
switch (expression) The switch Statement (cont’d)
{
case value1: Uses
statement11; 4 keywords (cont’d)
... Case
break;
case value2: identifies possible entry points of switch
statement21; statement
...
break; comparison done in order in which case
cout<<"vowel";
break;
case ' ':
cout<<"space";
break;
default:
cout<<"consonant";
break;
}
return 0;
}
NESTED SWITCH STATEMENT
#include <iostream>
using namespace std;
This is outer switch
int main ( )
This is inner switch
{ The value of a is : 100
int a = 100; The value of b is : 200
int b = 200;
switch (a) {
case 100:
cout << "This is outer switch" << endl;
switch (b) {
case 200:
cout << "This is inner switch" << endl;
}
}
cout << "The value of a is : " << a << endl;
cout << "The value of b is : " << b << endl;
return 0;
}
1.5. THE CONDITIONAL EXPRESSIONS
Write a program that gives grade based on the following scale using if else
statement:
• Write a program that display greatest of three numbers using if statement accept
input from user.
CONT..
• Write a program that accepts three numbers from the user and
prints "increasing" if the numbers are in increasing order,
"decreasing" if the numbers are in decreasing order and "Neither
increasing nor decreasing order" otherwise.
Hint: accept at least three course marks then calculate the total,
and average of your mark, the status will be based on average
value. Display the total mark, average and status.
2. ITERATION OR LOOPING STATEMENTS
#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++;
}
do keyword do
{
(single statement or
compound statement)
body
{ statement(s);
}
while (condition);
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
• The code goto is used for moving back and forth in the program.
Therefore, for using goto statement one needs to put in a label.
• Syntax:
EXAMPLE : GOTO STATEMENT AS A
LOOPING
int main(){
int n , m;
cout<<“Write and enter two integers: ” ;
cin>> n >> m;
cout<< “You have written the numbers as n= ”<<n <<“
and m = ”<<m <<endl;
Again: //The label Again, see colon at end.
if(n < m) n++; else m++;
if(n == m)
cout<<“Now m = ” << m<<“ and n = ” << n<<“\n”;
else
goto Again; //Jump back to Again making a loop
return 0;
}
3.2. THE BREAK STATEMENT
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
Write a program that calculates sum of numbers from 1 to 100.
Write a program that displays numbers between 0 -100 that are
divisible by 2, 3, and 5. The numbers displayed should be those
that can be divided by 2, 3, and 5 without remainder.
Write a program that calculates factorial using for loop, while
loop and do while loops. The program should accept the number
and then perform the calculation of the factorial.
Write a while loop that prints the average of numbers from 1 to
10