While Loops. Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right.

Slides:



Advertisements
Similar presentations
1a)I can identify the hypothesis and the conclusion of a conditional 1b)I can determine if a conditional is true or false 1c)I can write the converse of.
Advertisements

While loops.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
James Tam Repetition In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
Chapter 61 Flags A flag is a variable that keeps track of whether a certain situation has occurred. The data type most suited to flags is Boolean.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Doubles Math Facts! Doubles Math Facts!
MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal.
This will let you see the game you are going to play on the inside. It will help you see what all goes into creating instance to that goes into setting.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
CSC 204 Programming I Loop I The while statement.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Implementation of the Hangman Game in C++
30/10/ Iteration Loops Do While (condition is true) … Loop.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
 There are times when you will want blocks to repeat. Instead of duplicating blocks and ending up with a long script that might be confusing, there.
Simple Quiz Assessment David Yan Under the direction of Susan Rodger Duke University June 2015.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris
Algorithms Writing instructions in the order they should execute.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Copyright © Curt Hill Loop Types and Construction Logical types and construction hints.
Random UNPREDICTABLE NUMBERS. A FEW APPLICATIONS THAT USE RANDOM…. Predictions for life expectance used in insurance Business simulations Games (Give.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Conditionals.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Formatting Output.
Repetition-Sentinel,Flag Loop/Do_While
Iterations Programming Condition Controlled Loops (WHILE Loop)
Logical Operators and While Loops
Writing Functions( ) (Part 5)
Introduction to Programming
Building Java Programs
Introduction to pseudocode
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Introduction to Primitive Data types
Do While (condition is true) … Loop
IST256 : Applications Programming for Information Systems
Design and Implementation
Module 4 Loops.
Do … Loop Until (condition is true)
3.1 Iteration Loops For … To … Next 18/01/2019.
Loop Strategies Repetition Playbook.
Conditional and iterative statements
Console.WriteLine(“Good luck!”);
ECS15 while.
Logical Operators and While Loops
How can you make a guessing game?
Another Example Problem
True / False Variables.
True or False True or False
Introduction to Primitive Data types
Presentation transcript:

While Loops

Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right

A while loop actually requires: ● A sentry variable ● An initial value for the sentry ● A condition that can be triggered to end the loop ● A statement inside the loop that somehow changes the sentry

Math Algorithm New program math game new integer correct = 5 new integer guess = 0 while (guess != correct) guess = input(“what’s 2 + 3?”) if (guess == correct){ output (“great!”) else output (“try again...”) end if end while end program

A while loop officially requires: ● only A condition

A while loop actually requires: ● A sentry variable ● An initial value for the sentry ● A condition that can be triggered to end the loop ● A statement inside the loop that somehow changes the sentry

Bad Loop new program badLoop new variable lap = 0 while lap <= 10 laps++ end while end program

Another Bad Loop new program badLoop new variable lap while lap != 10 lap = lap + 3 end while end program

Yet another bad Loop new program badLoop new variable lap = 0 while lap <= 0 lap++ end while end program

Challenge ● Change the math program so it gives no more than three chances ● Hint: still uses a while loop ● Hint: count number of turns ● Hint: add a special kind of sentry

Three Tries Algorithm New program three tries new integer correct = 5 new integer guess = 0 new integer turns = 0 new boolean keepGoing = true while (keepGoing == true) guess = input(“what’s 2 + 3?”) if (guess == correct) output (“great!”) keepGoing = false else output (“try again...”) end if turns++ if (turns >= 3){ output “too many turns” keepGoing = false end if end while end program

Tricks of the Three Tries Use a boolean (true/false) sentry keepGoing indicates whether loop should continue Anything that causes loop to end changes keepGoing