Computer Science 101 While Statement.

Slides:



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

Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.
CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
CS1010 Programming Methodology
Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
CS150 Introduction to Computer Science 1
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Counting Loops.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
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.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
CS1010 Programming Methodology
Introduction to Computing Science and Programming I
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
UMBC CMSC 104 – Section 01, Fall 2016
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
while Repetition Structure
Control Structures II Chapter 3
CS161 Introduction to Computer Science
Lecture 7: Repeating a Known Number of Times
Loop Structures.
CHAPTER 5A Loop Structure
Chapter 5: Control Structure
Programming Problem steps must be able to be fully & unambiguously described Problem types; Can be clearly described Cannot be clearly described (e.g.
CS1010 Programming Methodology
Lecture 07 More Repetition Richard Gesick.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
Lecture 4B More Repetition Richard Gesick
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Chapter 4 Control structures and Loops
Repetition and Loop Statements
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Outline Altering flow of control Boolean expressions
Introduction to pseudocode
Unary Operators ++ and --
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
3 Control Statements:.
Chapter 5 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 6: Repetition Statements
3.1 Iteration Loops For … To … Next 18/01/2019.
Loop Strategies Repetition Playbook.
Let’s all Repeat Together
Lecture 13 Introduction to High-Level Programming (S&G, §§7.1–7.6)
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Presentation transcript:

Computer Science 101 While Statement

The While-Statement T F Cond The syntax for the While-Statement is while <condition> : <list of statements> Note the colon and indentation

While statement example In this example, we want a program where the user inputs an upper limit. Then we want to think of adding consecutive integers 1 + 2 + 3 + … until the sum reaches or exceeds the limit. The program should print out how many terms were in the sum. For example, if the limit is 7, 1+2+3 gives 6, and 1+2+3+4 gives 10; so we should print 4.

While-Statement example limit = input (“Enter limit for sum”) total = 0 num = 1 while total < limit : total = total + num num = num + 1 print num Do example Discount now.

While-Statement example Do example Discount now.

While-Statement example Do example Discount now.

While-Statement example Do example Discount now.

Count-controlled Loops // General form <initialize a counter variable> while <test counter for termination condition> : <do something> <change the value of counter>

Count-controlled Loops // General form <initialize a counter variable> while <test counter for termination condition> : <do something> <change the value of counter> // Count up <initialize a counter variable> while <counter is less than a limit value> : <do something> <increase the value of counter> counter = 1 while counter <= 10 : <do something> counter = counter + 1

Count-controlled Loops // General form <initialize a counter variable> while <test counter for termination condition> : <do something> <change the value of counter> // Count down <initialize a counter variable> while <counter is greater than a limit value>: <do something> <decrease the value of counter> counter = 10 while (counter > 0): <do something> counter = counter - 1

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

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

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

Infinite Loop counter = 1 while (counter <= 10) : // Executes 5 passes <do something> counter = counter + 2 counter = 1 while (counter != 10) : // Runs forever <do something> counter = counter + 2; In general, avoid using != in loop termination conditions.

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

GCD The largest integer that evenly divides into two non-negative integers Example: gcd(32, 24) = 8 Algorithm discovered by Euclid was one of the first algorithms ever discovered

Flash from the Past Euclid’s Algorithm for Greatest Common Divisor Get two positive integers as input, call them I and J. Divide I by J, and call the Remainder R. If R is not 0, then reset I to the value of J, reset J to the value of R, and go back to step 2. Print out the answer, which is the value of J. Stop

Example GCD(206, 40)

Example GCD(206, 40) = GCD(40, 6)

Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4)

Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4) = GCD(4, 2)

Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4) = GCD(4, 2) = GCD(2, 0)

Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4) = GCD(4, 2) = GCD(2, 0) = 2

Pseudocode Get I, J Set R to remainder of I divided by J While R≠0 Do Set I to J Set J to R Set R to remainder of I divided by J end-of-loop Print J

Code

Some comments on programming style Use meaningful variable names. Use comments for your name, purpose of program, date. Include a pseudocode algorithm in comments, if appropriate. Use comments to mark off sections of code and to clarify “tricky” code.

Keep lookin, boys, we'll findem somewhere