CSS161: Fundamentals of Computing

Slides:



Advertisements
Similar presentations
Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
Advertisements

Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
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.
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
Java Programming: From the Ground Up
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 5 Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Java Prepared by Gary Langner Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
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:
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
Java Programming: From Problem Analysis to Program Design, 4e 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.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Repetition Statements
Java Fundamentals 4.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Loops.
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
Chapter 6 More Conditionals and Loops
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 5: Control Structures II
Loop Structures.
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
Control Statements Loops.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Control Statements Loops.
Repetition Statements
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Outline Boolean Expressions The if Statement Comparing Data
CSS161: Fundamentals of Computing
Chapter 3 Flow of Control Loops in Java.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

CSS161: Fundamentals of Computing Loops This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Loops Loop: Loop body, (i.e., Statements) to be repeated A Boolean expression to terminate the repetition Iteration: Each repetition of the loop body Three types of loop statements while loop: a Boolean expression evaluated at top of each iteration do-while loop: a Boolean expression evaluated at bottom of each iteration for loop: is similar to while loop but additionally includes initialization statements and post-iteration updating statements CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing while Statement Syntax while (Boolean_Expression) Statement Or { Statement_1 Statement_2 Statement_Last } Example int countDown = 3; while ( countDown > 0 ) System.out.println( “Hello” ); countDown = countDown - 1; Boolean Expression? false true Statement(s) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing do-while Statement Syntax do Statement while (Boolean_Expression); Or { Statement_1 Statement_2 Statement_Last } while (Boolean_Expression); Example countDown = 3; System.out.println( “Hello” ); countDown = countDown – 1; } while ( countDown > 0 ); Statement(s) true Boolean Expression? false CSS161: Fundamentals of Computing

Algorithms and Pseudocode Algorithm: a set of precise instructions that leads a solution. Example: Sorting Algorithms (Selection, Bubble, Insertion, Quick, Merge) Sorting three integers: Declare three variables: a, b, and c; Read keyboard inputs into a, b, and c; Swap a and b if a < b Swap a and c if a < c Swap b and c if b < c Print out a, b, and c Pseudocode: a mixture of a programming language and human language to write an algorithm int a, b, c, tmp; // read keyboard inputs into a, b, and c; if ( a < b ) { tmp = a; a = b; b = tmp; } if ( a < c ) { a = c; c = tmp; if ( b < c ) { tmp = b; b = c; // print out a, b, and c CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Pseudocode to Program import java.util.Scannaer; public class Averager { public static void main( String[] args ) Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter nonnegative scores.” ); System.out.println( “End with a negative number.” ); System.out.println( “I will compute their average” ); double next, sum = 0; int count = 0; next = keyboard.nextDouble( ); while( next >= 0 ) sum += next; count++; } if ( count == 0 ) System.out.println( “No scores entered.” ); else double average = sum/count; System.out.println( count + “ scores read” ); System.out.println( “The average is ” + average ); Give the user instructions. count = 0; sum = 0; Read a number and store it in a variable named text. While ( next >= 0 ) { sum = sum + next; count++; Read a number and store it in next. } The average is sum/count provided count is not zero. Output the results Sentinel Value CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on Textbook p131’s exercises 22 ~ 27. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing for Statement Syntax for (Initialization; Boolean_Expression; Updates) Body Examples int next, sum = 0; for ( next = 0; next <= 10; next++ ) { sum = sum + next; System.out.println( “sum up to ” + next + “ is ” + sum ); } Scanner keyboard = new Scanner( System.in ); int value = keyboard.nextInt( ); int limit = keyboard.nextInt( ); for ( int mult = value; mult <= limit; mult += value ) System.out.println( mult ); Initialization false Boolean Expression? true Statement(s) Updates CSS161: Fundamentals of Computing

for-Equivalent while Loop Syntax CSS161: Fundamentals of Computing

