1 / 43

Loops Main Menu

Loops Main Menu. Learning Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . page 2 Overview of Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . page 3

auryon
Download Presentation

Loops Main Menu

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. Loops Main Menu Learning Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .page 2 Overview of Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .page 3 The While Loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . page 5 The Do While Loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .page 7 The For Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . page 11

  2. Loops Learning Objectives Upon completion of this lesson, the student will be able to: • Identify the three terms that refer to the loop structure. • Name the elements in a loop structure and describe their function. • Discuss why Boolean logic is used to evaluate the loop control variable. • Interpret Pseudocode and flow charts of the loop control structure.

  3. Loops Overview of Loops The loop (also know as the repetition and iteration) is a very useful tool when each element in a group of data need to be processed using the same set of instructions. Loop structures allow for repeated execution of block of statements called the loop body. Loop body : The loops body contains the statements that will be executed inside the loop as long as the loop control variable evaluates to true. All loop structures must have the following elements in order for the loop to execute properly. • Initialize the loop control variable. • Evaluate the Boolean condition of the loop control variable. • Update the loop control variable. • Exit the loop structure when the loop control variable evaluates to false. Loop control variable : A loop control variable is a variable that contains a value that is elevated to Boolean condition statement to determine whether the loop body will execute. Condition : Condition statements are expressions with rational and logical operators used in decision and repetition structures to control the flow of instruction processing.

  4. Loops Overview of Loops Consider the loops structure that will display the even numbers from 1 to 10. Using the elements listed above, the program must first initialize the loop control variable to a starting value. Initialize : start evenNum = 0 Next, the program must evaluate the condition of the loop control variable. Evaluate : While evenNum 10 < = Since even Num start with the value of 0, and 0 is less than or equal to 10 (True), the program can now execute the loop body and update the loop control variable. Execute : display evenNum Update : add 2 to evenNum When the value of even Num reaches 11 even Num less than or equal to 10 is false, and the program will exit the loop. Exit : End while end See entire pseudocode

  5. Loops Overview of Loops All loops structures have the following four elements. • The loop control variable. • The loop entry. • The loop body. • The loop exit. See flow chart for example with Elements. 

  6. Loops Overview of Loops The While Loop Most computer programming languages support three loop control structures: the while loop, the do while loop, and the for loop. The while loop first initializes the loop control variable to a starting value. Next, the condition statement is evaluated to determine if the condition is either True or False. As long as the condition statement evaluates to true, the instructions inside the loop body are executed and the loop control structure is updated. This process continues until the condition statement evaluates to false. When the condition statement evaluates to false, the program exits the while loop structure. Example of while loop in java code:

  7. Loops Overview of Loops The Do While Loop The do while loop first initializes the loop control variable to a starting value. Next, the instructions inside the loop are executed and the loop control variable is updated. Then, the condition statement is evaluated to determine if the condition is either True or False. As long as the condition statement evaluates to True, the process of executing the instructions inside the loop body and updating the loop control variable continue until the condition statement evaluates to false, the program exits the do while loop structure. The main difference between the while loop and the do while loop is that the while loop evaluates the condition statement of the control variable before executing the instructions in the loop body, and the do while loop executes the instruction once and then evaluates the condition statement after the loop body executes once. Example do while loop in java code:

  8. Loops Overview of Loops The For Loop The for loop initializes the loop control variable to a starting value, evaluates the condition statement, and updates the loop control variable in one single expression. When the expression is evaluated, The loop control variable is initialized. Next, the condition statement is evaluated to determine if the condition is either true or False. If true, the instructions inside the loop body are executed, then the loop control variable is updated. As long as the condition statement evaluates to true, the process of executing the instructions inside the loop body and updating the control variable in the expression continue until the condition statement evaluates to False. When the condition statement evaluates to false, the program exits the for loop structure. Example for loop in java code :

  9. Loops Overview of Loops The main difference between the while loop and the do while loop, and the for loop is that while loop evaluates the condition statement of the control variable before executing the instructions in the loop body, the do while loop executes the instructions and then evaluates the condition statement after the loop body executes once, evaluates the condition statement repeatedly until False is evaluated, and updates the loop control variable all in the same expression. The loop body in a for loops does not include updating the control variable, because that happens in the for loop expression.

  10. Loops Quick Quiz

  11. TRY AGAIN!

  12. CORRECT!

  13. Loops Quick Quiz

  14. TRY AGAIN!

  15. CORRECT!

  16. Loops Quick Quiz

  17. TRY AGAIN!

  18. CORRECT!

  19. Loops Quick Quiz

  20. TRY AGAIN!

  21. CORRECT!

  22. Loops Quick Quiz

  23. TRY AGAIN!

  24. CORRECT!

  25. Loops Quick Quiz

  26. TRY AGAIN!

  27. CORRECT!

  28. Loops Quick Quiz

  29. TRY AGAIN!

  30. CORRECT!

  31. Loops Quick Quiz

  32. TRY AGAIN!

  33. CORRECT!

  34. Loops Quick Quiz

  35. TRY AGAIN!

  36. CORRECT!

  37. Loops Quick Quiz

  38. TRY AGAIN!

  39. CORRECT!

  40. Loops Quick Quiz

  41. TRY AGAIN!

  42. CORRECT!

  43. Loops Overview of Loops Congratulations! You have completed the Loops lesson.

More Related