CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between.

Slides:



Advertisements
Similar presentations
CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
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.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CSC 107 – Programming For Science. Announcement Today’s Goal  Know how to write selections besides if-else  Why we use select or if - else and how.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
While ( number
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
PHY 107 – Programming For Science. Today’s Goal  How to use while & do / while loops in code  Know & explain when use of either loop preferable  Understand.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CSC Programming for Science Lecture 12: while & do/while Loops.
Decision Making: while loops and Boolean Logic. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Chapter 4 Repetition Statements (loops)
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Iteration CSCE 121 J. Michael Moore.
Loop Control Structure.
Looping and Repetition
Iteration with While You can say that again.
LRobot Game.
Lecture Notes – Week 3 Lecture-2
Introduction to Object-Oriented Programming with Java--Wu
CSC215 Lecture Control Flow.
Repetition Control Structure
Chapter 8: More on the Repetition Structure
CMPT 102 Introduction to Scientific Computer Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
CHAPTER 21 LOOPS 1.
Seating “chart” Front - Screen rows Back DOOR.
PROGRAM FLOWCHART Iteration Statements.
CSC215 Lecture Control Flow.
statement. Another decision statement, , creates branches for multi-
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between the 3 loops with same end result  Explain why we would never convert between loops

Loop Structure

while Loop while (expression) { statement;... } L OOP TERMINATION CONDITION  expression is L OOP TERMINATION CONDITION  Boolean expression checked at start of each pass  Executes entire loop body if expression is true  Once expression checked & found false, loop is over  L OOP BODY  L OOP BODY inside braces executed during pass

do-while Loop

Loop Structure

Loop Control Variant  Variable used to control when loop ends  Counting passes of loop often performed with this  Count not literal; could be constant form of update  Very common when used in strictly bounded loops

Loop Control Variant  Examples of loops using loop control variant  Launching a rocket ( Blast off!)

Loop Control Variant  Examples of loops using loop control variant  Launching a rocket ( Blast off!)  Going through even numbers ( …)

Loop Control Variant  Examples of loops using loop control variant  Launching a rocket ( Blast off!)  Going through even numbers ( …)  Alarm clocks (5:57… 5:58… 5:59… it!)

Loop Control Variant  Examples of loops using loop control variant  Launching a rocket ( Blast off!)  Going through even numbers ( …)  Alarm clocks (5:57… 5:58… 5:59… it!)  Love (She loves me, she love me not, she loves me)

for Loop

Initialization Expression

Loop Control Variant Update

Comparing for & while Loops for Loop while Loop

Tracing for Loop

Counting in for Loops  for 's control variant often counts up or down  Check against limit in loop termination expression  Limit against which we will check is often constant  Use constants to hold value of these limits  Makes code much easier to read  Simplifies changing code when limit needs to change for (i = 0; i < 42; i++) or for (i = 0; i < THE_ANSWER; i++)

Your Turn  Get in groups & work on following activity

For Next Lecture