© A+ Computer Science - www.apluscompsci.com What is a LOOP? © A+ Computer Science - www.apluscompsci.com.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
Java Programming: From the Ground Up
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Fundamental Programming Fundamental Programming More on Repetition.
© A+ Computer Science - Until I can hear the song Make it louder Loops repeat as long as something is true.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Learning Javascript From Mr Saem
Lecture 4b Repeating With Loops
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Loop Structures.
JavaScript: Control Statements.
Lecture 07 More Repetition Richard Gesick.
Computer Science 101 While Statement.
A+ Computer Science AP Review 2018 AP CS A EXAM
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.
Chapter 6 The while Statement
Loops The loop blocks you've used in Scratch can all be recreated in C! Loops allow for the repetition of code until a condition is met. There are three.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Looping and Repetition
Outline Altering flow of control Boolean expressions
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Loops CIS 40 – Introduction to Programming in Python
3 Control Statements:.
LOOPS BY: LAUREN & ROMEO.
For Loops.
Chapter 6: Repetition Statements
Lab5 PROGRAMMING 1 Loop chapter4.
Computer Science Core Concepts
Algorithms computer as the tool process – algorithm
By Hector M Lugo-Cordero September 3, 2008
See requirements for practice program on next slide.
Loops.
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Loops CGS3416 Spring 2019 Lecture 7.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
A+ Computer Science AP Review 2019 AP CS A EXAM
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
REPETITION Why Repetition?
Looping and Repetition
Presentation transcript:

