Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Programming Logic and Design Eighth Edition
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
CS150 Introduction to Computer Science 1
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Pseudocode and Algorithms
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.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
5.05 Apply Looping Structures
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Fundamentals of Python: From First Programs Through Data Structures
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
1 JavaScript: Control Structures. 2 Control Structures Flowcharting JavaScript’s sequence structure.
JavaScript, Fourth Edition
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CPTG286K Programming - Perl Chapter 4: Control Structures.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Chapter 4 Introduction to Control Statements
Controlling Program Flow with Looping Structures
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
UCT Department of Computer Science Computer Science 1015F Iteration
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Chapter 4 C Program Control Part I
CS161 Introduction to Computer Science
Lecture 7: Repeating a Known Number of Times
Think What will be the output?
Computer Science 101 While Statement.
Control Structures - Repetition
Programming Fundamentals Lecture #6 Program Control
And now for something completely different . . .
Chapter 8 JavaScript: Control Statements, Part 2
Unary Operators ++ and --
Chapter (3) - Looping Questions.
Introduction to Programming
Repetition Control Structure
Introduction to Computer Science
Introduction to Programming
Presentation transcript:

Computer Science 101 While Statement

Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement is while : Note the colon and indentation Note the colon and indentation T F Cond

Example: Print first 10 positive integers

Example: Print even positives thru 20

Function rollDie

Function rollDie (cont.)

Function checkDie

JES Input Functions The Python input function works only for numerical input. The Python input function works only for numerical input. JES provides some special input functions. These functions allow us to get input for other types, via a dialog box that pops up with given prompt. JES provides some special input functions. These functions allow us to get input for other types, via a dialog box that pops up with given prompt. – requestInteger for integers – requestNumber for floats – requestString for strings

JES printNow With JES, print statements within a function do not display their output until the function has completed its execution. Sometimes this is ok, but often the user needs to see intermediate results for purposes of decision making. JES provides a function printNow that displays its output immediately. It is of the form printNow( ) Note that it only prints one thing. With JES, print statements within a function do not display their output until the function has completed its execution. Sometimes this is ok, but often the user needs to see intermediate results for purposes of decision making. JES provides a function printNow that displays its output immediately. It is of the form printNow( ) Note that it only prints one thing.

str str is a Python function that will convert a number to a string. This is often useful when we want to combine string and numerical data for output. str is a Python function that will convert a number to a string. This is often useful when we want to combine string and numerical data for output.

Recommendation Unless specified otherwise, you should use the “request” forms for input and printNow for output in your functions. Unless specified otherwise, you should use the “request” forms for input and printNow for output in your functions.

Function averageScores

More on Conditions and In Python we can combine two conditions with and to form a new condition that is true only if both or the original conditions are true. and In Python we can combine two conditions with and to form a new condition that is true only if both or the original conditions are true.

More on Conditions or In Python we can combine two conditions with or to form a new condition that is true provided at least one of the original conditions is true. or In Python we can combine two conditions with or to form a new condition that is true provided at least one of the original conditions is true.

More on Conditions not In Python we can make a new condition from a given condition by placing not in front of the original condition. The truth value of the new condition is the opposite of the original condition. not In Python we can make a new condition from a given condition by placing not in front of the original condition. The truth value of the new condition is the opposite of the original condition.

Count-controlled Loops // General form while :

Count-controlled Loops // General form while : // Count up while : counter = 1 while counter <= 10 : counter = counter + 1

Count-controlled Loops // General form while : // Count down while : counter = 10 while (counter > 0): counter = counter - 1

Increment and Decrement counter = 1 while counter <= 10 : counter += 1 counter = 10 while counter > 0 : counter -= 1

Designing Correct Loops Initialize all variables properly Initialize all variables properly –Plan how many iterations, then set the counter and the limit accordingly Check the logic of the termination condition Check the logic of the termination condition Update the loop control variable properly Update the loop control variable properly

Off-by-One Error counter = 0 while (counter < 10) : // Executes 10 passes counter = counter + 1 counter = 1 while (counter < 10) : // Executes 9 passes counter = counter + 1; counter = 1 while (counter <= 10) : // Executes 10 passes counter = counter + 1 counter = 0 while (counter <= 10) : // Executes 11 passes counter = counter + 1

Infinite Loop counter = 1 while (counter <= 10) : // Executes 5 passes counter = counter + 2 counter = 1 while (counter != 10) : // Runs forever counter = counter + 2; In general, avoid using != in loop termination conditions with count-controlled loops.

Testing Loops Can vary the limit or the control variable, or both Can vary the limit or the control variable, or both Use a negative value, zero, and a positive value Use a negative value, zero, and a positive value Display a trace if things aren’t working Display a trace if things aren’t working