Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
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 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 5 Loops.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
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 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
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
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:
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Lecture 6 Repetition Richard Gesick.
The switch Statement, and Introduction to Looping
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Loop Structures.
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
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.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Lecture 4A Repetition Richard Gesick.
Chapter 6 More Conditionals and Loops
CSS161: Fundamentals of Computing
Outline Altering flow of control Boolean expressions
Control Statements Loops.
3.5- The while Statement The while statement has the following syntax:
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Module 4 Loops and Repetition 4/4/2019 CSE 1321 Module 4.
Module 4 Loops and Repetition 4/15/2019 CSE 1321 Module 4.
Control Statements Loops.
Repetition Statements
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Outline Boolean Expressions The if Statement Comparing Data
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
CprE 185: Intro to Problem Solving (using C)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4

Outline Motivation while Loop Statement do-while Loop Statement for Loop Statement Infinite Loops Nested Loops Using break and continue 2/1/2019 CSE 1321 Module 4

1. Motivation USING LOOPS You want to print a string (e.g., "Welcome to CSE 1321!") a thousand times. You could copy/paste the following: PRINT("Welcome to CSE 1321!") There has to be a better way! How do we solve this problem? USING LOOPS 2/1/2019 CSE 1321 Module 4

1. Loop Statements They are controlled by boolean expressions Loops are repetition statements that allow us to execute a statement (or block of statements) multiple times They are controlled by boolean expressions We will study three types of loop statements: the while loop the do-while loop the for loop We must choose the right type for the situation at hand 2/1/2019 CSE 1321 Module 4

Loop Statements The while and do-while loops run un-determined (unknown) number of iterations The for loop, on the other hand, runs a pre-determined (known) number of iterations (some call it a counting loop) 2/1/2019 CSE 1321 Module 4

Criteria for all loops Have some initial condition Starting a counter Beginning in a certain state Must have a test to continue Must make progress towards finishing 2/1/2019 CSE 1321 Module 4

2. while Loop Statement A while loop (statement) has the following syntax: WHILE (condition) statement block //loop body END WHILE If the condition is true, the statement block is executed Then the condition is evaluated again, and executes the statement until the condition becomes false If the condition is false initially, the statement (loop body) is never executed Therefore, the body of a while loop will execute zero or more times 2/1/2019 CSE 1321 Module 4

while Loop Logic Note: If the initial evaluation of the condition is false, the loop body executes zero times. Therefore, the while loop executes zero or more times 2/1/2019 CSE 1321 Module 4

Example: Writing 1,000 Sentences (using a while loop) int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) } counter 1

Example: Writing 1,000 Sentences (using a while loop) int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) } counter 1

Example: Writing 1,000 Sentences (using a while loop) int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) } Output: 1 I will not eat at Taco Bell again counter 1

Example: Writing 1,000 Sentences (using a while loop) int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) } counter 1

Example: Writing 1,000 Sentences (using a while loop) int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) } counter 1

Example: Writing 1,000 Sentences (using a while loop) int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) } Output: 1 I will not eat at Taco Bell again counter 1

Example: Writing 1,000 Sentences (using a while loop) int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) } counter 1

Example: Writing 1,000 Sentences (using a while loop) int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) } counter 1

Example: Writing 1,000 Sentences (using a while loop) int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) } Output: 1 I will not eat at Taco Bell again counter 1

Infinite Loops This loop isn’t making a lot of progress! Loops that repeat forever are called infinite loops Apparently “lock up” Output: 1 I will not eat at Taco Bell again .

Problem Solved 1 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 1

Problem Solved 1 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 1

Problem Solved 1 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } Output: 1 I will not eat at Taco Bell again counter 1

Problem Solved 2 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 2 // Remember, counter++ is the same as // counter = counter + 1

Problem Solved 2 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 2

Problem Solved 2 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 2

Problem Solved 2 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } Output: 2 I will not eat at Taco Bell counter 2