© A+ Computer Science - www.apluscompsci.com What is a LOOP? © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com for loops © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Loop Definition A loop is a tool used to repeat a block of code. As long as the loop condition is true, the block of code associated with the condition is executed. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com The for loop © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com For Loop Definition A for loop is a block of code associated with a condition. The block of code will run a set number of times depending on the loop condition and increment/decrement value. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com the for loop for(init value; boolean condition placed here; inc/dec) { do something 1; do something 2; } do something 1 and do something 2 will occur if the condition is true. If the condition is true, do something 1 and do something 2 will occur at least once. do something 1 and do something 2 will continue to occur as long as the loop condition is true. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com for loop start stop inc/dec for(int run=1; run<= 5; run=run+1) { out.println(run); } OUTPUT1 2 3 4 5 The for loop above starts run at 1. As long as run is less than or equal to 5 ( run<=5 ), the loop will continue to run and print out the value of variable run. Run increases by one each iteration. run begins with the value 1 Iteration 1 – print run(1) run = 1 + 1 Iteration 2 – print run(2) run = 2 + 1 Iteration 3 – print run(3) run = 3 + 1 Iteration 4 – print run(4) run = 4 + 1 Iteration 5 – print run(5) run = 5 + 1 The loop condition fails when run reaches the value 6 as 6 is not less than or equal to 5. You have to tell the loop where to start, when to stop, and how much to change run. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com for loop start stop inc for (int run=1; run<=6; run=run+1) { out.println(run); } OUTPUT1 2 3 4 5 6 This loop starts run at 1 and increments run by one each iteration. The loop will continue to run as long as run is less than or equal to 6. The loop will stop when the condition run<=6 fails. run begins with the value 1 Iteration 1 – print run(1) run = 1 + 1 Iteration 2 – print run(2) run = 2 + 1 Iteration 3 – print run(3) run = 3 + 1 Iteration 4 – print run(4) run = 4 + 1 Iteration 5 – print run(5) run = 5 + 1 Iteration 6 – print run(6) run = 6 + 1 The loop condition fails when run reaches the value 7 as 7 is not less than or equal to 6. How many times does this loop run? © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com for loop 2 for(int run=1; run<7; run=run+2) { out.println("loop"); out.println(run); } OUTPUT loop 1 loop 3 loop 5 This loop starts run at 1 and increments run by two each iteration. The loop will continue to run as long as run is less than 7. The loop will stop when the condition run<7 fails. The condition will fail when run equals 7. run begins with the value 1 Iteration 1 – print run(1) run = 1 + 2 Iteration 2 – print run(3) run = 3 + 2 Iteration 3 – print run(5) run = 5 + 2 The loop condition fails when run reaches the value 7 as 7 is not less than 7. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com for loop 3 for(int run=7; run>2; run=run-2) { out.println("loop"); out.println(run); } OUTPUT loop 7 loop 5 loop 3 This loop starts run at 7 and decrements run by two each iteration. The loop will continue to run as long as run is greater than 2. The loop will stop when the condition run>2 fails. The condition will fail when run equals 1. run begins with the value 7 Iteration 1 – print run(7) run = 7 - 2 Iteration 2 – print run(5) run = 5 - 2 Iteration 3 – print run(3) run = 3 - 2 The loop condition fails when run reaches the value 1 as 1 is not greater than 2. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com sum / total int total = 0; for(int run=1; run<6; run++) { total=total+run; } out.println(total); total starts at zero. For each iteration of the for loop, the current value of run is added to total. Runs values would be 1,2,3,4,5,6 The loop fails when run reaches 6. Iteration 1 – total = 1 + 1 total is 1 Iteration 2 – total = 2 + 2 total is 3 Iteration 3 – total = 3 + 3 total is 6 Iteration 4 – total = 6 + 4 total is 10 Iteration 5 – total = 10 + 5 total is 15 The loop condition fails when run reaches the value 6 as 6 is not less than 6. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com sum / total int total=0; for(int x=1; x<6; x++) { total=total+x; } out.println(total); TRACE x total output 1 1 2 3 3 6 4 10 5 15 6 15 total starts at zero. For each iteration of the for loop, the current value of run is added to total. Runs values would be 1,2,3,4,5,6 The loop fails when run reaches 6. Iteration 1 – total = 1 + 1 total is 1 Iteration 2 – total = 2 + 2 total is 3 Iteration 3 – total = 3 + 3 total is 6 Iteration 4 – total = 6 + 4 total is 10 Iteration 5 – total = 10 + 5 total is 15 The loop condition fails when run reaches the value 6 as 6 is not less than 6. OUTPUT 15 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Strings and loops String s = "compsci"; for(int i=0; i<s.length(); i++) { out.println(s.charAt(i)); } OUTPUT c o m p s c i The for loop starts i at 0. As long as i is less than s.length(), the loop continues to run. For each iteration through the loop, i is incremented by 1. The loop will print out each character in the String starting at 0 and going up through the String to s.length(). © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com common errors for(int run=0; run>5; run++) { //do something } The loop condition should always agree with the loops starting value. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com common errors For(int run=0; run<5; run++) { } for(int run=0; run<5; run++); For is always all lowercase. Never capitalize the f on for. NEVER put a semi-colon before an OPEN BRACE. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com The while loop © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com While Loop Definition A while loop is a block of code associated with a condition. As long as the condition is true, the loop will continue to run the block of code. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com the while loop while( boolean condition placed here ) { do something 1; do something 2; } As long as the condition is true, do something 1 and do something 2 will occur. If the condition is false, do something 1 and do something 2 do not occur. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com while loop checks condition first int run = 0; //0 – start while(run<5) //1 - stop { run = run + 1; //2 - increment out.println(run); //3 - code } As long as run is less than 5( run<5 ), the loop will iterate. For each iteration, run is increased by 1 and run is displayed. run begins with the value 0 Iteration 1 – run = 0 + 1 print(1) Iteration 2 – run = 1 + 1 print(2) Iteration 3 – run = 2 + 1 print(3) Iteration 4 – run = 3 + 1 print(4) Iteration 5 – run = 4 + 1 print(5) The loop condition fails when run reaches the value 5 as 5 is not less than 5. OUTPUT 1 2 3 4 5 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com while loop int run = 7; //0 - start while(run<10) //1 - stop { out.println(run); //2- code run++; //3 - increment } As long as run is less than 10 ( run<10 ), the loop iterates. For each iteration, run is displayed and then increased by 1. The loop condition fails when run reaches the value 10 as 10 is not less than 10. run begins with the value 7 Iteration 1 – print(7) run = 7 + 1 Iteration 2 – print(8) run = 8 + 1 Iteration 3 – print(9) run = 9 + 1 OUTPUT7 8 9 What is the final value of run? © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com while loop 2 OUTPUT 25 loop 20 loop 15 loop 10 loop int run=25; while(run>=10) { out.println(run); out.println("loop"); run=run-5; } As long as run is less than or equal to 10 ( run<=10 ), the loop will iterate. For each iteration, run is displayed, loop is displayed, and run is decreased by 5. run begins with the value 25 Iteration 1 – print(25) print(loop) run = 25-5 Iteration 2 – print(20) print(loop) run = 20-5 Iteration 3 – print(15) print(loop) run = 15-5 Iteration 4 – print(10) print(loop) run = 10-5 The loop condition fails when run reaches the value 5 as 5 is not greater than or equal to 10. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com while loop 3 OUTPUT 10 loop 15 loop 20 loop 25 loop int run=10; while(run<=25) { out.println(run); out.println("loop"); run=run+5; } As long as run is less than or equal to 25 ( run<=25 ), the loop will iterate. For each iteration, run is displayed, loop is displayed, and run is increased by 5. run begins with the value 10 Iteration 1 – print(10) print(loop) run = 10+5 Iteration 2 – print(15) print(loop) run = 15+5 Iteration 3 – print(20) print(loop) run = 20+5 Iteration 4 – print(25) print(loop) run = 25+5 The loop condition fails when run reaches the value 30 as 30 is not less than or equal to 25. What is the final value of run? © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com tracing a while loop int total=0,x=1; while(x<6) { total=total+x; x++; } out.println(total); TRACE x total output 1 1 2 3 3 6 4 10 5 15 6 15 As long as x is less than 6 ( x<6 ), the loop will iterate. For each iteration, total is increased by the value of x and x is increased by 1. total begins with the value 0 and x begins with the value of 1 Iteration 1 – total = 0 + 1 x=1+1 Iteration 2 – total = 1 + 2 x=2+1 Iteration 3 – total = 3 + 3 x=3+1 Iteration 4 – total = 6 + 4 x=4+1 Iteration 5 – total = 10 + 5 x=5+1 The loop condition fails when x reaches the value 6 as 6 is not less than 6. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Accessing Digits How would you take apart the number 9154 digit by digit? You would need a loop. In this example, the loop iterates as long as number is larger than 0. Each time the loop iterates, the right most digit of the number is printed. % 10 is used to access the right most digit. The number is also reduced by removing the right digit using / 10 and assigning that value back to number. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Accessing Digits OUTPUT4 5 1 9 int number = 9154; while( number > 0 ) { out.println( number % 10 ); number = number / 10; } In this example, the loop iterates as long as number is larger than 0. Each time the loop iterates, the right most digit of the number is printed. % 10 is used to access the right most digit. The number is also reduced by removing the right digit using / 10 and assigning that value back to number. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Summing Digits set total to 0 while num is greater than 0 add right most digit to total remove right most digit print out the total © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Common Errors © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com common errors int run=0; while(run<5) { out.println(run); < blank 1 > } This loop will run forever as there is no code to change the value of variable run. run begins with the value of 0 and run never changes. 0 is less than 5; thus the loop will never terminate as the condition will never fail. © A+ Computer Science - www.apluscompsci.com