LESSON 11 WHILE LOOPS UNIT 5 12419 In

  • Slides: 12
Download presentation
LESSON 11 – WHILE LOOPS UNIT 5 – 1/24/19

LESSON 11 – WHILE LOOPS UNIT 5 – 1/24/19

In this lesson, you will: Explain that a while loop continues to run while

In this lesson, you will: Explain that a while loop continues to run while a boolean condition remains true. Translate a real-life activity with repeated components into a form that could be represented by a while loop. Analyze a while loop to determine if the initial condition will be met, how many times the loop will run, and if the loop will ever terminate. Write programs that use while loops in a variety of contexts.

Vocabulary: Iterate - To repeat in order to achieve, or get closer to, a

Vocabulary: Iterate - To repeat in order to achieve, or get closer to, a desired goal. while loop - a programming construct used to repeat a set of commands (loop) as long as (while) a boolean condition is true. Counters: Variables that count how many times a while loop has run, also called iterators, can be used to control the number of times a while loop runs. Infinite Loops: The final example in this activity guide produces an “infinite loop. ” This means that the computer (or person implementing the algorithm) will cycle through a set of commands forever, because the while loop condition is always true. Infinite loops are almost always undesirable, but they can be deceptively easy to create by mistake.

while loops repeat a set of steps until a certain condition is met. Like

while loops repeat a set of steps until a certain condition is met. Like conditional statements, while loops use Boolean expressions to determine if they will run and how many times.

 The structure of a while loop by demonstrating how a conditional statement that

The structure of a while loop by demonstrating how a conditional statement that “loops” back on itself can be used to repeatedly execute a block of commands.

This conditional statement loops back on itself. As a result, the conditional might be

This conditional statement loops back on itself. As a result, the conditional might be evaluated multiple times. As a result, the same command is running multiple times.

The structure we just explored is called a “while loop. ” When you look

The structure we just explored is called a “while loop. ” When you look at the flowchart, you can see that we “loop through” a command as long as, or “while, ” a condition is true. while loops are a way we can easily represent a process that includes many repeated steps.

Distribute: Flowcharts with While Loops - Activity Guide to each student.

Distribute: Flowcharts with While Loops - Activity Guide to each student.

Errors in writing a while loop include: Writing the condition on which the while

Errors in writing a while loop include: Writing the condition on which the while loop should stop rather than continue. Not including steps in the while loop that will make the condition false at some point (i. e. , creating an infinite loop)

Go into Code Studio. Do the levels. Show me #21 – roll the dice

Go into Code Studio. Do the levels. Show me #21 – roll the dice with counter (finished project) Turn in Activity Guide: flow charts with while loops

Things to pay attention to when creating while loops. It repeats a set of

Things to pay attention to when creating while loops. It repeats a set of commands. It continues to run “while” a Boolean expression is true. It is called a loop because it “loops through” a set of commands. Visually it looks like a loop when shown in a flowchart. Things to pay attention to when programming while loops: Something inside the while loop has to update the variable in the condition so the while loop will stop while loops may never run if the condition begins as false while loops can become infinite if the condition never becomes false Off-by-one errors are common with while loops