REPETITION STATEMENTS

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Nested LOOPS.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Iterations Very Useful: Ability to repeat a block of code Example:
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Repetition statements
ARRAYS.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
ECE Application Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 5: Control Structures II
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 4 - Program Control
REPETITION STATEMENTS
CS1010 Programming Methodology
FUNCTIONS EXAMPLES.
Chapter 2.2 Control Structures (Iteration)
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 5: Control Structures II
Control Structures Lecture 7.
Looping.
Looping and Repetition
- Additional C Statements
CS1100 Computational Engineering
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Functions.
Chapter 4 - Program Control
1) C program development 2) Selection structure
Control Statements Loops.
Decision making If statement.
Program Control Topics While loop For loop Switch statement
Chapter 2.2 Control Structures (Iteration)
Week 6 CPS125.
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Functions Extra Examples.
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
CprE 185: Intro to Problem Solving (using C)
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

REPETITION STATEMENTS DO…WHILE

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

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

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

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

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

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: #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 1 2 3 4 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: #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

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

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

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

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