Problem Solved 3 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 3

How does it end? 999 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 999

How does it end? 999 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 999

How does it end? 999 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 999

How does it end? 999 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } Output: 999 I will not eat at Taco Bell again counter 999

How does it end? 1000 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 1000

How does it end? 1000 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter 1000

How does it end? 1000 int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } // So we never print out // 1000 I will not eat at Taco Bell again counter now false 1000

Another Problem Solved int counter = 1 while (counter <= 1000) { PRINT (counter + “I will not…”) counter++ } counter now true 1000

Code int counter = 1; while (counter <= 1000) { System.out.println (counter + “ I will not…”); counter++; }

Code int counter = 1; while (counter <= 1000) { Console.WriteLine (counter + “ I will not…”); counter++; }

Code counter = 1 while (counter <= 1000): print (counter, " I will not...") counter = counter + 1

Sentinel Values Question: How can the user control a while loop? A sentinel value is a special input value that represents the end of inputs from the user The sentinel value should be included in the prompt so that the user knows how to stop the loop. For example, PRINT(“Enter a grade (type 9999 to quit):”) A sentinel value gives the user control over the loop 2/1/2019 CSE 1321 Module 4

Input Validation A while loop can be used for input validation, making a program more robust Ensure correct input values before processing Can issue error messages for invalid data 2/1/2019 CSE 1321 Module 4

In-class Problem: Input Validation Example Problem Statement: Write a program in which you allow your user to guess a secret number between 1 and 10. For this example, set your secret number to a literal for easy testing. When you are done, think about you can modify the program to allow the user to continue to guess until they get it right. Also, think about how you could use conditionals to give them valuable feedback to reach the answer! 2/1/2019 CSE 1321 Module 4

Pseudocode – Input Validation Example userGuess ← 0, secretNumber ← 5 PRINT “Enter a number between 1 and 10” READ userGuess WHILE (userGuess < 1 OR userGuess > 10) PRINT “That is not between 1 and 10. Try again.” READ userGuess ENDWHILE IF (userGuess == secretNum) THEN PRINT “That’s right! ”, userGuess, “ is the secret!” ELSE PRINT userGuess, “ is not the secret!” ENDIF Ps 2/1/2019 CSE 1321 Module 4

Input Validation Example public static void main (String[] args) { int userGuess = 0, secretNum = 5; Scanner scan = new Scanner (System.in); System.out.println("Enter a number between 1 and 10: "); userGuess = scan.nextInt(); while(userGuess < 1 || userGuess > 10) System.out.println ("Not between 1 and 10. Try again!"); } if (userGuess == secretNum) System.out.println(userGuess + " is the secret number!"); else System.out.println(userGuess + " isn’t the secret number!"); 2/1/2019 CSE 1321 Module 4

Input Validation Example public static void Main(string[] args) { int userGuess = 0, secretNum = 5; Console.WriteLine("Enter a number between 1 and 10: "); userGuess = Convert.ToInt32(Console.ReadLine()); while(userGuess < 1 || userGuess > 10) Console.WriteLine("That’s not between 1 and 10. Try again!"); } if (userGuess == secretNum) Console.WriteLine(userGuess + " is the secret number!"); else Console.WriteLine(userGuess + " is not the secret number!"); 2/1/2019 CSE 1321 Module 4

Python - Input Validation Example secretNumber = 5 userGuess = int(input("Enter a number between 1 and 10: ")) while(userGuess < 1 or userGuess >10): userGuess = int(input("Not between 1 and 10! Try again!")) if(userGuess == secretNumber): print("That's right! ", userGuess, " is the secret number!") else: print(userGuess, " is not the secret number!") 2/1/2019 CSE 1321 Module 4

3. do-while Loop A do-while loop has the following syntax: //statement block WHILE (condition) END DO-WHILE The statement is executed a minimum once initially, and then the condition is evaluated The statement is executed repeatedly until the condition becomes false 2/1/2019 CSE 1321 Module 4

