Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control.

Slides:



Advertisements
Similar presentations
Execute Blocks of Code Multiple Times Telerik Software Academy C# Fundamentals – Part 1.
Advertisements

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.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Computer Science 1620 Loops.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Loops and Iteration for Statements, while Statements and do-while Statements.
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
CONTROLLING PROGRAM FLOW
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
CS101 Computer Programming I Chapter 4 Extra Examples.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Application development with Java Lecture 6 Rina Zviel-Girshin.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
UCT Department of Computer Science Computer Science 1015F Iteration
Identify the Appropriate Method for Handling Repetition
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Control Structures.
Loop Structures.
Programming Fundamentals
Selected Topics From Chapter 6 Iteration
LOOPS.
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
LOOPS BY: LAUREN & ROMEO.
Control structures to direct program flow
Loop Strategies Repetition Playbook.
February , 2009 CSE 113 B.
PROGRAM FLOWCHART Iteration Statements.
Looping Structures.
Presentation transcript:

Loops

More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control statement

While Loop while(BOOLEAN){ ACTION } Keep doing actions while the boolean evaluates to true.

Example  Calculate the sum of the first 100 integers.  I don’t want to write out … int sum = 0; int number = 1; while(number<=100){ sum = sum + number; number = number +1; }

How it works  Boolean is the “Gatekeeper” – lets you into the block of code if the boolean is true.  Elevator always bring you back up to the gate keeper at the end of the block.

Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : 0 number : 1 1<=3? True!

Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : 0 1 number : 1 2

Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : 0 1 number : 1 2 2<=3? True!

Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : number : 1 2 3

Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : number : <=3? True!

Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : number :

Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : number : <=3? False!

Practice int x = 1; int y = 3; while(x 0){ y = y+1; } x = x+2; }

Practice – trick question! int x = 1; int y = 3; while(x 0){ y = y+1; } x = x-2; }

Nested Whiles int w = -1; int x = 0; int y = 2; int z = 3; while(x 0){ w = w*z; y = y-1; } x = x+1; z = z-1; } y = z/2;

Problem Solving: Counter int counter = 1; while (counter <= 5){ System.out.println(counter); System.out.println(counter); counter ++; counter ++;}

Problem Solving: Running total int counter = 1; int total = 0; while (counter <= 5){ total = total + counter; total = total + counter; counter++; counter++;}

Practice Session 1  Write a method that takes a String and a number and prints the string to the screen that many times.  Take an integer and compute how many times you can divide it by 2 before you get 1.

Problem Solving: Make a chart Timmy Turtle is crawling to a wall. He starts out crawling 2 inches every minute. But, he is getting tired. And every minute he crawls half as fast. How far has he crawled in four minutes?

Making a Chart  What you would do on paper: MinuteTotalCrawl Rate 00 in2 in 12 in1 in 23 in.5 in 33.5 in.25 in in.125 in

Making a Chart MinuteTotalCrawl Rate 00 in2 in 12 in1 in 23 in.5 in 33.5 in.25 in in.125 in The columns are your variables, and the first row is their initial value int minute = 0; double total = 0; double crawlRate = 2;

Making a Chart MinuteTotalCrawl Rate 00 in2 in 12 in1 in 23 in.5 in 33.5 in.25 in in.125 in How do you know when to keep going? int minute = 0; double total = 0; double crawlRate = 2; while (minute < 4){

Making a Chart MinuteTotalCrawl Rate 00 in2 in 12 in1 in 23 in.5 in 33.5 in.25 in in.125 in How do you get from one row to the next? int minute = 0; double total = 0; double crawlRate = 2; while (minute < 4){ minute++; total = total + crawlRate; crawlRate = crawlRate / 2;

Making a Chart MinuteTotalCrawl Rate 00 in2 in 12 in1 in 23 in.5 in 33.5 in.25 in in.125 in What’s the answer? int minute = 0; double total = 0; double crawlRate = 2; while (minute < 4){ minute++; total = total + crawlRate; crawlRate = crawlRate / 2; } return total;

Practice Session 2  The turtle takes go-go juice! Each minute he crawls twice as fast as the last minute. Given his start speed, calculate how long it takes him to crawl 20 inches?

For loop  Does the same thing as the while loop, with different syntax.  Most useful for – do something X times.  for(INIT;BOOLEAN;UPDATE){ ACTION; }

For-while comparison  Calculate 5!  int prod = 1; int num = 1; while(num <= 5){ prod = prod*num; num++; }

For-while comparison  Calculate 5!  int prod = 1; int num = 1; while(num <= 5){ prod = prod*num; num++; } int prod = 1; for(int num = 1; num <= 5; num++){ prod = prod*num; }

Do-While Loop do{ ACTIONS; } while(BOOLEAN); Difference is when the Boolean condition is checked – before or after the loop

While comparisons  Calculate 5!  int prod = 1; int num = 1; while(num <= 5){ prod = prod*num; num++; } int prod = 1; int num =1; do{ prod = prod * num; num++; }while(num<=5);

Do-While statement  Always executes the body of the loop at least once!  Checks the boolean after the loop, not before  When would you want this?

Extras  One trip through a loop is called an iteration  Do some iterations by hand to get an idea of what’s going on  Put in print lines for yourself when debugging gets bad

Last Practice  Given a number, draw that many circles in a row to the screen.  Calculate the factorial of a number using a for loop.