Comma in for Statements Multiple initializing actions, each separated by a comma Multiple updating actions, each separated by a comma Syntax: for ( Init1, Init2, ..; Boolean_Expression; update1, update2, … ) body Notes: If Initialization also declares variables (valid only within the for-loop), all declared variables must be the same type. Updates can replace body that can be even eliminated. (the last semicolon is necessary.) Example: for ( term = 1; sum = 0; term <= 10; sum += term, term++ ) ; // empty body CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Examples Syntactically illegal for ( int term = 1, double sum = 0; term <= 10; term++ ) sum = sum + term; double sum; for ( int term = 1, sum = 0; term <=10; term++ ) Syntactically legal for ( int term = 1; sum = 0; term <= 10; term++ ) Should be int CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Repeat N Times Loops Print N times (1-indexed counting) public static final int N = 30; for ( int count = 1; count <= N; count++ ) System.out.println( “Hop, Step, Jump” ); Alternative way to count (0-indexed counting) for ( int count = 0; count < N; count++ ) System.out.println( “Hop, Stem, Jump” ); int[] array = new int[N]; for ( int index = 0; index < N; index++ ) array[index] = 100; CSS161: Fundamentals of Computing

Empty Statement in a for Loop Pitfall for ( int count = 0; count <= 10; count++ ); System.out.println( “count = ” + count ); Correct for ( int count = 0; count <= 10; count++ ) Intentional for ( int count = 0; count <= 10; System.out.println( “count = “ + (count++ ) ); An empty statement: meaninglessly repeated 10 times; CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Infinite Loops The Boolean expression in a do-while/while/for loop must eventually end up with “false”. Otherwise the loop can’t get finished. Examples number = 1; while ( number != 12 ) { System.out.println( number ); number = number + 2; } for ( int count = 0, limit = 5; count < limit; limit++ ) System.out.println( count ); for( ; ; ) { while ( true ) { number <= 12 count++ if ( Boolean_expression ) break; CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Nested Loops When nested, the inner loop iterates from beginning to end for each single iteration of the outer loop Examples What results will be printed out? int rowNum, columnNum; for (rowNum = 1; rowNum <=3; rowNum++) { for (columnNum = 1; columnNum <=2; columnNum++) System.out.print(" row " + rowNum + " column " + columnNum); System.out.println(); } for ( int row = 1; row <= 10; row++ ) for ( int star = 1; start <= row; star++ ) System.out.print( “*” ); System.out.println( ); CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing break Statement Breaking out of the current loop for ( int x = 0; x < MAX; x++ ) if ( ( next = keyboard.nextInt( ) ) <= 0 ) break; for ( int y = 0; y < MAX; y++ ) for ( int z = 0; z < MAX; z++ ) Breaking out of the current and outer loops all under a give label outerLoop: middleLoop: innerLoop: for ( int z = 0; z < MAX; z++ ) { if ( ( next = keyboard.nextInt( ) ) < 0 ) break innerLoop; if ( next == 0 ) break middleLoop; if ( next < 100 ) break outerLoop; } CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing continue Statement Ends the current iteration of the nearest enclosing loop, and then Starts the next iteration. for ( int num = 0; num < MAX; num++ ) { if ( ( div = keyboard.nextInt( ) ) == 0 ) continue; System.out.println( num + “/” + div + “=“ + num/div ) } for ( int x = 0; x < MAX; x++ ) for ( int y = 0; y < MAX; y++ ) for ( int z = 0; z < MAX; z++ ) { if ( ( div = keyboard.nextInt( ) ) <= 0 ) System.out.println( ( x + y + z ) / div ) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing exit Statement break ends a loop, but does not end the program. System.exit(0) immediately ends the program. 0 used to indicate a normal ending of the program. System.out.println( “Enter a negative number:” ); int negNumber = Kyeboard.nextInt( ); if ( negNumber >= 0 ) { System.out.println( negNumber + “>= 0 ... Program aborting.” ); System.exit( 0 ); } CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on Textbook P140’s exercises 28 ~ 39 CSS161: Fundamentals of Computing