Logic of do-while Loop 2/1/2019 CSE 1321 Module 4

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) counter

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) counter

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) counter 1

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) counter 1 Output: 1

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) counter 1

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) counter 2

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) counter 2 Output: 2

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) counter 2

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) // Note: counter is now 3, but we still have // to finish out the loop – it doesn’t skip counter 3

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) counter 3 Output: 3

Example (count from 1 to 3) int counter = 0 do { counter++ PRINT (counter) } while (counter < 3) counter 3 now false, so loop is finished

Pseudocode – do-while Loop Example Problem Statement: Write a program in which you enable your user to enter a number. Using a do- while loop, output the reverse display of that number. 2/1/2019 CSE 1321 Module 4

Pseudocode – do-while Loop Example CREATE number ← 0, lastDigit ← 0, reverse ← 0 PRINT “Enter a positive number.” READ number DO lastDigit ← number % 10 reverse ← (reverse * 10) + lastDigit number ← number / 10 WHILE (number > 0) ENDDO Ps 2/1/2019 CSE 1321 Module 4

Java - do-while Loop Example public static void main (String[] args) { int number, lastDigit, reverse = 0; Scanner scan = new Scanner (System.in); System.out.print ("Enter a positive integer: "); number = scan.nextInt(); do lastDigit = number % 10; reverse = (reverse * 10) + lastDigit; number = number / 10; } while (number > 0); // NOTE THE SEMICOLON!!!!!!!!!!! System.out.println ("The reversed is " + reverse); } 2/1/2019 CSE 1321 Module 4

C# - do-while Loop Example public static void Main(string[] args) { int number=0, lastDigit=0, reverse = 0; Console.WriteLine("Enter a positive integer: "); number = Convert.ToInt32(Console.ReadLine()); do { lastDigit = number % 10; reverse = (reverse * 10) + lastDigit; number = number / 10; } while (number > 0); // NOTE THE SEMICOLON!!!! Console.WriteLine("That number reversed is " + reverse); } 2/1/2019 CSE 1321 Module 4

Python – do-while Loop Example number = 0 lastDigit = 0 reverse = 0 number = int(input("Enter a positive integer: ")) helperVariable = True while (helperVariable == True): lastDigit = number % 10 reverse = (reverse * 10) + lastDigit number = (int)(number / 10) print (number, ":", reverse) if (number >= 1): else: helperVariable = False print("The number reversed is: ", reverse) Note: Python does not have an explicit do-while loop, However, one way is to use a helper variable 2/1/2019 CSE 1321 Module 4

4. for Loop A for statement has the following syntax: The initialization is executed once before the loop begins The statement is executed until the condition becomes false FOR (initialization, condition, increment) statement Block ENDFOR The increment portion is executed at the end of each iteration Collectively, the parameters in the FOR header identify the iteration bounds of the loop. 2/1/2019 CSE 1321 Module 4

for Loop Logic 2/1/2019 CSE 1321 Module 4

for Loop as a while Loop A for loop is functionally equivalent to the following while loop structure: FOR (initialization, condition, increment) statement block ENDFOR initialization WHILE (condition) statement block increment ENDWHILE 2/1/2019 CSE 1321 Module 4

“Equivalency” of while and for loops FOR (count ← 1, count <= 5, count ← count + 1) PRINT count ENDFOR count ← 1 WHILE (count <= 5) count ← count + 1 ENDWHILE 2/1/2019 CSE 1321 Module 4

Walkthrough of a for loop 1000 sentences again? No problem… int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) }

Why this works 1 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter 1

Why this works 1 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter true 1

Why this works 1 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } Output: 1 I will not eat Taco Bell. counter 1

Why this works 2 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter 2

Why this works 2 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter true 2

Why this works 2 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } Output: 2 I will not eat Taco Bell. counter 2

Why this works 3 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter 3

