Вы находитесь на странице: 1из 36

Session 5

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 1 of 36

Session Objectives
Identify the iteration constructs 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 Understand simple control statements
Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 2 of 36

Iteration Constructs
Iteration constructs supported by C++ includes

The while loop The do.while loop

The for loop


Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 3 of 36

Basics of Loops
Standard Algorithm for any loop constructs is Step 1: - Initialising the control variable Step 2: - Evaluating expression. Step 3: - Incrementing the value of control variable

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 4 of 36

The while loop


The while loop iterates until the expression is true.

while (expression) Processing statement

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 5 of 36

Simple while Example


#include <iostream.h> #include <conio.h> void main(void) This will print the { numbers 0 to 10 on int x = 0 ; successive lines. clrscr() ; while(x <= 10) { cout << x++ << endl ; } }
Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 6 of 36

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 ;

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 7 of 36

while Example (2)


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 ;

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 8 of 36

while Example (3)


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: C Continue (Y/N) : n

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 9 of 36

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) ;

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 10 of 36

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 extract rightmost digit i_sum += right_digit ;

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 11 of 36

Simple do..while Example (2)


n /= 10 ;// Move next digit into rightmost position } while(n>0);//No more digits to extract if value of n = 0 cout << "The sum of the digits is: " << i_sum ; }

Type an integer value: 123 The sum of the digit is: 6

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 12 of 36

Nested while and do while Loop


To enable multiple layers of iteration.
while (expression) { while(expression { statements } } do { do { statements }while(expression); }while(expression);

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 13 of 36

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 ;

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 14 of 36

Example (2)
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') ; }
Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 15 of 36

Example (3)
Enter Enter Enter MARKS Enter MARKS Enter MARKS Enter MARKS Enter MARKS class number: 5 TOTAL STUDENTS IN CLASS: 5 MARKS for roll number 1:65 ENTERED for roll number 1 = MARKS for roll number 2:67 ENTERED for roll number 2 = MARKS for roll number 3:54 ENTERED for roll number 3 = MARKS for roll number 4:76 ENTERED for roll number 4 = MARKS for roll number 5:89 ENTERED for roll number 5 =

65 67 54 76 89

Enter marks for another class (Y/N) ? N

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 16 of 36

The for loop (1)


The general syntax of the for statement is -

The initialize counter is an assignment statement that sets the 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.
Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 17 of 36

The for loop (1)


The three sections of the for loop must be separated by a semicolon(;).

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.

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 18 of 36

Simple For loop Example


#include <iostream.h> #include <conio.h> void main(void) { int num ;

This will print the numbers 0 to 10 on successive lines.

clrscr() ; for(num = 0; num <= 10; num++) { cout << num << endl ; } }

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 19 of 36

Multiple Initialization / Increments


The following loop is used for multiple initialization -

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 20 of 36

Example (1)
#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 ;

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 21 of 36

Example (2)
if(bats_no < 11) { cout << "Quit entering data (Y/N) ? " ; cin >> c_quit ; } } cout << "\nTOTAL TEAM SCORE: " << team_score ;}

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 22 of 36

Example (3)
TEAM SCORE: 0 BATSMAN 1 - RUNS SCORED: 56 Quit entering data (Y/N) ? n TEAM SCORE: 56 BATSMAN 2 - RUNS SCORED: 56 Quit entering data (Y/N) ? n TEAM SCORE: 112 BATSMAN 3 - RUNS SCORED: 76 Quit entering data (Y/N) ? n TEAM SCORE: 188 BATSMAN 4 - RUNS SCORED: 56 Quit entering data (Y/N) ? n TEAM SCORE: 244 BATSMAN 5 - RUNS SCORED: 56 Quit entering data (Y/N) ? n TEAM SCORE: 300 BATSMAN 6 - RUNS SCORED: 65 Quit entering data (Y/N) ? y TOTAL TEAM SCORE: 365
Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 23 of 36

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 ; }

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 24 of 36

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 * ** *** **** ***** ****** ******* ******** ********* **********

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 25 of 36

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.

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 26 of 36

The break Statement


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.

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 27 of 36

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: " ;

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 28 of 36

Simple break Example (2)


cin >> num ; cout << endl ;

if(num == 5) break ; } cout << "\nBreak statement executed" ; }

Enter any number between 1 & 10: 2 Enter any number between 1 & 10: 5 Break statement executed
Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 29 of 36

The continue Statement


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.

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 30 of 36

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.

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 31 of 36

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 ;

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 32 of 36

continue and break Example (2)


i_total += i_num ; } while(1) ; cout << "The sum of all the positive values entered : " << i_total ; getch() ; } Enter a Enter a Enter a Enter a The sum number number number number of all (0 to quit) : 45 (0 to quit) : 45 (0 to quit) : 87 (0 to quit) : 0 the positive values entered : 177

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 33 of 36

The exit() Construct


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)

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 34 of 36

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 immediately " ; cin >> c_entry ;

to

exit

system

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 35 of 36

exit() construct Example (2)


if(c_entry == 'E' || c_entry == 'e') exit(0) ; } cout << "Exiting system..." ; }

Val = Enter Val = Enter

45 E to exit system immediately T 3 E to exit system immediately E

Linux, C, C++ / Object Oriented Programming with C++ / Session 5 / 36 of 36

Вам также может понравиться