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

Slides:



Advertisements
Similar presentations
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Repeating Actions While and For Loops
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
Looping Yong Choi School of Business CSU, Bakersfield.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
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.
1 Repetition structures [cont’d] Overview l Nested Loops l Sentinel-Controlled loop l Avoiding Number Format exception.
Unit 3 1 Flow of control H We will talk about: conditional statements  if-then-else  logical and conditional operators  switch repetition statements.
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
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
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.
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,
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
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.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
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.
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.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
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.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
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 :
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Repetition-Counter control Loop
Programming Fundamentals
Repetition.
Decision statements. - They can use logic to arrive at desired results
Loop Control Structure.
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
LRobot Game.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Week 6 CPS125.
class PrintOnetoTen { public static void main(String args[]) {
while while (condition) { statements }
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

1 Repetition structures Overview while statement for statement do while statement

2 Repetition structures while statement »Syntax »Interpretation »Flow diagram for while statement »Example for statement »Syntax »Interpretation »Flow diagram for for statement »Example do while statement »Syntax »Interpretation »Flow diagram for do while statement »Example

3 while statement l Syntax while (condition) statement; l Interpretation »If the condition 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 of a while statement is false initially, the statement is not executed even once »Therefore, we say that a while statement executes its body zero or more times

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

5 while statement Example 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)); }

6 Three important points with regard to while statement 1. The condition of the while statement should have atleast one variable which is 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"); }

7 Nested Loops l One of the statements inside the a while could be another while loop – this are then called Nested Loops. public class NestedWhileLoop { public static void main(String[] args) { int i=1,j; while(i<=3) { j=1; while(j<=3) { System.out.print(i+","+j+"\t"); j++; } System.out.println(); i++; }

8 For statement l Syntax for (initialization; condition; updation) statements l Interpretation »First initialization statement is executed »Then the condition is evaluated »Repeating the following are done »If the condition is true, –The statements inside the loop are executed –Then the updation statement is executed –Then the condition is checked »If the condition is false, –The control will proceed to the next statement(s) after the for loop

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

10 For statement Example class ForExample { public static void main(String[] args) { for (int i = 1; i <= 10; i++) System.out.println(i + "\t" + Math.pow(i,2)); } for statements can also be nested. public class NestedForLoop { public static void main(String[] args) { int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) System.out.print(i+","+j+"\t"); System.out.println(); }

11 do while statement syntax l Syntax do { statements } while ( condition ) ; l Interpretation »First the statements inside the do loop are executed regardless of the condition »The statements inside the loop should have an Updation statement of loop control variable »Then the condition is evaluated »If it is true –The statements inside the do loop are executed again –And the condition is checked »If condition is false –The statement that follows the do while statement is executed

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

13 do while statement Example import TextIO; class DoExample { static TextIO stdin = new TextIO(System.in); public static void main(String[] args) throws java.io.IOException { int number; do { System.out.println("Enter a Number or 0 to stop execusion"); number = stdin.readInt(); System.out.println("Square of the number is: "+(number*number)); } while (number != 0); }