Why this works 3 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter true 3

Why this works 3 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } Output: 3 I will not eat Taco Bell. counter 3

Why this works 4 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter 4

When will it end? We see that this will go on for a while It’s a little more interesting later around 1000

Why this works 999 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter true 999

Why this works 999 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } Output: 999 I will not eat Taco Bell. counter 999

Why this works 1000 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter 1000

Why this works 1000 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter true for last time 1000

Why this works (are we finished?) int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } Output: 1000 I will not eat Taco Bell. counter 1000

Why this works 1001 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } counter 1001

Why this works 1001 int counter for (counter = 1, counter <= 1000, counter++) { PRINT (counter + “I will not…”) } // Jump down here and continue … counter false 1001

Final Output 1 I will not eat at Taco Bell.

In-class Problem: Problem Statement: Write a program in which you enable your user to enter a number for which they want to see the multiplication table. 2/1/2019 CSE 1321 Module 4

Pseudocode – for Loop Example CREATE userChoice ← 0 PRINT “Choose a number to see the multiplication table.” READ userChoice FOR (multiplier ← 1, multiplier < 12, multiplier ← multiplier + 1) PRINT userChoice, “ * “, multiplier, “ = “, (multiplier * userChoice) ENDFOR Ps 2/1/2019 CSE 1321 Module 4

Java - for Loop Example public static void Main(String[] args) { int choice = 0; Scanner scan = new Scanner (System.in); System.out.println("Enter a number to see the table."); choice = scan.nextInt(); for(int multiplier = 1; multiplier <= 12; multiplier += 1) System.out.println (multiplier + " * " + choice + " = " + (multiplier * choice)); } 2/1/2019 CSE 1321 Module 4

C# - for Loop Example public static void Main(string[] args) { int choice = 0; Console.WriteLine("Enter a number for the table."); choice = Convert.ToInt32(Console.ReadLine()); for(int multiplier = 1; multiplier <= 12; multiplier += 1) Console.WriteLine(multiplier + " * " + choice + " = " + (multiplier * choice)); } 2/1/2019 CSE 1321 Module 4

Python – for Loop Example choice = int(input("Enter the number for the table: ")) for multiplier in range(1, 13): print(multiplier, " * ", choice, " = ", multiplier * choice) We can add a parameter to define the incrementation, if it is other than 1. For instance, the code below will show the choice multiplied only by even numbers for multiplier in range(2, 13, 2): print(multiplier, " * ", choice, " = ", multiplier * choice) 2/1/2019 CSE 1321 Module 4

5. Infinite Loops The body of a while loop eventually must 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 – so double check! 2/1/2019 CSE 1321 Module 4

Example An example of an infinite loop: count ← 1 WHILE (count <= 25) PRINT count count ← count - 1 //Error ENDWHILE This loop will continue executing until interrupted or until an underflow error occurs 2/1/2019 CSE 1321 Module 4

Be Careful! If the condition is left out, it is always considered to be true, and therefore creates an infinite loop If the increment is left out, no increment operation is performed, and therefore creates an infinite loop FOR (count ← 1, count <= 5, count ← count+1) PRINT count ENDFOR 2/1/2019 CSE 1321 Module 4

6. Nested Loops Similar to nested if statements, loops contain other loop statements For each iteration of the outer loop, the inner loop iterates completely 2/1/2019 CSE 1321 Module 4

Pseudocode – Nested for Loop Example Problem Statement: Write a program that will use a nested for loop to print 10 rows of stars, as shown in the picture below 2/1/2019 CSE 1321 Module 4

Pseudocode – Nested for Loop Example CREATE maxRows ← 10, row, star FOR (row ← 1, row < maxRows, row ← row + 1) FOR (star ← 1, star <= row, star ← star + 1) PRINT “*” ENDinnerFOR PRINTLINE () ENDouterFOR Ps 2/1/2019 CSE 1321 Module 4

