Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.

Slides:



Advertisements
Similar presentations
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Advertisements

1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
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
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Chapter 5 Loops.
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.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
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 4: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Fundamentals 4.
Chapter 9 Repetition.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 5: Control Structures II
The switch Statement, and Introduction to Looping
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Topic 5 for Loops -Arthur Schopenhauer
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 4 – Control Structures Part 1
Chapter 8: Control Structures
Chapter 5: Control Structures II
SELECTION STATEMENTS (1)
Control Structures - Repetition
Chapter 5 Repetition.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
CSS161: Fundamentals of Computing
Outline Altering flow of control Boolean expressions
Additional Control Structures
Control Statements Loops.
Repetition Control Structure
Chapter 8: More on the Repetition Structure
Lab5 PROGRAMMING 1 Loop chapter4.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
The for loop suggested reading:
Flowcharts and Pseudo Code
Chapter 5: Control Structures II (Repetition)
Repetition Statements (Loops) - 2
Control Statements Loops.
Java LESSON 3 Loops.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13

written in Java Programs (general solution) transform the initial input values (specific problem) to the final output values (specific solution) Solutions are expressed as – Sequence of statements – Branching – Repetition Design before code – Flowcharts – Activity Diagrams – Pseudo Code thus far in CS140 … 2 − Variables − Classes − Objects and references

Chapter 4 Java Loop Statements (4.1) The while statement The do-while statement Nested loops The for statement Programming with loops (4.2) Graphics Supplement (4.3) 3

Conditional Loops: while and do-while SYNTAX: while (condition) loop body SYNTAX: do loop body while (condition) ; 4 SEMANTICS: repeat the loop body while the condition is true Loop Body can be a single statement, or a {statement block} Condition can be a logical expression or a boolean value (variable or constant)

Repetition: while and do-while SYNTAX: while (condition) loop body SYNTAX: do loop body while (condition) ; 5 loop body true false condition loop body true false condition

What is the output? public static final int MAX = 3; int count = 0; while (count < MAX){ System.out.print(count + “, ”); count++; } 1.(none) 2.0, 1, 2, 3, 3.1, 2, 3, 4.0, 1, 2, 5.(don’t know) 6

public static final int MAX = 3; int count = 0; while (count > MAX){ count++; System.out.print(count + “, ”); } What is the output? 1.(none) 2.0, 1, 2, 3, 3.1, 2, 3, 4.0, 1, 2, 5.idk 7

What is the output? public static final int MAX = 3; int count = 0; while (count < MAX){ count++; System.out.print(count + “, ”); } 1.0, 1, 2, 3, 2.1, 2, 3, 3.0, 1, 2, 4.1, 2, 3, 4 5.idk 8

What is the output? public static final int MAX = 3; int count = 0; while (count < MAX) count++; System.out.print(count + “, ”); 1.0, 2.1, 2, 3, 3.1, 4.3, 5.idk 9

What is the output? public static final int MAX = 3; int count = 0; while (count > MAX) count++; System.out.print(count + “, ”); 1.0, 2.1, 2, 3, 3.1, 4.3, 5.idk 10

What is the output? public static final int MAX = 3; int count = 0; while (count < MAX); count++; System.out.print(count + “, ”); 1.0, 2.1, 2, 3, 3.1, 4.3, 5.(none) 6.idk 11

What is the output? public static final int MAX = 3; int count = 0; while (count > MAX); count++; System.out.print(count + “, ”); 1.0, 2.1, 2, 3, 3.1, 4.3, 5.(none) 6.idk 12

What is the output? public static final int MAX = 3; int count = 0; while (count < MAX) System.out.print(count + “, ”); 1.(none) 2.0, 1, 2, 3, 3.1, 2, 3, 4.0, 0, 0, 0, 0, … 5.idk 13

What is the output? public static final int MAX = 3; int count = 0; do { count++; System.out.print(count + “, ”); } while (count < MAX); 1.0, 1, 2, 3, 2.1, 2, 3, 3.0, 1, 2, 4.1, 2, 3, 4 5.idk 14

What is the output? public static final int MAX = 3; int count = 0; do{ System.out.print(count + “, ”); count += 3; } while (count < MAX); 1.(none) 2.0, 3.0, 3, 4.0, 0, 0, 0, 0, … 5.idk 15

What is the output? public static final int MAX = 3; int count = 0; do System.out.print(count + “, ”); while (count > MAX); 1.(none) 2.0, 3.1, 2, 3, 4.0, 0, 0, 0, 0, … 5.idk 16

What is the output? public static final int MAX = 3; int count = 0; do System.out.print(count + “, ”); while (count < MAX); 1.(none) 2.0, 3.1, 2, 3, 4.0, 0, 0, 0, 0, … 5.idk 17

SYNTAX: while (condition) loop body SYNTAX: do loop body while (condition) ; 18 Loop body true false condition Loop body true false condition 1.for loop 2.while loop 3.do-while loop 4.idk

Repetition: for loop SYNTAX: for (initialization; condition; update) loop body 19 SEMANTICS: Execute the initialization (once) repeat the loop body while the condition is true Execute the update statement after each loop

