Presentation is loading. Please wait.

Presentation is loading. Please wait.

REPETITION STATEMENTS

Similar presentations


Presentation on theme: "REPETITION STATEMENTS"— Presentation transcript:

1 REPETITION STATEMENTS
DO…WHILE

2 1. The DO…WHILE STATEMENT – SYNTAX
{ 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

3 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: The above menu should be displayed to the user at least once  Use do…while // Display a menu // These are the available options: printf (“R: Residential Customer\n”); printf (“C: Commercial Customer\n”); printf (“I: Industrial Customer\n”); // Instruct the user to enter his option printf (“Enter the user code> “); scanf (“%c”, &code); Dr. Soha S. Zaghloul 3

4 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 Customer\n”); printf (“C: Commercial Customer\n”); printf (“I: Industrial Customer\n”); 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

5 3. example (1) - complete solution
#include <stdio.h> int main (void) { char code; double consumption, amount_due; do { printf (“R: Residential Customer\n”); printf (“C: Commercial Customer\n”); printf (“I: Industrial Customer\n”); printf (“X: to exit \n”); printf (“Enter the user code> “); scanf (“%c”, &code); printf (“Enter consumption in KWH> “); scanf (“%f”, &consumption); 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 input \n”); break; } // end of switch printf (“Due amount = SR 6.1%f\n”, amount_due); } while ((code != ‘X’) ||(code != ‘x’)) return (0); }// end of main Note the braces

6 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

7 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

8 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; case ‘I’: amount_due = 15 * consumption; case ‘X’: break; default: printf (“invalid input\n”); } printf (“Due amount = SR 6.1%f\n”, amount_due); Dr. Soha S. Zaghloul 8

9 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

10 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 (“\nBroke out of loop at x equals %d\n”, x); return (0); } // end of main OUTPUT Broke out of loop at x equals 5 Dr. Soha S. Zaghloul 10

11 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 program\n”); 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

12 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

13 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 Used continue to skip printing the value 5 Dr. Soha S. Zaghloul 13

14 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

15 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. 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

16 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 number\n”); 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

17 An infinite loop is a loop whose condition is never satisfied.
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

18 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

19 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


Download ppt "REPETITION STATEMENTS"

Similar presentations


Ads by Google