Java – Nested for Loop Example public static void main (String[] args) { int maxRows = 10; for (int row = 1; row <= maxRows; row++) { for (int star = 1; star <= row; star++) { System.out.print ("*"); } System.out.println(); } } 2/1/2019 CSE 1321 Module 4

C# - Nested for Loop Example public static void Main(string[] args) { int maxRows = 10; for (int row = 1; row <= maxRows; row++) for (int star = 1; star <= row; star ++) Console.Write("*"); } Console.WriteLine(); 2/1/2019 CSE 1321 Module 4

Python – Nested for Loop Example # Demonstrates the use of nested for loops to print stars rows = 10 for row in range(1,11): for star in range(1, row): print("*", end = "") print() 2/1/2019 CSE 1321 Module 4

Nested Loops Iterations How many times will the string "I am here" be printed? count1 ← 1 WHILE (count1 <= 10) count2 ← 1 WHILE (count2 <= 5) PRINT "I am here!" count2 ← count2 + 1 END inner WHILE PRINTLINE count1 ← count1 + 1 END outer WHILE 2/1/2019 CSE 1321 Module 4

7. Using break and continue We can additionally control the flow of how loops work with keywords break and continue break stops the loop continue “skips” the current iteration 2/1/2019 CSE 1321 Module 4

Example of break CREATE sum ← 0 CREATE number ← 0 WHILE (number < 20) number ← number + 1 sum ← sum + number if (sum >= 100) // stop if sum is over 100 break END WHILE PRINT "The number is ", number PRINT "The sum is ", sum 2/1/2019 CSE 1321 Module 4

Example of continue CREATE sum ← 0 CREATE number ← 0 WHILE (number < 10) number ← number + 1 if (number == 5 OR number == 6) continue; // do not add 5 and 6 to sum sum ← sum + number END WHILE PRINT "The number is ", number PRINT "The sum is ", sum 2/1/2019 CSE 1321 Module 4

In-class Problem Write Pseudocode for a calculator program that enables the user to select an operation (Add, Subtract, Multiply, Divide, or Quit) and then enter a series of numbers where that operation is applied. The program should continue asking for numbers until the user enters a 0, at which point it asks for another operation. 2/1/2019 CSE 1321 Module 4

Ps Partial Solution CREATE operation ← “”, number ← 0 WHILE (operation NOT EQUAL ”Q”) PRINT (“Enter an operation: A, S, M, D, Q”) READ operation SWITCH (operation) CASE ‘A’ DO PRINT (“Number to add (0 to quit):”) READ number total += number PRINT total WHILE (number <> 0) BREAK CASE ‘D’ PRINT (“Number to divide (0 to quit):”) total /= number DEFAULT PRINTLINE (“Invalid choice!”) END WHILE Ps 2/1/2019 CSE 1321 Module 4

In-class Problem #2 Problem Statement: Write a program that asks the user to input a number. The program should contain a while loop that runs if the user inputs any value other than 0. The loop body should accumulate the sum of all numbers that the user inputs. During each iteration, output the sum of the numbers input so far. When the user inputs a 0, the loop stops. After the loop has stopped, output the total of all numbers input and the average; if the loop never ran, indicate this to the user. 2/1/2019 CSE 1321 Module 4

Pseudocode – while Loop Sentinel Value CREATE sum ← 0, value ← 0, count ← 0 PRINT “Enter an integer (0 to quit)” READ value WHILE(value != 0) count ← count + 1 sum ← sum + value PRINT “The sum so far is “ + sum.” PRINT “Enter another integer (0 to quit)” READ value ENDWHILE IF (count == 0) THEN PRINT “No values were entered.” ELSE PRINT “The sum of all values is “ + sum PRINT “The average of all values is “ + sum/count ENDIF Ps 2/1/2019 CSE 1321 Module 4

End of Module 4 Slides 2/1/2019 CSE 1321 Module 4