Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Repeating Actions While and For Loops
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
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.
Course A201: Introduction to Programming 09/16/2010.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris
CHAPTER 3 Decisions and Repetition. A few new constants Constants so far:  Strings  Numbers  integer  float Boolean constants: [On board]
CS 100 Introduction to Computing Seminar October 7, 2015.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Random UNPREDICTABLE NUMBERS. A FEW APPLICATIONS THAT USE RANDOM…. Predictions for life expectance used in insurance Business simulations Games (Give.
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
Complex Branching Statements CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
Testing Programs with Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Structured Programming The Basics
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
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.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Iteration with While You can say that again.
IST256 : Applications Programming for Information Systems
Module 4 Loops.
Repetition Control Structure
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Alternate Version of STARTING OUT WITH C++ 4th Edition
A LESSON IN LOOPING What is a loop?
How can you make a guessing game?
Repetition Statements (Loops) - 2
Iteration – While Loops
Presentation transcript:

Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1

Guessing Game Generate random int between 1 and 10 Prompt user for guess Keep prompting as long as guess incorrect Tell user guess correct

While Loop Execute list of statements while some condition is true – Keep executing those statements until condition is false Syntax: while condition: first statement inside loop … last statement inside loop first statement after loop Statements in loop indicated with indentation Condition true Loop back to condition at end of loop Condition false

While Loop Design What is condition to be tested each time loop starts? What must be done before the while loop? – Usually something to get initial value to be tested in condition What must be done inside the while loop? – What code should be repeated as long as condition true? – Must be something that can eventually make the condition false!

Guessing Game Design What is condition to be tested each time loop starts? – User guess not correct – guess != random number generated What must be done before the while loop? – Need a value for guess and random number – Generate random number random.randint(1, 10) generates random int from 1 to 10 – Prompt user for their first guess What must be done inside the while loop? – Tell user guess incorrect – Prompt user for next guess Without this, will never exit loop!

Guessing Game Code

Guessing Game Trace Computer generates 9 as number Prompt user for first number 5 != 9 true so enter loop 1 != 9 true so enter loop again 8 != 9 true so enter loop again 2 != 9 true so enter loop again 6 != 9 true so enter loop again 9 != 9 false so exit loop

Infinite Loops What if loop never exits? Usual cause: Nothing in loop body can change condition to false Can exit program manually with Ctrl-C

Branches and Loops Can nest complex code inside a loop Example: Hints in guessing game – If guess less than number, print “too low” – If guess greater than number, print “too high” Must be placed inside loop, since done for each wrong guess Must be executed before prompting for next guess

Improved Guessing Game Code