Download presentation
Presentation is loading. Please wait.
Published byJonas Shields Modified over 8 years ago
1
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney
2
The Loop Structure A loop is used to repeat a set of code A loop is used to repeat a set of code It can have a specific number of times set by the programmer It can have a specific number of times set by the programmer It can have varying set of times entered by the user It can have varying set of times entered by the user It can be logic based and ends when a condition is met It can be logic based and ends when a condition is met
3
Loop Structure Types In java there are many ways to loop In java there are many ways to loop While While Do {code} While Do {code} While For (int x = 1; x < 10; x++) For (int x = 1; x < 10; x++)
4
Typical Loop Functions Incrementing (counting) – counts how many times something is done, for example calculating an average, or determining how many times a person had a turn. Incrementing (counting) – counts how many times something is done, for example calculating an average, or determining how many times a person had a turn. Counter = Counter + 1 Counter = Counter + 1 Accumulating – this can keep a running total, for example tally a score in a game, sum a group of numbers entered by the user Accumulating – this can keep a running total, for example tally a score in a game, sum a group of numbers entered by the user Sum = Sum + Sum = Sum +
5
While /Do While loops Use boolean logic to determine when the loop should end Use boolean logic to determine when the loop should end EX. while (age !=0) EX. while (age !=0) This will continue to run until the user enters the age as 0 This will continue to run until the user enters the age as 0
6
For loops Runs a predetermined amount of times. Runs a predetermined amount of times. Can be a number of times set by programmer Can be a number of times set by programmer Can be a number of time entered by the user Can be a number of time entered by the user Can be a variable that is generated by user or randomly Can be a variable that is generated by user or randomly
7
While Loop Algorithm Sample algorithm using while loop Sample algorithm using while loop Start Start Enter age Enter age While age <> 0 While age <> 0 Sum = Sum + age Sum = Sum + age Enter age Enter age Print Sum Print Sum End End
8
For Loop Algorithm Sample algorithm using For loop Sample algorithm using For loop Start Start Enter age Enter age Loop: x = 1 to 12 Loop: x = 1 to 12 Sum = Sum + age Sum = Sum + age Enter age Enter age Print Sum Print Sum End End
9
While Loop Flowchart Symbol T F While
10
For Loop Flowchart Symbol Loop Var BeginEnd Step
11
Nested Loops Loops can contain nested loops Loops can contain nested loops The nested loop will repeat until it is complete The nested loop will repeat until it is complete The main loop will then repeat again The main loop will then repeat again This process continues until the entire main loop is completed This process continues until the entire main loop is completed During each loop of the main loop the nested loop runs its complete number of loops During each loop of the main loop the nested loop runs its complete number of loops
12
Sample Problem – Lesson 6 Write a solution complete with all organizational tools to solve the following problem: Write a solution complete with all organizational tools to solve the following problem: Enter several grades and then -1 to calculate the average of the numbers entered (-1 is not calculated in the average) Enter several grades and then -1 to calculate the average of the numbers entered (-1 is not calculated in the average) See sample problem sheet See sample problem sheet See sample problem sheet See sample problem sheet
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.