Loops For, While, and Do While. Loop structure Loops need a control variable to operate correctly. This variable must be declared, initialized, updated,

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.
Repeating Structures Do While Loops. Do While Loop While loops have a test condition before the loop –This means that java will test the condition before.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
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.
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
Copyright © Texas Education Agency, Computer Programming For Loops.
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”);
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.
CPS120 Introduction to Computer Science Iteration (Looping)
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
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,
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
ALBERT WAVERING BOBBY SENG. Week 4: JavaScript  Quiz  Announcements/questions.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
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.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
PHY 107 – Programming For Science. Today’s Goal  How to use while & do / while loops in code  Know & explain when use of either loop preferable  Understand.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
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.
Review 1.
Control Structures.
Loop Structures.
Lecture 07 More Repetition Richard Gesick.
Loops October 10, 2017.
Using variables, for..loop and functions to help organize your code
LRobot Game.
Basketball Drill Programming Alternatives
More Loops.
Module 4 Loops.
Control structures to direct program flow
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
By Hector M Lugo-Cordero September 3, 2008
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
CHAPTER 21 LOOPS 1.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Building Java Programs
design OO words Debug Variables Data types
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Loops For, While, and Do While

Loop structure Loops need a control variable to operate correctly. This variable must be declared, initialized, updated, and verified. Without this control variable, the loop will never end. This will cause an error.

For Loops The for loop is structured so that the header forces you to take care of all of the loop’s needs. There are three statements in the header: the declaration/initialization, the verification, and the update(tion). for(int var = startVal; var(, =,==,or !=)endVal; ) Notice that the loop control variable must be an int and that the first two statements have semicolons but the last does not. For loops are ideal when you know how want times you want the loop to run.

For Loops cont. What would be the result of the following? for(int i = 0; i < 5; i++) {System.out.print( “Hi”); } And the result of: for(int i = 1; i <= 5; i++) {System.out.print(2 * i ); }

While loops A while loop is structured with only a verification(condition) statement in the header. while(var < 5 ) {code} for example Since you are using the loop control variable, it must be declared and initialized before the loop begins! Since there is no update statement in the header, the update must be coded inside the loop. If the condition is initially false, no statements inside the loop will be executed.

While cont. What would be the result of the following? int x = 5; while(x > 1) {System.out.print( “Hi”); x--; } And the result of: int x = 3; while(x > 0) {System.out.print( 2 / x); x--; }

Do while loops A do while loop is almost exactly the same as a while loop. The structure is: do {code} while(condition); Notice that since the verification takes place at the end, the loop will always execute at least once!

Do While cont. What would be the result of the following? int x = 1; do{System.out.print( “Hi”); x++; } while(x < 5); And the result of: int x = 10; do{System.out.print( 2 * x); x++; } while(x < 5);

For each intro This is a short-cut loop that will come in handy later. For now you may use for Strings. for(type var: target) –Type is the type of data each element in target is –Var is your variable name –Target is the item to be looped over

For Each Example String word = “Walsh”; for(char letter: word) { System.out.println(letter); } This would do the same as: –for(int a=0; a < word.length(); a++) { System.out.println(word.charAt(a)); }

Loops For, While, and Do While