Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

Slides:



Advertisements
Similar presentations
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Advertisements

8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
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:
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Loops and Iteration for Statements, while Statements and do-while Statements.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Chapter 4: Control Structures II
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
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.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
For Loop Tips And Tricks
Java Review if Online Time For loop Quiz on Thursday.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Computer Programming Your First Java Program: HelloWorld.java.
Lecture 4b Repeating With Loops
Introduction To Repetition The for loop
Chapter 5: Control Structures II
Repetition-Sentinel,Flag Loop/Do_While
Incrementing ITP © Ron Poet Lecture 8.
Building Java Programs
Building Java Programs
Building Java Programs
Decision statements. - They can use logic to arrive at desired results
Arrays, For loop While loop Do while loop
Fencepost loops reading: 4.1.
Building Java Programs
Control Statements Loops.
Building Java Programs
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Repetition Statements (Loops) - 2
Building Java Programs
Control Statements Loops.
Additional control structures
Just Enough Java 17-May-19.
Control Statements:.
Presentation transcript:

Loops For While Do While

Loops Used to repeat something Loop statement creates and controls the loop

while Requires a conditional test at the beginning Runs the conditional test each time the loop runs If the test is false at the beginning the loop statement will be ignored Variables are set up before the loop statement

while Example while (gameLives > 0) { //the statements inside the loop go here Loop continues until gameLives variable is no longer greater than 0

while Example int limit = 5; int count = 1; while (count < limit) { System.out.println (“Pork is not a verb”); count ++; } How many times is the line printed? 5

while int limit = 5 int count = 6 How many times will the same loop continue with these variables? 0

do-while Similar to the while loop but the conditional test is placed after the statements inside of the loop The statements between the do and while are handled automatically, the while condition is tested to determine if the loop will run again The change of condition has to take place in the do and while for the loop to end

do- while int limit = 5; int count = 1; do { System.out.println (“Pork is not a verb”); count ++; } while (count < limit);

do - while The print line will run 4 times using this example, If we change the count to an initial value of 6 it will run 1 time (different than the 0 times when just used in the while loop)

while or do-while?? Strategy 1 (do-while) Borrow the car first and tell someone later that you did Strategy 2 (while) Ask permission before you borrow the car

for The most complex of the loop statements Has 3 values set up between the ( ) Can use a variable to set the number of times that a loop will be repeated

for Example for (int p = 0; p < 500; p ++) { System.out.println (“Pork is not a verb.”); } Brackets are not necessary with a 1 line statement but it makes it easier to understand in comparison to the other loops

for for (counter, condition, change)

Exiting a loop W hen condition becomes false (all 3) To end a loop immediately without the condition becoming false use a break statement (all 3) To send a loop back to the beginning use continue (all 3)

Using loops together Example while; for; break int points = 0; int target = 100; while (target <= 100){ for (int x =0; x < target; x ++) { if (points > 50) break; points = points + x; }

Example explained break statement causes the for loop to end is the variable is greater than 50, the while loop will continue because the target is never greater than 100 To end the while loop it must be named, Put the name on the line before the beginning of the loop and follow it with a colon : use the name after break to indicate what break /continue applies to…

Example of while; for; break; name int points = 0; int target = 100; targetloop: while (target <= 100){ for (int x =0; x < target; x ++) { if (points > 50) break targetloop; points = points + x; } Now both loops will end

Example while; for; continue int points = 0; int target = 100; while (target <= 100){ for (int x =0; x < target; x ++) { if (points == 50) continue; points = points + x; }

Questions What must be used to separate each section of a for statement? A) Commas B) Semicolon C) Colon Which statement causes a program to go back to the statement that began a loop and then keep going from there? A) continue B) break C) naming

Answer to the word problem import java.util*/; class camera int x; long sensor, camera; while (sensor <=6){ for (int x = 0; x >sensor; x ++) { if (sensor < 6) System.out.println (“Calling , you have an intruder”); }

Now that you know, Go back to your class repeat program and exchange the while loop that is used with a for loop (run it, compile & turn in) Note: using a semicolon with a for loop will not necessarily cause an error but the loop will not run correctly for (int x = 0; x < 100; x ++); {

Now try this class Nines{ public static void main (String[ ] args){ for (int dex = 1; dex <= 200; dex ++) { int multiple = 9* dex; System.out.print (multiple + “ “); }