Module 4 Loops.

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

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
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
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
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:
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Lesson #5 Repetition and Loops.
CSC111 Quick Revision.
Whatcha doin'? Aims: To start using Python. To understand loops.
Repetition Structures
REPETITION CONTROL STRUCTURE
The switch Statement, and Introduction to Looping
CS161 Introduction to Computer Science
Lesson #5 Repetition and Loops.
Chapter 5: Control Structures II
CS1371 Introduction to Computing for Engineers
Repetition-Counter control Loop
While Loops in Python.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
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
Building Java Programs
Building Java Programs
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Looping and Repetition
Iteration with While You can say that again.
Conditional Statements
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Outline Altering flow of control Boolean expressions
Lesson #5 Repetition and Loops.
Repetition Structures
Building Java Programs
Java Programming Loops
Iteration: Beyond the Basic PERFORM
Control Statements Loops.
Lec 4: while loop and do-while loop
Building Java Programs
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Computing Fundamentals
Flowcharts and Pseudo Code
A LESSON IN LOOPING What is a loop?
For loops Taken from notes by Dr. Neil Moore
Java Programming Loops
Lesson #5 Repetition and Loops.
Building Java Programs
Control Statements Loops.
Building Java Programs
Selections and Loops By Sarah, Melody, Teresa.
While Loops in Python.
The while Looping Structure
Looping Structures.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Computer Science Club 1st November 2019.
Presentation transcript:

Module 4 Loops

Sometimes it’s nice to do something over and over again Say we ask the user for an even number. They give us the number 5. Wouldn’t it be nice if we could ask over and over again until they gave us what we asked for? We can use a while loop to repeat a section of code WHILE a certain condition is true. Code it!

Sometimes it’s nice to do something over and over again While this condition is true, we repeat the code inside the curly brackets over and over again.

While loops There are three parts to every loop: int n = 0; while(n <= 3) { System.out.println(n); n++; } 1) Loop variable initialization 2) Conditional Statement 3) Loop variable update

While loops n So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; }

While loops n So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output:

While loops n 1 So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output:

While loops n 1 So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output: 1

While loops n 2 So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output: 1 2

While loops n 3 So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output: 1 2 3

So this loop prints out the numbers 0 through 3 While loops n 4 So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output: 1 2 3 So this loop prints out the numbers 0 through 3

Now it’s your turn! Use a while loop to print all the numbers from 1 to 100 to the screen. One number per line. Notice how it’s no more work for you to print 100 numbers to the screen than it was to print 4 numbers to the screen.

What is the output? Socrative What is the output of the following code? This is what is known as an infinite loop. Since we start above the baseline and count up, our conditional statement will ALWAYS be true.

What about this one? Socrative What is the output of the following code? This loop will not run at all. The conditional statement is false, so the loop gets skipped and the update statement never occurs.

Print positives Use a while loop to keep asking the user for numbers until they enter a zero. Print each number the user enters AS LONG AS IT IS POSITIVE (do not print any negative numbers or zero). So for example, if the user typed: 5 -8 3 1 0 You would print out: 5 3 1 Hint – start by printing out all the numbers the user enters. Once you get that working, then try and print out only the numbers you want.

Print positives

Average Now let’s write a program that finds the average of a bunch of numbers. Ask the user to enter numbers, entering zero when done Use a while loop to sum up the numbers and keep track of how many there are Divide the sum by the count to get the average Code it!

Careful or you run into integer division here! If you don’t check, you might end up dividing by zero! Careful or you run into integer division here!

ReverseString Ask the user for a String. Print out the string in reverse order. Do this by starting at the last letter of the String and use a while loop to “walk through” the String, index by index until you get to the beginning of the String. Hint: recall the String methods length() and charAt() Example: If the user enters “Hello There”, you would want to display “erehT olleH”

ReverseString

Do-While Loops Do-while loops are very similar to while loops. The main difference is that a do-while loop ALWAYS executes at least once.

Remember what a While loop looks like? There are three parts to every loop: int n = 0; while(n <= 3) { System.out.println(n); n++; } 1) Loop variable initialization 2) Conditional Statement 3) Loop variable update

Here’s what a Do-While loop looks like: There are three parts to every loop: int n = 0; do { System.out.println(n); n++; } while(n <= 3); 1) Loop variable initialization 3) Loop variable update Notice the ; 2) Conditional Statement

Let’s walk through this do-while n int n = 0; do { System.out.println(n); n++; } while(n <= 3); Output:

Let’s walk through this do-while n int n = 0; do { System.out.println(n); n++; } while(n <= 3); 1 Output: 1

Let’s walk through this do-while n int n = 0; do { System.out.println(n); n++; } while(n <= 3); 2 Output: 1 2

Let’s walk through this do-while n int n = 0; do { System.out.println(n); n++; } while(n <= 3); 3 Output: 1 2 3

Let’s walk through this do-while Notice how this works the same way as the while loop except that the order the code is executed has changed slightly. n int n = 0; do { System.out.println(n); n++; } while(n <= 3); 4 Output: 1 2 3

Now it’s your turn! Use a do-while loop to print all the numbers from 1 to 100 to the screen. One number per line.

Remember, do-while loops ALWAYS run at least once! What is the output? Socrative Nothing 1 1 through 100 Something else Remember, do-while loops ALWAYS run at least once!

How about this one? Socrative If you forget the update statement for any type of loop, you can run into an infinite looping situation.

Do-while loops make great game loops! Scanner kb = new Scanner(System.in); char playAgain = ‘n’; do { //game code System.out.print(“Do you want to play again? “); playAgain = kb.nextLine().toLowerCase().charAt(0); } while(playAgain == ‘y’); If you start a game, you probably want to play it at least once. After playing, you can ask if the user wants to play again or not.

Your turn Take a look at your Rock/Paper/Scissors game Use a do-while loop to allow the user to play over and over again.