Chapter 4 Repetition Statements (loops)

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Loops.
Advertisements

Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©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.
1 Infinite Loops  The body of a while loop eventually must make the condition false  If not, it is an infinite loop, which will execute until the user.
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.
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.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Repetition Statements while and do while loops
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
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.
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
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
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 3: Decisions and Loops
The switch Statement, and Introduction to Looping
Chapter 6 More Conditionals and Loops
Loop Structures.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 Control structures and Loops
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Lecture Notes – Week 3 Lecture-2
The ‘while’ Statement September 27, 2006
Chapter 8: More on the Repetition Structure
3.5- The while Statement The while statement has the following syntax:
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
CprE 185: Intro to Problem Solving (using C)
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Outline Boolean Expressions The if Statement Comparing Data
Chap 7. Advanced Control Statements in Java
CSCI 1100/1202 February 6, 2002.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Chapter 4: Loops and Iteration
Presentation transcript:

Chapter 4 Repetition Statements (loops)

Loops – While, Do, For Repetition Statements While Do For

Repetition Statements Repetition statements allow us to execute a statement or a block of statements 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: while do for The programmer should choose the right kind of loop statement for the situation

The while Statement A while statement has the following syntax: 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 while ( condition ) statement;

Logic of a while Loop condition evaluated false statement true

The while Statement An example of a while statement: 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 int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }

Trace while Loop int count = 0; while (count < 2) { animation Trace while Loop Initialize count int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }

Trace while Loop, cont. int count = 0; while (count < 2) { animation Trace while Loop, cont. (count < 2) is true int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }

Trace while Loop, cont. int count = 0; while (count < 2) { animation Trace while Loop, cont. Print Welcome to Java int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }

Trace while Loop, cont. int count = 0; while (count < 2) { animation Trace while Loop, cont. Increase count by 1 count is 1 now int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }

(count < 2) is still true since count is 1 animation Trace while Loop, cont. (count < 2) is still true since count is 1 int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }

Trace while Loop, cont. int count = 0; while (count < 2) { animation Trace while Loop, cont. Print Welcome to Java int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }

Trace while Loop, cont. int count = 0; while (count < 2) { animation Trace while Loop, cont. Increase count by 1 count is 2 now int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }

(count < 2) is false since count is 2 now animation Trace while Loop, cont. (count < 2) is false since count is 2 now int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }

The loop exits. Execute the next statement after the loop. animation Trace while Loop The loop exits. Execute the next statement after the loop. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }

Example (Average.java)

Infinite Loops Executing the statements in the body of a while loop must eventually make the condition false If not, it is called an infinite loop, which will execute until the user interrupts the program This is a common logical error You should always double check the logic of a program to ensure that your loops will terminate

Infinite Loops An example of an infinite loop: This loop will continue executing until the user externally interrupts the program. int count = 1; while (count <= 25) { System.out.println(count); count = count - 1; }

Nested Loops Similar to nested if statements, loops can be nested as well That is, the body of a loop can contain another loop For each iteration of the outer loop, the inner loop iterates completely

Nested Loops How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) System.out.println ("Here"); count2++; } count1++; 10 * 20 = 200