DOWHILE 1 THE DOWHILE STATEMENT SYNTAX do statement

  • Slides: 19
Download presentation
DO…WHILE

DO…WHILE

1. THE DO…WHILE STATEMENT – SYNTAX do { statement 1 statement 2 ----} while

1. THE DO…WHILE STATEMENT – SYNTAX do { statement 1 statement 2 ----} while (condition) statement n � The do…while executes the body loop then it evaluates the condition. � If the condition is true, the execution of the body loop repeats. Otherwise, it goes to statement n. Dr. Soha S. Zaghloul 2

2. THE DO…WHILE STATEMENT – WHY? ? � The do…while statement is used in

2. THE DO…WHILE STATEMENT – WHY? ? � The do…while statement is used in case you want the loop body to execute at least once. � Example displaying a menu, and waiting the input from the user. Consider the following example: // Display a menu // These are the available options: printf (“R: Residential Customern”); printf (“C: Commercial Customern”); printf (“I: Industrial Customern”); // Instruct the user to enter his option printf (“Enter the user code> “); scanf (“%c”, &code); � The above menu should be displayed to the user at least once Use do…while Dr. Soha S. Zaghloul 3

3. THE DO…WHILE STATEMENT – EXAMPLE (1) � An option should be also added

3. THE DO…WHILE STATEMENT – EXAMPLE (1) � An option should be also added as a SENTINEL to exit from the loop. do { // Display a menu // These are the available options: printf (“R: Residential Customern”); printf (“C: Commercial Customern”); printf (“I: Industrial Customern”); printf (“X: to exit n”); // Instruct the user to enter his option printf (“Enter the user code> “); scanf (“%c”, &code); } while ((code != ‘X’) ||(code != ‘x’)) Dr. Soha S. Zaghloul 4

3. EXAMPLE (1) - COMPLETE SOLUTION #include <stdio. h> 2. int main (void) {

3. EXAMPLE (1) - COMPLETE SOLUTION #include <stdio. h> 2. int main (void) { 3. char code; 4. double consumption, amount_due; 5. do { printf (“R: Residential Customern”); 6. printf (“C: Commercial Customern”); 7. printf (“I: Industrial Customern”); 8. printf (“X: to exit n”); 9. printf (“Enter the user code> “); scanf (“%c”, &code); 10. printf (“Enter consumption in KWH> “); scanf (“%f”, &consumption); 11. switch (code) { 12. case ‘R’: amount_due = 5 * consumption; 13. case ‘C’: amount_due = 10 * consumption; break; 14. case ‘I’: amount_due = 15 * consumption; break; 15. case ‘X’: break; 16. default : printf (“Invalid input n”); break; 17. } // end of switch 18. printf (“Due amount = SR 6. 1%fn”, amount_due); 19. 20. 21. } while ((code != ‘X’) ||(code != ‘x’)) return (0); }// end of main Note the braces 1.

4. THE DO…WHILE STATEMENT – vs. FOR & WHILE � Both for and while

4. THE DO…WHILE STATEMENT – vs. FOR & WHILE � Both for and while statements evaluate the condition before deciding to execute the loop body or not. � The do…while statement executes the loop body then evaluates the condition to check if it should be re-executed or not. � If you don’t know the number of iterations, use while or do…while. � A problem solved with for, can also be solved using while. Dr. Soha S. Zaghloul 6

5. SELF-CHECK EXERCISE Write a complete program that displays a menu to perform an

5. SELF-CHECK EXERCISE Write a complete program that displays a menu to perform an arithmetic operation between two non-integer numbers. The user should select one of the following symbols: +, -, *, /, and %. The menu should contain a sentinel to exit from the program. Dr. Soha S. Zaghloul 7

6. THE BREAK STATEMENT – REVISITED printf (“Enter customer code> “); scanf (“%c”, &code);

6. THE BREAK STATEMENT – REVISITED printf (“Enter customer code> “); scanf (“%c”, &code); switch (code) { case ‘R’: amount_due = 5 * consumption; break; case ‘C’: amount_due = 10 * consumption; break; case ‘I’: amount_due = 15 * consumption; break; case ‘X’: break; default: printf (“invalid inputn”); break; } printf (“Due amount = SR 6. 1%fn”, amount_due); Dr. Soha S. Zaghloul 8

7. THE BREAK STATEMENT WITHIN LOOPS � The break statement may be executed in

7. THE BREAK STATEMENT WITHIN LOOPS � The break statement may be executed in a for, while, do…while, and switch commands. � In all cases, it causes an immediate exit from that statement. � Execution continues with the next statement immediately. � Therefore, a break statement is used to exit early from a loop (or a switch). � In general, a break statement is executed after checking a specific condition. Therefore, it is found with an if statement in a loop. Dr. Soha S. Zaghloul 9

8. THE BREAK STATEMENT WITHIN LOOPS – EXAMPLE 1 � Consider the following code:

8. THE BREAK STATEMENT WITHIN LOOPS – EXAMPLE 1 � Consider the following code: #include <stdio. h> int main (void) { int x; // to be used as a counter for (x = 1; x <= 10; x++) { if (x == 5) break; // break loop only when x equals 5 printf (“%d”, x); } // end for printf (“n. Broke out of loop at x equals %dn”, x); return (0); } // end of main OUTPUT 1234 Broke out of loop at x equals 5 Dr. Soha S. Zaghloul 10

9. THE BREAK STATEMENT WITHIN LOOPS – EXAMPLE 2 � Consider the following code:

9. THE BREAK STATEMENT WITHIN LOOPS – EXAMPLE 2 � Consider the following code: #include <stdio. h> int main (void) { int x, number; for (x = 1; x <= 10; x++) { printf (“Enter an integer, 0 exits the programn”); scanf (“%d”, &number); if (number == 0) break; // break loop only user enters 0 sum += number; // executes if number != 0 printf (“%d%d”, x, number); } // end for // if loop is broken, then x <= 10, and number = 0 printf (“n sum = %d, x = %d, number = %d”, sum, x, number); return (0); } // end of main Dr. Soha S. Zaghloul 11

10. THE CONTINUE STATEMENT � The continue statement is executed within a for, while,

10. THE CONTINUE STATEMENT � The continue statement is executed within a for, while, do…while loops. � In all cases, it skips the remaining of the statements in the body loop, checks the condition and starts a new iteration if the condition is true. � Like the break statement, it is generally coupled with an if statement. Dr. Soha S. Zaghloul 12

11. THE CONTINUE STATEMENT – EXAMPLE (1) � Consider the following code fragment: #include

11. THE CONTINUE STATEMENT – EXAMPLE (1) � Consider the following code fragment: #include <stdio. h> Int main (void) { int x; // counter for (x = 1; x <= 10; x++) { if (x == 5) continue; // restart a new iteration printf (“%d”, x); } // end for printf (n Used continue to skip printing the value 5 n”); return (0); } // end of main OUTPUT 1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5 Dr. Soha S. Zaghloul 13

12. EXAMPLE (2) Write a complete program that outputs the sum of odd numbers,

12. EXAMPLE (2) Write a complete program that outputs the sum of odd numbers, and the sum of even numbers. If the number is negative skip it. The user should enter 0 to end his data entry. How to recognize that a number x is even? x%2=0 How to recognize that a number x is odd? x % 2 != 0 How to recognize that a number x is positive? x>0 How to recognize that a number x is negative? x<0 Dr. Soha S. Zaghloul 14

12. EXAMPLE 2 – CNT’D Write a complete program that outputs the sum of

12. EXAMPLE 2 – CNT’D Write a complete program that outputs the sum of odd numbers, and the sum of even numbers. If the number is negative skip it. The user should enter 0 to end his data entry while (x != 0) If the number is negative skip it if (x < 0) continue; Dr. Soha S. Zaghloul 15

12. EXAMPLE 2 – CNT’D #include <stdio. h> int main (void) { int number

12. EXAMPLE 2 – CNT’D #include <stdio. h> int main (void) { int number = -1; // initialize with a non-zero value to enter the loop int sumodd = 0, sumeven = 0; while (number != 0) { printf (“Enter numbern”); scanf (“%d”, &number); if (number < 0) continue; if ((number % 2) == 0) sumeven += number; else sumodd += number; } // end while printf (“Sum odd = %d, sum even = %d”, sumodd, sumeven); return (0); } // end main Dr. Soha S. Zaghloul 16

13. INFINITE LOOPS � An infinite loop is a loop whose condition is never

13. INFINITE LOOPS � An infinite loop is a loop whose condition is never satisfied. � The previous example could be rewritten as follows: Dr. Soha S. Zaghloul 17

14. EXAMPLE 2 – ANOTHER SOLUTION #include <stdio. h> int main (void) { int

14. EXAMPLE 2 – ANOTHER SOLUTION #include <stdio. h> int main (void) { int number, sumodd = 0, sumeven = 0; int loop = 1; // used to enter the loop while (loop == 1) { printf (“Enter number> ”); scanf (“%d”, &number); if (number == 0) break; if (number < 0) continue; if ((number % 2) == 0) sumeven += number; else sumodd += number; } // end while printf (“Sum odd = %d, sum even = %d”, sumodd, sumeven); } // end main Dr. Soha S. Zaghloul 18

15. SELF-CHECK EXERCISE 2 Write a complete program that allows the user to enter

15. SELF-CHECK EXERCISE 2 Write a complete program that allows the user to enter values and prints out number of positive values, and the number of negative values entered. In your program, use a sentinel-controlled loop using zero as the sentinel value. Dr. Soha S. Zaghloul 19