Session 5
Session Objectives <ul><ul><li>Identify the iteration constructs </li></ul></ul><ul><ul><li>Understand simple control stat...
Iteration Constructs <ul><ul><li>Iteration constructs supported by C++ includes </li></ul></ul>The   for   loop The   whil...
Basics of Loops <ul><ul><li>Step 1: - Initialising the control variable  </li></ul></ul><ul><ul><li>Step 2: - Evaluating e...
The while loop   The ‘while’ loop iterates until the expression 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...
‘  while ’ Example (1) #include <iostream.h> #include <conio.h>   void main(void) { int marks, att_percent ; char grade, c...
switch(marks >= 80)    { case 1 : grade = 'A' ; break ; case 0 : switch(marks >= 60){ case 1 : switch(att_percent >= 80) {...
case 0 : grade = 'P' ; break ; } break ;  case 0 : grade ='F' ; break ; } break ; } } cout << &quot;Grade: &quot; << grade...
The  do … while  Loop The ‘do..while’ loop is useful in  conditions where a certain set of  processing statements needs to...
Simple ‘do..while ’ Example (1) #include <iostream.h> #include <conio.h>   void main(void) { int n, i_sum = 0, right_digit...
Simple ‘do..while ’ Example (2) n /= 10 ;// Move next digit into rightmost position } while(n>0);//No more digits to extra...
Nested while and  do … while  Loop To enable multiple layers of iteration.  while (expression) {  while(expression {  stat...
Example (1) #include <iostream.h> #include <conio.h>   void main(void) { int class_no, marks, tot_studs, roll_no ; char ch...
cout << endl ; roll_no = 1 ; while(roll_no <= tot_studs) { cout << &quot;Enter MARKS for roll number &quot; ; cout << roll...
Enter class number: 5 Enter TOTAL STUDENTS IN CLASS: 5 Enter MARKS for roll number 1:65 MARKS ENTERED for roll number 1 = ...
The  for  loop (1) <ul><ul><li>The general syntax of the  for  statement is - </li></ul></ul><ul><ul><li>The initialize co...
The  for  loop (1)  <ul><ul><li>The three sections of the  for  loop must be separated by a semicolon( ; ).  </li></ul></u...
Simple ‘ For loop ’ Example #include <iostream.h> #include <conio.h>   void main(void) { int num ;   clrscr() ; for(num = ...
Multiple Initialization / Increments <ul><ul><li>The following loop is used for multiple initialization - </li></ul></ul>
#include <iostream.h> #include <conio.h>   void main(void) { int bats_score, team_score, bats_no, runs_scored ; char c_qui...
if(bats_no < 11) { cout << &quot;Quit entering data (Y/N) ? &quot; ; cin >> c_quit ; } } cout << &quot;
TOTAL TEAM SCORE: ...
TEAM SCORE: 0 BATSMAN 1 - RUNS SCORED: 56 Quit entering data (Y/N) ? n   TEAM SCORE: 56 BATSMAN 2 - RUNS SCORED: 56 Quit e...
Nested  for  Loops <ul><ul><li>The general syntax of the nested  for  loop is </li></ul></ul>for (expression 1; expression...
Simple ‘Nested For loop ’ Example #include <iostream.h> #include <conio.h>   void main(void) { int i, j, k ; i = 0 ;   clr...
The Comma Operator <ul><ul><li>Comma Operator </li></ul></ul>Example: b = 5, a = (++b), a + b   The value of the expressio...
<ul><ul><li>‘ break’ Statement </li></ul></ul>The break statement used within a loop causes the loop to be terminated and ...
Simple ‘ break ’ Example (1) #include <iostream.h> #include <conio.h>   void main(void) { int i, num ;   clrscr() ;   for(...
Simple ‘ break ’ Example (2) Enter any number between 1 & 10: 2 Enter any number between 1 & 10: 5 Break statement execute...
<ul><ul><li>‘ continue’ Statement </li></ul></ul>When continue statement is encountered in a loop, the rest of the stateme...
Simple ‘ continue ’ Example #include <iostream.h> #include <conio.h>   void main(void) { int i; clrscr() ; for(i = 1; i <=...
‘  continue and break’ Example (1) #include <iostream.h> #include <conio.h> void main(void) { int i_num, i_total = 0 ;    ...
‘  continue and break’ Example (2) Enter a number (0 to quit) : 45 Enter a number (0 to quit) : 45 Enter a number (0 to qu...
<ul><ul><li>exit() Construct </li></ul></ul>The function exit() is used to terminate a program immediately. An exit() is u...
‘  exit() construct’ Example (1) #include <iostream.h> #include <conio.h> #include <process.h>   void main(void) { int val...
‘  exit() construct’ Example (2) if(c_entry == 'E' || c_entry == 'e') exit(0) ; } cout << &quot;Exiting system...&quot; ; ...
Upcoming SlideShare
Loading in …5
×

C++ Program control statements2-session5

3,493 views

Published on

This is the C++ Control Statements

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,493
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
146
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

C++ Program control statements2-session5

  1. 1. Session 5
  2. 2. Session Objectives <ul><ul><li>Identify the iteration constructs </li></ul></ul><ul><ul><li>Understand simple control statements </li></ul></ul><ul><ul><li>The while loop </li></ul></ul><ul><ul><li>The do… while loop </li></ul></ul><ul><ul><li>Nested while and do…while loops </li></ul></ul><ul><ul><li>The for loop </li></ul></ul><ul><ul><li>Multiple initialisations / increments in for </li></ul></ul><ul><li>loop </li></ul><ul><ul><li>Nested for loops </li></ul></ul>
  3. 3. Iteration Constructs <ul><ul><li>Iteration constructs supported by C++ includes </li></ul></ul>The for loop The while loop The do….while loop
  4. 4. Basics of Loops <ul><ul><li>Step 1: - Initialising the control variable </li></ul></ul><ul><ul><li>Step 2: - Evaluating expression . </li></ul></ul><ul><ul><li>Step 3: - Incrementing the value of control variable </li></ul></ul>Standard Algorithm for any loop constructs is
  5. 5. The while loop The ‘while’ loop iterates until the expression is true. while (expression) Processing statement
  6. 6. 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.
  7. 7. ‘ 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 << &quot;Enter MARKS: &quot; ; cin >> marks ; cout << endl ;   cout << &quot;Enter ATTENDANCE PERCENTAGE: &quot; ; cin >> att_percent ; cout << endl ; 
  8. 8. 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)
  9. 9. case 0 : grade = 'P' ; break ; } break ; case 0 : grade ='F' ; break ; } break ; } } cout << &quot;Grade: &quot; << grade << endl ; cout << &quot;Continue (Y/N) : &quot; ; cin >> cont ; }} Enter MARKS: 56   Enter ATTENDANCE PERCENTAGE: 98   Grade: C Continue (Y/N) : n ‘ while ’ Example (3)
  10. 10. 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) ;
  11. 11. Simple ‘do..while ’ Example (1) #include <iostream.h> #include <conio.h>   void main(void) { int n, i_sum = 0, right_digit ;   cout << &quot;Type an integer value: &quot; ; cin >> n ; cout << endl ; do { right_digit = n % 10 ; // To extract rightmost digit i_sum += right_digit ;
  12. 12. 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 << &quot;The sum of the digits is: &quot; << i_sum ; } Type an integer value: 123 The sum of the digit is: 6
  13. 13. Nested while and do … while Loop To enable multiple layers of iteration. while (expression) { while(expression { statements } } do { do { statements }while(expression); }while(expression);
  14. 14. Example (1) #include <iostream.h> #include <conio.h>   void main(void) { int class_no, marks, tot_studs, roll_no ; char choice ;   clrscr() ;   do { cout << &quot;Enter class number: &quot; ; cin >> class_no ; cout << endl ;   if(class_no >0) { cout << &quot;Enter TOTAL STUDENTS IN CLASS: &quot; ; cin >> tot_studs ;  
  15. 15. cout << endl ; roll_no = 1 ; while(roll_no <= tot_studs) { cout << &quot;Enter MARKS for roll number &quot; ; cout << roll_no << &quot;:&quot; ; cin >> marks ; cout << endl ; cout << &quot;MARKS ENTERED for roll number &quot; ; cout << roll_no << &quot; = &quot; << marks << endl ; roll_no++ ; } } cout << &quot;Enter marks for another class (Y/N) ? &quot; ; cin >> choice ; clrscr() ;   } while(choice != 'N' && choice != 'n') ; } Example (2)
  16. 16. Enter class number: 5 Enter TOTAL STUDENTS IN CLASS: 5 Enter MARKS for roll number 1:65 MARKS ENTERED for roll number 1 = 65 Enter MARKS for roll number 2:67 MARKS ENTERED for roll number 2 = 67 Enter MARKS for roll number 3:54 MARKS ENTERED for roll number 3 = 54 Enter MARKS for roll number 4:76 MARKS ENTERED for roll number 4 = 76 Enter MARKS for roll number 5:89 MARKS ENTERED for roll number 5 = 89 Enter marks for another class (Y/N) ? N Example (3)
  17. 17. The for loop (1) <ul><ul><li>The general syntax of the for statement is - </li></ul></ul><ul><ul><li>The initialize counter is an assignment statement that sets the loop control variable, before entering the loop. </li></ul></ul><ul><ul><li>The conditional test is a relational expression, which determines, when the loop will exit. </li></ul></ul><ul><ul><li>The evaluation parameter defines how the loop control variable changes, each time the loop is executed. </li></ul></ul>
  18. 18. The for loop (1) <ul><ul><li>The three sections of the for loop must be separated by a semicolon( ; ). </li></ul></ul><ul><ul><li>The statement, which forms the body of the loop, can either be a single statement or a compound statement. </li></ul></ul><ul><ul><li>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. </li></ul></ul>
  19. 19. 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.
  20. 20. Multiple Initialization / Increments <ul><ul><li>The following loop is used for multiple initialization - </li></ul></ul>
  21. 21. #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 << &quot; TEAM SCORE: &quot; << team_score ; cout << &quot; BATSMAN &quot; << bats_no << &quot; - RUNS SCORED: &quot; ; cin >> runs_scored ; Example (1)
  22. 22. if(bats_no < 11) { cout << &quot;Quit entering data (Y/N) ? &quot; ; cin >> c_quit ; } } cout << &quot; TOTAL TEAM SCORE: &quot; << team_score ;} Example (2)
  23. 23. 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 Example (3)
  24. 24. Nested for Loops <ul><ul><li>The general syntax of the nested for loop is </li></ul></ul>for (expression 1; expression 2; expression 3) { for (expression 1; expression 2; expression 3) Processing statement ; }
  25. 25. Simple ‘Nested For loop ’ Example #include <iostream.h> #include <conio.h>   void main(void) { int i, j, k ; i = 0 ;   clrscr() ; cout << &quot;Enter number of rows: &quot; ; cin >> i ; cout << endl ; for(j = 0; j < i; j++) { cout << &quot; &quot; ; for(k = 0; k <= j; k++) { cout << &quot;*&quot; ; }}} Enter number of rows: 10 * ** *** **** ***** ****** ******* ******** ********* **********
  26. 26. The Comma Operator <ul><ul><li>Comma Operator </li></ul></ul>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.
  27. 27. <ul><ul><li>‘ break’ Statement </li></ul></ul>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
  28. 28. Simple ‘ break ’ Example (1) #include <iostream.h> #include <conio.h>   void main(void) { int i, num ;   clrscr() ;   for(i = 0; i < 10; i++) { cout << &quot;Enter any number between 1 & 10: &quot; ;
  29. 29. Simple ‘ break ’ Example (2) Enter any number between 1 & 10: 2 Enter any number between 1 & 10: 5 Break statement executed cin >> num ; cout << endl ;   if(num == 5) break ; } cout << &quot; Break statement executed&quot; ; }
  30. 30. <ul><ul><li>‘ continue’ Statement </li></ul></ul>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
  31. 31. 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 << &quot; &quot; ; } } This prints all the numbers from 1 to 100 which are not divisible by 9.
  32. 32. ‘ continue and break’ Example (1) #include <iostream.h> #include <conio.h> void main(void) { int i_num, i_total = 0 ;   clrscr() ;   do { cout << &quot;Enter a number (0 to quit) : &quot; ; cin >> i_num ;   if(i_num == 0) break ; else if(i_num < 0) continue ;  
  33. 33. ‘ continue and break’ Example (2) Enter a number (0 to quit) : 45 Enter a number (0 to quit) : 45 Enter a number (0 to quit) : 87 Enter a number (0 to quit) : 0 The sum of all the positive values entered : 177 i_total += i_num ; } while(1) ; cout << &quot;The sum of all the positive values entered : &quot; << i_total ; getch() ; }
  34. 34. <ul><ul><li>exit() Construct </li></ul></ul>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
  35. 35. ‘ 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 << &quot;Val = &quot; ; cin >> val ; cout << &quot;Enter E to exit system immediately &quot; ; cin >> c_entry ;
  36. 36. ‘ exit() construct’ Example (2) if(c_entry == 'E' || c_entry == 'e') exit(0) ; } cout << &quot;Exiting system...&quot; ; } Val = 45 Enter E to exit system immediately T Val = 3 Enter E to exit system immediately E

×