Chapter 5 Repetition.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
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:
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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.
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
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
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.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Repetition Statements while and do while loops
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
1 The for loop. 2 Repetition with for loops So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so.
ADMIT TICKET WHAT DOES THIS OUTPUT? double y = 2.5; int x = 6 / (int) y; System.out.println(“x = “ + x);
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
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:
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
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:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
For Loop Tips And Tricks
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Chapter 9 Control Structures.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
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.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
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.
Chapter 9 Repetition.
Lecture 4b Repeating With Loops
Exam 2 Review.
Chapter 4 Repetition Statements (loops)
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Decision statements. - They can use logic to arrive at desired results
For Loops October 12, 2017.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
Chapter 9 Control Structures.
LOOPS BY: LAUREN & ROMEO.
Control Statements Loops.
Chapter 8: More on the Repetition Structure
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
Building Java Programs
Repetition Statements (Loops) - 2
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Presentation transcript:

Chapter 5 Repetition

Objectives Code programs using the following looping techniques: While Loop Do While Loop For Loop Explain when the Break statements would be used when coding loops. Walk through While, Do While, and For loops documenting variables as they change.

while loop While Syntax: Flowchart: while (boolean condition) {         repeated statements   }

while loop example: While : Output: int counter = 1;  while (counter <=5)  {        System.out.println ("Hello " + counter);        counter ++; } Hello 1 Hello 2 Hello 3 Hello 4 Hello 5

do while loop Do While Syntax: Flowchart: do {          repeated statements   } while (boolean condition);

do while loop example: Do While : Output: Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 int counter = 1;  do { System.out.println("Hello " + counter);  counter++;  }  while (counter <=5);

for loop Initialization - initializes the start of the loop. for (initialization;   test;    increment)  {            statements; } Initialization - initializes the start of the loop. Example: int i = 0; Boolean Test - occurs before each pass of the loop. Example: i<10; Increment - any expression which is updated at the end of each trip through the loop. Example: i ++,   j+=3 Statements are executed each time the loop is executed.

for loop logic

for loop example 1: for loop Results for (int counter =1;  counter <=5;  counter++ ) { System.out.println ("Hello  " + counter); } Hello 1 Hello 2 Hello 3 Hello 4 Hello 5

for loop example 2: for loop with modulus results for (int number = 0;   number <1000;   number++) {           if ( number  %  12  = =  0 )  {                    System.out.println("Number is " + number);  } } ..... etc. Number is 996

for loop example 3: for loop to print even numbers from 1 to 20 results   for (int number = 2;   number <=20;   number+=2) {          System.out.println("Number is " + number);  } The number is 2 The number is 4 The number is 6 The number is 8 The number is 10 The number is 12 The number is 14 The number is 16 The number is 18 The number is 20

for loop example 4: for loop with multiple initialization & increments results   for (int i=0,  j=0 ;  i*j < 1000;  i++,  j+=2) {       System.out.println( "The answer is " + i + " * " + j + " = " + i*j );  }

Nested Loops

Break Statement for (int index =1; index<= 1000; index ++) { if (index   ==   400) { break; }   System.out.println("The index is " + index);  }