1 / 15

Understanding Loops in Programming

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.

kevinharlan
Download Presentation

Understanding Loops in Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. 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)

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

  6. 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--;

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

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

  9. In Class Exercise #2 • Can you think of how both programs above could be modified to create “infinite loops?”

  10. 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!

  11. 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”!!

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

  13. 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!

  14. 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.”

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

More Related