Upload
chiranjeevi-veluru
View
3.218
Download
1
Tags:
Embed Size (px)
DESCRIPTION
This is the C++ Control Statements
Citation preview
Session 5
Session Objectives• Identify the iteration constructs
• Understand simple control statements
• The while loop• The do… while loop• Nested while and do…while loops • The for loop• Multiple initialisations / increments
in for loop• Nested for loops
Iteration Constructs
• Iteration constructs supported by C++ includes
The for loop
The while loop
The do….while loop
Basics of Loops
• Step 1: - Initialising the control variable
• Step 2: - Evaluating expression.
• Step 3: - Incrementing the value of control variable
Standard Algorithm for any loop constructs is
The while loop
The ‘while’ loop iterates until theexpression is true.
while (expression)Processing statement
Simple ‘ while ’ Example#include <iostream.h>
#include <conio.h>void main(void){ int x = 0 ;clrscr() ; while(x <= 10){cout << x++ << endl ;}}
This will print the numbers 0 to 10 on successive lines.
‘ while ’ Example (1)
#include <iostream.h>#include <conio.h> void main(void){int marks, att_percent ;char grade, cont = 'Y' ;while(cont != 'N' && cont != 'n'){clrscr() ; cout << "Enter MARKS: " ;cin >> marks ;cout << endl ; cout << "Enter ATTENDANCE PERCENTAGE: " ;cin >> att_percent ;cout << endl ;
switch(marks >= 80) { case 1 : grade = 'A' ;break ;case 0 : switch(marks >= 60){case 1 : switch(att_percent >= 80){ case 1 : grade = 'A' ;break ;case 0 : grade = 'B' ;break ; }break ; case 0 : switch(marks >= 35){case 1 : switch(att_percent >= 80){ case 1 : grade = 'C' ;break ;
‘ while ’ Example (2)
case 0 : grade = 'P' ;break ; }break ;
case 0 : grade ='F' ;break ;}break ; } }cout << "Grade: " << grade << endl ;cout << "Continue (Y/N) : " ;cin >> cont ;}}
Enter MARKS: 56 Enter ATTENDANCE PERCENTAGE: 98 Grade: CContinue (Y/N) : n
‘ while ’ Example (3)
The do…while Loop
The ‘do..while’ loop is useful in conditions where a certain set of processing statements needs to be performed at least once.
do{Processing statements} while (expression) ;
Simple ‘do..while ’ Example (1)
#include <iostream.h>#include <conio.h> void main(void){int n, i_sum = 0, right_digit ; cout << "Type an integer value: " ;cin >> n ;cout << endl ;do{right_digit = n % 10 ; // To extractrightmost digiti_sum += right_digit ;
Simple ‘do..while ’ Example (2)
n /= 10 ;// Move next digit into rightmostposition}while(n>0);//No more digits to extract ifvalue of n = 0cout << "The sum of the digits is: " << i_sum ;}
Type an integer value: 123The sum of the digit is: 6
Nested while and do…while Loop
To enable multiple layers of iteration.
while (expression){ while(expression { statements}}
do{do{statements}while(expression);}while(expression);
Example (1)
#include <iostream.h>#include <conio.h> void main(void){int class_no, marks, tot_studs, roll_no ;char choice ; clrscr() ; do{cout << "Enter class number: " ;cin >> class_no ;cout << endl ; if(class_no >0){cout << "Enter TOTAL STUDENTS IN CLASS: " ;cin >> tot_studs ;
cout << endl ;roll_no = 1 ;while(roll_no <= tot_studs){cout << "Enter MARKS for roll number " ;cout << roll_no << ":" ;cin >> marks ;cout << endl ;cout << "MARKS ENTERED for roll number " ;cout << roll_no << " = " << marks << endl ;roll_no++ ;}}cout << "Enter marks for another class (Y/N) ? " ;cin >> choice ;clrscr() ; } while(choice != 'N' && choice != 'n') ;}
Example (2)
Enter class number: 5Enter TOTAL STUDENTS IN CLASS: 5Enter MARKS for roll number 1:65MARKS ENTERED for roll number 1 = 65Enter MARKS for roll number 2:67MARKS ENTERED for roll number 2 = 67Enter MARKS for roll number 3:54MARKS ENTERED for roll number 3 = 54Enter MARKS for roll number 4:76MARKS ENTERED for roll number 4 = 76Enter MARKS for roll number 5:89MARKS ENTERED for roll number 5 = 89
Enter marks for another class (Y/N) ? N
Example (3)
The for loop (1)• The general syntax of the for statement is
-
• The initialize counter is an assignment statement that setsthe loop control variable, before entering the loop.
• The conditional test is a relational expression, which determines, when the loop will exit.
• The evaluation parameter defines how the loop control variable changes, each time the loop is executed.
The for loop (1) • The three sections of the for loop must be separated by
asemicolon(;).
• The statement, which forms the body of the loop, can either be a single statement or a compound statement.
• The for loop continues to execute as long as the conditional test evaluates to true. When the condition becomes false, the program resumes on the statement following the for loop.
Simple ‘ For loop ’ Example#include <iostream.h>#include <conio.h> void main(void){int num ; clrscr() ;for(num = 0; num <= 10; num++){cout << num << endl ;}}
This will print the numbers 0 to 10 on successive lines.
Multiple Initialization / Increments
• The following loop is used for multiple initialization -
#include <iostream.h>#include <conio.h> void main(void){int bats_score, team_score, bats_no, runs_scored ;char c_quit ;clrscr() ;for(team_score = 0, bats_no = 1; bats_no <= 11 && c_quit != 'Y' && c_quit != 'y'; bats_no++, team_score += runs_scored){cout << "\nTEAM SCORE: " << team_score ;cout << "\t\tBATSMAN " << bats_no << " - RUNS SCORED: " ;cin >> runs_scored ;
Example (1)
if(bats_no < 11){cout << "Quit entering data (Y/N) ? " ;cin >> c_quit ;} }cout << "\nTOTAL TEAM SCORE: " << team_score ;}
Example (2)
TEAM SCORE: 0 BATSMAN 1 - RUNS SCORED: 56Quit entering data (Y/N) ? n TEAM SCORE: 56 BATSMAN 2 - RUNS SCORED: 56Quit entering data (Y/N) ? n TEAM SCORE: 112 BATSMAN 3 - RUNS SCORED: 76Quit entering data (Y/N) ? n TEAM SCORE: 188 BATSMAN 4 - RUNS SCORED: 56Quit entering data (Y/N) ? n TEAM SCORE: 244 BATSMAN 5 - RUNS SCORED: 56Quit entering data (Y/N) ? n TEAM SCORE: 300 BATSMAN 6 - RUNS SCORED: 65Quit entering data (Y/N) ? y TOTAL TEAM SCORE: 365
Example (3)
Nested for Loops
• The general syntax of the nested for loop is
for (expression 1; expression 2; expression 3){for (expression 1; expression 2; expression 3)Processing statement ;}
Simple ‘Nested For loop ’ Example#include <iostream.h>
#include <conio.h> void main(void){int i, j, k ;i = 0 ; clrscr() ;cout << "Enter number of rows: " ;cin >> i ;cout << endl ;for(j = 0; j < i; j++){cout << "\n" ;for(k = 0; k <= j; k++){cout << "*" ;}}}
Enter number of rows: 10*******************************************************
The Comma Operator
• Comma Operator
Example:
b = 5, a = (++b), a + b The value of the expression is 12. When the last sub-expression; that is, a + b is calculated, b has the value of 6 and a has the value of 6.
• ‘break’ Statement
The break statement used within a loop causes the loop to be terminated and forces the execution to proceed with the statements following that loop.
The break Statement
Simple ‘ break ’ Example (1)
#include <iostream.h>#include <conio.h> void main(void){int i, num ; clrscr() ; for(i = 0; i < 10; i++){cout << "Enter any number between 1 & 10: " ;
Simple ‘ break ’ Example (2)
Enter any number between 1 & 10: 2Enter any number between 1 & 10: 5Break statement executed
cin >> num ;cout << endl ; if(num == 5)break ;}cout << "\nBreak statement executed" ;}
• ‘continue’ Statement
When continue statement is encountered in a loop, the rest of the statements in the loop are skipped and the control passes to the condition, which is evaluated and if TRUE the loop is entered again.
The continue Statement
Simple ‘ continue ’ Example#include <iostream.h>#include <conio.h> void main(void){int i;clrscr() ;for(i = 1; i <= 100; i++){if(i % 9 == 0){continue ;}cout << i << "\t" ;}}
This prints all the numbers from 1 to 100 which are not divisible by 9.
‘ continue and break’ Example (1)
#include <iostream.h>#include <conio.h>void main(void){int i_num, i_total = 0 ; clrscr() ; do{cout << "Enter a number (0 to quit) : " ;cin >> i_num ; if(i_num == 0)break ;else if(i_num < 0)continue ;
‘ continue and break’ Example (2)
Enter a number (0 to quit) : 45Enter a number (0 to quit) : 45Enter a number (0 to quit) : 87Enter a number (0 to quit) : 0The sum of all the positive values entered : 177
i_total += i_num ;} while(1) ;cout << "The sum of all the positive values entered : " << i_total ;getch() ;}
• exit() Construct
The function exit() is used to terminate a program immediately. An exit() is used to check if a mandatory condition for a program execution is satisfied or not. The general form of an exit() is: exit(int return_code)
The exit() Construct
‘ exit() construct’ Example (1)#include <iostream.h>#include <conio.h>#include <process.h> void main(void){int val = 1 ;char c_entry ;clrscr() ;while(val <= 50){cout << "Val = " ;cin >> val ;cout << "Enter E to exit system immediately " ;cin >> c_entry ;
‘ exit() construct’ Example (2)
if(c_entry == 'E' || c_entry == 'e')exit(0) ;}cout << "Exiting system..." ;}
Val = 45Enter E to exit system immediately TVal = 3Enter E to exit system immediately E