Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.

Slides:



Advertisements
Similar presentations
How SAS implements structured programming constructs
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 Repetition structures Overview while statement for statement do while statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
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
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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,
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
CS 100 Introduction to Computing Seminar October 7, 2015.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 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 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:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
Application development with Java Lecture 6 Rina Zviel-Girshin.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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”
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
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.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Identify the Appropriate Method for Handling Repetition
Slides by Evan Gallagher
Slides by Evan Gallagher
Chapter 4 Repetition Statements (loops)
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming Loops
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Chapter 6: Repetition Statements
Control Structures Part 1
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Presentation transcript:

Overview of Java Loops By: Reid Hunter

What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition is met. For example, let's say you have the names of people in an array, and you wanted print a list of all of the names. You could setup a loop that would print the first persons name, and then move on to the next and print her name, etc (the continuing to repeat itself part.) The condition would be that it stops once all of the names have been used.

Simple Loop VS. Nested Loop A simple loop is the use of just one loop. Would be used if you only need to do something a certain number of times. A nested loop is the use of one loop inside of another loop. You would use this if you need to run something a certain number of times while running something else a certain number of times

Different Types of Java Loops “For Loops” “While Loops” “Do-While Loops”

Best Uses For Loop : When you need a simple loop to list something such as a println a certain number of times or extracting a certain group of numbers While Loop : when statements which the condition that was present on the first line is true Do-While Loop : when condition present on the last line is true

For Loops One of the easiest types of loops to establish and use in code. Usually uses a counter with a precise starting point and a precise ending point. iterate through a certain lines of code within the loop's limit as long as the loop condition is satisfied.

For Loops Cont’d Example: for(int i = 0; i <= 5; i++) // i 0 and 5 {System.out.print(i);} // prints every possible i value Output: Nested Loop Example: for (int i=1; i<=5; i++) // { System.out.println(); // moves down 1 line per number in loop for (int j=1; j<=i; j++) {System.out.print(j);} // progresses 1-5 as loop first loop goes 1 line down for ea value } Output:

While Loop Best used when statements which the condition that was present on the first line is true Slightly more complicated to use compared to for loop differs from the for loop in that it only contains a condition which gets tested at the beginning of the loop. if the condition evaluates to false, it will not be executed at all. called a pre-tested loop because the test is made before executing the sequence of statements contained within.

While Loop Examples int i = 1; while (i <= 5) { System.out.println ("count : "+ i); i++; } Output: count : 1 count : 2 count : 3 count : 4 count : 5 I int x=10; I I while (x>=1) I I { I System.out.print(x+ " "); I I x--; I I } I I Output:

Do-While Loops Most complicated out of the three types Best used when condition present on the last line is true executes the “do” part of loop at least once even before checking the condition. even if the loop condition is not satisfied the loop would execute once. useful for certain types of situations that need to run at least once.

Do-While Examples int i = 1; do { System.out.println ("count : " + i); i++; } while (i <= 5) Iint x=1; I I do { I I System.out.println(+x+" "); I Ix++; I I } I while (x<=10); I I Output:

Useful Websites for those Still Confused a/index.htmlhttp:// a/index.html trol.htm andbolts/for.htmlhttp://java.sun.com/docs/books/tutorial/java/nuts andbolts/for.html andbolts/while.html