Looping Structures.

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.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
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.
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.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
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,
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
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.
Iteration 1 Looping Structures. Iteration 2 The Plan While not everyone understands: 1.Motivate loops 2.For loops 3.While loops 4.Do-while loops 5.Equivalence.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
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.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
© 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 :
Slides by Evan Gallagher
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Lecture 6 Repetition Richard Gesick.
The switch Statement, and Introduction to Looping
Chapter 5: Control Structures II
Loop Structures.
Lecture 4: Program Control Flow
Topic 5 for Loops -Arthur Schopenhauer
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Lecture 4A Repetition Richard Gesick.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
Iteration with While You can say that again.
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Module 4 Loops.
Building Java Programs
Loop Strategies Repetition Playbook.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
What output is produced by the following fragment?
Building Java Programs
The for loop suggested reading:
A LESSON IN LOOPING What is a loop?
Building Java Programs
Repetition Statements (Loops) - 2
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Building Java Programs
Control Structures.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

Looping Structures

The Plan While not everyone understands: Motivate loops For loops While loops Do-while loops Equivalence Application of Simulated Collision Practice Problems

Motivation Why loop? Sometimes you need to do things again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again...and finally you get tired of typing.

Motivation Okay, so that's not all. You also loop in order to: Group repeatedly executed code for uniformity Make the number of repetitions easily changeable Repeat events which the number of exectutions is known only dynamically Combine with selection statements to make more complex algorithms

System.out.println(i); i++; while Loop int i=0; while(i<10) { System.out.println(i); i++; }

System.out.println(i); i++; while Loop Initialization int i=0; while(i<10) { System.out.println(i); i++; } Condition true Update

System.out.println(i); i+=0.1; while Loop Why might this be a problem? double i=0; while(i<1) { System.out.println(i); i+=0.1; }

System.out.println(i); for Loop for(int i=0; i<10; i++) { System.out.println(i); }

System.out.println(i); for Loop Initialization Condition Update for(int i=0; i<10; i++) { System.out.println(i); } True False

for(int i=0; i<10; i++) { } for Loop Initialization Condition Update for(int i=0; i<10; i++) { System.out.println(i*0.1); } True False Scale factor

for(int i=0; i<10; i++) { } for Loop Initialization Condition Update for(int i=0; i<10; i++) { System.out.println(i*0.1+5); } True False Scale factor Translation

System.out.println(i); i++; } Equivalence of Loops int i=0; while(i<10) { System.out.println(i); i++; } for(int i=0; i<10; i++) { System.out.println(i); }

System.out.println(i); i++; }while(i<=10); do-while Loop int i=0; do { System.out.println(i); i++; }while(i<=10);

System.out.println(i); i++; }while(i<=10); do-while Loop int i=0; do { System.out.println(i); i++; }while(i<=10); Initialization True Update Condition False

was not here in the while loop! do-while Loop int i=0; do { System.out.println(i); i++; }while(i<=10); Notice this semicolon was not here in the while loop!

Is it important for the loop body to always execute at least once? When to use which loop? Is it known how many times the loop will execute prior to executing the loop body? Yes for No Is it important for the loop body to always execute at least once? Yes do-while No while

When to use which loop? Real answer: Use which ever structure is most convenient, because all loop structures can be represented as any other loop structure. Why are there multiple loop structures then? Simple answer – for the programmer’s convenience. Note: Java 5.0 offers another form of the for loop We will cover this at a later point

Practice Problems Write a loop to print out from 10 to 100 inclusive counting by 10s Write a loop that starts with an arbitrary double x and divides it by 2 repeatedly until it is less than 1. Output the number of times the loop executed. What is being computed? Write a loop that sums the first x integers where x is a positive integer. Print out the results. Write a loop that takes an integer x starting with value 1 and doubles x so long as x is positive. Bonus question: why doesn’t this loop infinitely? Super Bonus question: why does it loop infinitely when x is a double?