1 Repetition structures Overview while statement for statement do while statement.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

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.
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.
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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Looping Yong Choi School of Business CSU, Bakersfield.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
1 Selection Statements Overview l Relational and Logical Operations l Selection structures »if statement »if-else statement l Preview: More on Selection.
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.
1 Repetition structures Overview while statement for statement do while statement.
1 Repetition structures [cont’d] Overview l Nested Loops l Sentinel-Controlled loop l Avoiding Number Format exception.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
UNIT II Decision Making And Branching Decision Making And Looping
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.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested 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,
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
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.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CSC111 Quick Revision.
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Programming Fundamentals
Repetition.
Chapter 5: Control Structures II
Loop Control Structure.
Outline Altering flow of control Boolean expressions
LRobot Game.
Control Statements Loops.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
class PrintOnetoTen { public static void main(String args[]) {
while while (condition) { statements }
Control Statements Loops.
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Presentation transcript:

1 Repetition structures Overview while statement for statement do while statement

2 while statement l It is very common in programming to have a statement that needs to be executed more than once. l To avoid repeating the same statement in a program, java provide three repetition (loop) statements that may be used. These are while, for and do-while. while Statement: l The Syntax of the while statement is as follows: while (condition) statement; l Interpretation »First evaluate the condition. If it is true, the statement is executed; then the condition is evaluated again »The statement is executed over and over until the condition becomes false »If the condition is false initially, the statement is not executed even once »Therefore, we say that a while statement executes its body zero or more times

3 while statement flow diagram Flow diagram for while statement statement condition false true

4 while statement Example The following example prints a table of squares for integers from 1 to 10. class WhileExample { public static void main(String[] args) { int i; i = 0; while (i < 10) { i++; System.out.println(i+ "\t" + Math.pow(i, 2)); }

5 Important points about while statement 1. The condition of the while statement usually involves at least one variable called loop control variable. 2. The loop control variable should have a value ( initialized ) before entering the loop. 3. The loop control variable should be updated inside the body of the loop such that the condition will eventually evaluates to false, otherwise loop will be an infinite loop. public class Forever { public static void main(String[] args) { int i = 0; while (i < 10) System.out.println(”Never stop"); }

6 for statement l Because most loops involves three steps, namely, initialization, checking the condition and updating the control variable, Java provides the for statement which summarizes them on one line. The Syntax is: for (initialization; condition; updating) statements l Interpretation »First initialization statement is executed »Then the condition is evaluated »If the condition is true, –The statements inside the loop are executed –Then the updating statement is executed –Then the condition is checked again, etc. »If the condition is false, –The control will proceed to the next statement(s) after the for loop

7 For statement flow diagram l Flow diagram for ( ) {true } false Initialization Statement Updation Statement Statements inside the for loop condition

8 for statement Example The following example prints a table of squares for integers from 1 to 10 using for loop. class ForExample { public static void main(String[] args) { for (int i = 1; i <= 10; i++) System.out.println(i + "\t" + Math.pow(i,2)); }

9 do while statement syntax l Some times it is more appropriate to perform the test of a loop at the end. For this Java provides the do-while statement. l The Syntax is: do { statements } while ( condition ) ; l Interpretation »First the statements inside the do loop are executed regardless of the condition »Then the condition is evaluated »If it is true –The statements inside the do loop are executed again –And the condition is checked again, etc. »If condition is false –The statement that follows the do while statement is executed

10 do while statement flow diagram l Flow diagram true false Condition Statements inside the do loop

11 do while statement Example import java.io.*; class DoExample { public static void main(String[] args) throws java.io.IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); int num; do { System.out.println("Enter a Number or 0 to stop execusion"); String input= stdin.readLine(); num = Integer.parseInt(input); System.out.println("Squared number is:"+(num*num)); } while (num != 0); }