Repetition Conditionally –while –do-while “pre-determined number of times” can know in advance how many times something should be repeated –for loop 20 Q: So why all the examples of while and do-while where there is counting? A: Easiest way to illustrate how those loops work. Q: So why all the examples of while and do-while where there is counting? A: Easiest way to illustrate how those loops work.

Repetition: for loop SYNTAX: for (initialization; condition; update) loop body 21 Loop body true false condition initialization update

Repetition: for loop int max = 6; int i; for ( i = 0; i < max; i++ ) { System.out.print( i ); } 22 int max = 6; int i = 0; while ( i < max ) { System.out.print( i ); i++; }

Repetition: for loop int max = 6; int i; for ( i = 0; i < max; i++ ) { System.out.print( i ); } System.out.print( “last i ” + i ); 23 int max = 6; for ( int i = 0; i < max; i++ ) { System.out.print( i ); } System.out.print( “last i ” + i );

What is the output? public static final int MAX = 3; int count; for(count = 0; count < MAX; count++) System.out.print(count + “, ”); 1.(none) 2.0, 1, 2, 3.1, 2, 3, 4.0, 0, 0, 0, 0, … 5.idk 24 Print count true false count < MAX count = 0 count++

What is the output? public static final int MAX = 3; int count; for(count = 0; count < MAX; count++); System.out.print(count + “, ”); 1.(none) 2.0, 1, 2, 3.1, 2, 3, idk 25

What is the output? public static final int MAX = 3; int count; for(count = 0; count < MAX; count++) System.out.print(count + “, ”); System.out.print(count); 1. (none)2. 1, 1 2, 2 3, , 1, 2, 34. 1, 2, 3, , 0, 0, 0, 0, … 6. idk 26

What is the output? public static final int MAX = 3; int count; for(count = 0; count < MAX; count+=2) System.out.print(count + “, ”); System.out.print(count); 1. (none)2. 2, , 1, 2, 34. 0, 2, , 2 6. idk 27

What is the output? public static final int MAX = 3; int count; for(count = MAX; count > 0; count--) System.out.print(count + “, ”); System.out.print(count); 1. (none)2. 3, 2, 1, 3. 3, 2, 1, 0,4. 2, 1, 0, 5. 3, 3, 3, 3, 3, … 6. idk 28

What is the output? public static final int MAX = 3; int count; for(count = MAX; count < 0; count--) System.out.print(count + “, ”); System.out.print(count); 1. (none)2. 3, 2, 1, 3. 3, 2, 1, 0, idk 29

Nested loops Loops can be nested just like branching expressions int a = 3; int b = 2; for (int i = 0; i < b; i++) { for (int j = 0; j < a; j++) { System.out.print( j + i ); } System.out.println( i ); } 30

What is the output? public static final int MAX = 3; for (int f1 = 0; f1 < MAX; f1++){ for (int f2 = 0; f2 < MAX; f2++){ System.out.print(f1 + “ + ” + f2); System.out.println(“ = ” + (f1+f2)); } 31 not multi-choice

What is the output? public static final int MAX = 3; int count = 0; while (count < MAX){ for (int f2 = 0; f2 < MAX; f2++){ System.out.print(count + “ ”); } count++; } 32 not multi-choice

What is the output? public static final int MAX = 3; int count = 0; while (count < MAX){ for (int f2 = 0; f2 < MAX; f2++){ System.out.print(f2 + “ ”); } count++; } 33 not multi-choice

Which Loop? Do you know (for any input) how many times the loop body will be executed? Should the loop body execute one or more times? 34

Which Loop? Write a program to read a list of integers and to display the largest, smallest, and average of all integers. The user indicates the end of the input list by entering 0 (zero) while() 2.do-while() 3.for() 4.idk

Which Loop? Write a program to read a list of 20 integers and to display the largest, smallest, and average of all integers while() 2.do-while() 3.for() 4.idk

Which Loop? Write a program that reads a bank account balance and an interest rate and displays the balance of the account in ten years while() 2.do-while() 3.for() 4.idk

Which Loop? Write a program that reads a bank account balance and an interest rate and displays the balance of the account after a number of years entered by the user while() 2.do-while() 3.for() 4.idk

Which Loop? Write a program that reads a bank account balance and an interest rate and displays the balance of the account in ten years. The interest is added each month of each of the ten years while() 2.do-while() 3.for() 4.idk

Which Loop? Write a program that reads a bank account balance and an interest rate and displays the balance of the account in ten years. The interest is added each day of each month of each of the ten years while() 2.do-while() 3.for() 4.idk

Which Loop? Write a program that simulates the falling and bouncing of a ball by computing its height at each 1/10 of a second (of a simulated clock). The user enters an initial height (above the ground) and the program continues until the fifth bounce while() 2.do-while() 3.for() 4.idk

break; and continue; break; - ends only the innermost loop or switch continue; - ends current iteration and proceeds to the next one Only use these if you have a good reason Try to rewrite your code to avoid these 42

Review Three loop statements – Two are top-tested: execute loop body 0+ – One is bottom tested: execute loop body 1+ Loop body can be – a single statement or {statement block} – Branching – Loops Condition can be boolean or logical expression Choose for when number of loops is ‘known’ 43