Repetition Statements while and do while loops

Slides:



Advertisements
Similar presentations
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
1 Repetition structures Overview while statement for statement do while statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Copyright © Texas Education Agency, Computer Programming For Loops.
1 Repetition structures Overview while statement for statement do while statement.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
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
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
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.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
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.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
Chapter 5: Control Structures II
Control Structures.
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Loop Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Control Structures II
Chapter 5 Repetition.
Outline Altering flow of control Boolean expressions
The ‘while’ Statement September 27, 2006
Control Statements Loops.
Repetition Control Structure
3.5- The while Statement The while statement has the following syntax:
Chapter 2.2 Control Structures (Iteration)
Repetition Statements (Loops) - 2
Control Statements Loops.
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Outline Boolean Expressions The if Statement Comparing Data
Presentation transcript:

Repetition Statements while and do while loops Iterations Repetition Statements while and do while loops

Outline while statements Example of while statement Other repetition statements do loop for loop

Online material Chapter one of the book is available online under the following link: http://introcs.cs.princeton.edu/java/home/chapter1.pdf Please read pages 46 – 83. The exercises in this book are bit challenging. Do as much as you can.

Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements, they are controlled by boolean expressions Java has three kinds of repetition statements: the while loop the do loop the for loop

The while Statement A while statement has the following syntax: while(condition) { statement; } Body If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false

Logic of a while Loop condition evaluated false statement true

The while Statement An example of a while statement: int count = 1; while (count <= 5) { System.out.println(count); count= count +1; } Body If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times

The while Statement Let's look at some examples of loop processing A loop can be used to maintain a running sum A sentinel value is a special input value that represents the end of input A loop can also be used for input validation, making a program more robust

Example1 What is the output of the program? Prints hello 10 times // hello.java public class Hello1 { public static void main(String[] arguments) System.out.println(“hello”); System.out.println(“hello”) ; } //end of program Prints hello 10 times

Example2 What is the output of the program? Prints hello 10 times // hello.java public class Hello2 { public static void main(String[] arguments) int i =1; while(i<=10) System.out.println(“hello”); } //end of program Prints hello 10 times

Example3 What is the output of the program? // hello3.java public class Hello2 { public static void main(String[] arguments) int x = Integer.parseInt(args[0]); int i =1; while(i<=x) System.out.println(“hello”); } //end of program

Example4 What is the output of the program? // hello4.java Import java.util.Random public class Hello3 { public static void main(String[] arguments) Random generator = new Random(); while(i<=10) { int x = generator.nextInt(100); System.out.println(x); } //end of program

Example5 What is the output of the program? // hello5.java public class Hello2 { public static void main(String[] arguments) int x = Integer.parseInt(args[0]); int i =1; while(i<=x) System.out.println(“hello”); } //end of program

Example6 What is the output of the program? // sum1.java public class sum1 { public static void main(String[] arguments) int i =1; int sum =0; while(i<=100) sum = sum+i ; } System.out.println(sum); //end of program

Example7 What is the output of the program? // factorial.java public class sum1 { public static void main(String[] arguments) int i =1; int factorial =1; while(i<=100) fact= fact*i ; } System.out.println(factorial); //end of program

Infinite Loops To exit the loop The body of a while loop has to make the control condition false Otherwise, the loop will on go on for ever. This is called an infinite loop. Press Control-C to stop the execution of an infinite loop. This is a common logical error

Infinite Loops An example of an infinite loop: int i = 0; while (i<=0) { System.out.println (i); i = i - 1; } This loop will continue executing for ever. Control-C to stop the execution

Nested Loops Similar to nested if statements, loops can be nested as well The body of a loop can contain another loop.

Nested Loops What is the output of the following program? Int count1 = 1; Int count2; while (count1 <= 10) { System.out.println(“hello"); count2 = 1; while (count2 <= 20) System.out.println(“lahcen"); count2++; } count1++;

The do-while Statement A do-while statement (also called a do loop) has the following syntax: do{ statement; }while (condition ) The statement is executed once initially, and then the condition is evaluated The statement is executed repeatedly until the condition becomes false

Logic of a do-while Loop statement true condition evaluated false

The do Statement An example of a do loop: int count = 0; do{ count++; System.out.println (count); } while (count < 5); The body of a do loop will execute for 5 times

Comparing while and do statement true false condition evaluated The while Loop true condition evaluated statement false The do Loop

Summary Iterations using while (condition) {statements} do {statements} while(condition)