Академический Документы
Профессиональный Документы
Культура Документы
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
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
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 ;
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
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 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.
clrscr() ; for(num = 0; num <= 10; num++) { cout << num << endl ; } }
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 ;
Example (2)
if(bats_no < 11) { cout << "Quit entering data (Y/N) ? " ; cin >> c_quit ; } } cout << "\nTOTAL TEAM SCORE: " << team_score ;}
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
for (expression 1; expression 2; expression 3) { for (expression 1; expression 2; expression 3) Processing statement ; }
Enter number of rows: 10 * ** *** **** ***** ****** ******* ******** ********* **********
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.
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.
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
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.
This prints all the numbers from 1 to 100 which are not divisible by 9.
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)
to
exit
system