© A+ Computer Science - www.apluscompsci.com. Until I can hear the song Make it louder Loops repeat as long as something is true.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Looping Structures: Do Loops
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
© A+ Computer Science - public class CompSci { } All Java programs start with a class.
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.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
Copyright © Texas Education Agency, Computer Programming For Loops.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.
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.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
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,
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
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.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CPS120 Introduction to Computer Science Iteration (Looping)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
A: A: double “4” A: “34” 4.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
© A+ Computer Science - public class CompSci { } All Java programs start with a class.
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.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
© A+ Computer Science - Magpie Magpie is a lab that focuses on classes, randomness, and Strings. This lab will make sure that you.
Sophomore Scholars Java
Trace Tables In today’s lesson we will look at:
© A+ Computer Science - Magpie Chatbot Lab © A+ Computer Science -
Introduction to Computer Science / Procedural – 67130
Chapter 5: Loops and Files.
Web Programming– UFCFB Lecture 16
Manipulating Pictures, Arrays, and Loops part 2
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Loops The loop blocks you've used in Scratch can all be recreated in C! Loops allow for the repetition of code until a condition is met. There are three.
For Loops October 12, 2017.
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
CMSC 120: Visualizing Information 3/25/08
LRobot Game.
© A+ Computer Science - VARIABLES © A+ Computer Science -
Lecture Notes – Week 3 Lecture-2
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Computing Fundamentals
See requirements for practice program on next slide.
Chapter 4: Repetition Structures: Looping
Python While Loops.
© A+ Computer Science -
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.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
How to allow the program to know when to stop a loop.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

© A+ Computer Science -

Until I can hear the song Make it louder Loops repeat as long as something is true

© A+ Computer Science - As long as I am hungry I eat something While I have pretzels I eat one

© A+ Computer Science - loop if ( student is hungry and more pretzels left ) { student will eat 1 pretzel stick } condition

© A+ Computer Science - A loop is a tool used to repeat a block of code. As long as the loop condition is true, the block of code associated with the condition is executed.

© A+ Computer Science - total = 1 loop if (total < 2) { The class will : 1. stand up 2. sit down total = total + 1 } total 1 block of code condition total 1 2

© A+ Computer Science - Every loop has a condition that must be true before the loop can run. total<2 was the loop condition in the loop demonstration.

© A+ Computer Science -

A for loop is a block of code associated with a condition. The block of code will run a set number of times depending on the loop condition and increment/decrement value.

© A+ Computer Science - for( init value; boolean condition placed here; inc/dec) { do something 1; do something 2; }

© A+ Computer Science - start stop inc/dec for(int run=1; run<= 5; run=run+1) { out.println(run); } OUTPUT You have to tell the loop where to start, when to stop, and how much to change run.

© A+ Computer Science - Some languages use a structure with for loops. start – starting value of the loop stop – ending value of the loop step – amount to change the loop variable for x = start to stop step y - Visual Basic do something next x

© A+ Computer Science - start-0 for (int run = 1; //stop-1 ; //inc-3 ) { //code-2 } The start value tells the loop where to start. run will start with a value of 1.

© A+ Computer Science - stop-1 for ( //start-0; run<= 5; //inc-3 ) { //code-2 } Each time through the loop, the condition is evaluated. As long as run is less than or equal to 5, the loop continues. This condition must be true in order for the loop to execute.

© A+ Computer Science - inc-3 for ( //start-0; //stop-1; run=run+1) { //code-2 } The increment/decrement value tells the loop how much of a change to make to run.

© A+ Computer Science - start stop inc for (int run=1; run<=6; run=run+1) { out.println(run); } OUTPUT How many times does this loop run?

© A+ Computer Science -

for(int run=1; run<7; run=run+2) { out.println("loop"); out.println(run); } OUTPUT loop 1 loop 3 loop 5

© A+ Computer Science - for(int run=7; run>2; run=run-2) { out.println("loop"); out.println(run); } OUTPUT loop 7 loop 5 loop 3

© A+ Computer Science - for(int bin=1; bin<=32; bin=bin*2) { out.println(bin); } OUTPUT Why is this loop a log 2 N loop?

© A+ Computer Science -

out.println("cs contests are fun!"); for(int uil=5; uil>=1; uil--) { out.print("state-"); } out.println("\nchamps"); OUTPUT cs contests are fun! state-state-state-state-state- champs

© A+ Computer Science -

Many times you will use a loop to total up a run of values. total = total + run; total is totaling up all values of run.

© A+ Computer Science - int total = 0; for(int run=1; run<6; run++) { total=total+run; } out.println(total);

© A+ Computer Science - int total=0; for(int x=1; x<6; x++) { total=total+x; } out.println(total); TRACE xtotaloutput OUTPUT 15

© A+ Computer Science -

String s = "compsci"; for(int i=0; i<s.length(); i++) { out.println(s.charAt(i)); } OUTPUT c o m p s c i

© A+ Computer Science - String s = "compsci"; for(char c : s.toCharArray()) { out.println(c); } OUTPUT c o m p s c i

© A+ Computer Science -

for(int run=0; run>5; run++) { //do something }

© A+ Computer Science - For(int run=0; run<5; run++) { } for(int run=0; run<5; run++) ; { }

© A+ Computer Science - Never put a ; before an open { brace ;{ }; legal illegal

© A+ Computer Science -