150 likes | 151 Views
Learn the different types of loops in programming, including while loops and do/while loops. Understand their structures and see examples of how they are used. Also, learn when to use determinate vs. indeterminate loops.
E N D
Agenda • Perform Quiz #1 (20 minutes) • Loops • Introduction / Purpose • while loops • Structure / Examples involving a while loop • do/while loops • Structure / Examples involving a do/while loop • Homework Question
Loops • Loops are used for repetition when running a program. • To make sure the loop ends, we need a condition to control the loop; otherwise, the loop will continue forever! Perform action
Types of loops • while and do/while Loops • The “while” and “do/while” loops are often called indeterminate loops because they are best used when the number of repetitions are not known in advance. • for Loops • The “for” loop is often called a determinate loop since it is best used when the number of repetitions is already known.
In Class Exercise #1 • Which are the best types of loops for the following situations: determinate or indeterminate? • ask user to enter a group of marks until they are finished. • print a multiplication table (1 - 12) • indicate that user has inputted an invalid selection, and needs to try again (error checking)
while loop • The “while” loop test the condition first (referred to as a pre-test). • Therefore, if the pre-test condition is FALSE, the loop is NOT executed. false Testcondition true Commandsif TRUE
while loop - Example 1 Declare and assign variable i (counter) • main () • { • int i = 0; • while ( i < 3 ) • { • printf (“i is: %d\n”, i); • i = i + 1; • } • } • OUTPUT: • i is: 0 • i is: 1 • i is: 2 Pretest condition involving variable i Perform action in while loop if test is TRUE, advance the value of variable i (or “counter”) by 1 and return to pretest Tip: the short-form of i = i + 1; is i++; i = I - 1; is i--;
while loop - Example 2 • main () • { • int i ; • printf (“\nEnter a positive integer: “); • scanf (“%d”, &i); • while ( i < 0 ) • { • printf (“Oops! Enter a positive integer: ”); • scanf (“%d”, &i); • } • printf (“\n”); • } Declare and assign variable i (used to store selection, NOT used as a counter!) Prompt & scan value of i Notice NO semicolon “;” when performing pretest and notice braces (code block) Perform action if condition is TRUE (error-checking procedure). User is informed of mistake and must re-enter until a positive integer is finally entered
while loop - Example 2 • main () • { • int i ; • printf (“\nEnter a positive integer: “); • scanf (“%d”, &i); • while ( i < 0 ) • { • printf (“Oops! Enter a positive integer!: ”); • scanf (“%d”, &i); • } • printf (“\n”); • } It is OK to give very simple variable names like i, j, x or y when using them for looping purposes
In Class Exercise #2 • Can you think of how both programs above could be modified to create “infinite loops?”
while loop - Example 1 • main () • { • int i = 0; • while ( i != 3 ) • { • printf (“i is: %d\n”, i); • i = i + 2; • } • } • OUTPUT: • i is: 0 • i is: 2 • i is: 4 6 8 10 12, etc. etc. etc ---> forever! Test condition will only be FALSE (thus stop the loop) if i is not equal to 3, but counter can skip 3 and go beyond 3! So you have an infinate loop!
while loop - Example 2 • main () • { • int i ; • printf (“\nEnter a positive integer: “); • scanf (“%d”, &i); • while ( i < 0 ) • { • printf (“Oops! Enter a positive integer: ”); • } • printf (“\n”); • } WARNING: If you do NOT include an opportunity to allow user to re-enter again, you will encounter an “infinite loop”!!
do/while loop • The do statement performs an action first, and then is followed by the while statement to test condition (referred to as a “post test”) • Therefore, action is performed at least once, even if condition is FALSE Commands true Testcondition false
do/while loop - Example 1 • main () • { • int i = 10; • do • { • printf (“i is: %d\n”, i); • i--; • } while (i > 0); • } Declare and assign variable i (counter) Perform action first: Prompt & scan value of i After action is performed once, then perform post test) Notice while condition immediately after closing brace for do statement, and semi-colon “;” after while statement!
do/while loop - Example 2 • main () • { • int num; • do • { • printf (“\nGuess the secret number: ”, num); • scanf (“%d”, &num); • } while (num != 5); • printf (“That’s right!\n”); • printf (“The secret number is 5.\n\n”); • } Declare variable num Prompt user for number, and then test and repeat loop whilenum is not equal to 5 When post test is false (i.e. num is equal to 5, program continues, and prints the message “That’s right! The secret number is 5.”
Homework Question • Create a program to prompt the user for a group of integers. If the user enters a negative integer, then the program should display the average of the entered integers. • What loop should you use? • Plan the program • Create & compile the source code