1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

TE Sessions Supported by: Basic Concepts of Programming November 3, 2012.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Repeating Actions While and For Loops
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.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
An Introduction to Programming with C++ Fifth Edition
Copyright © Texas Education Agency, Computer Programming For Loops.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Mastery Objective: Students will understand how to use while loops in computer programming.
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.
CPS120 Introduction to Computer Programming The Programming Process.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Control Structures CPS120: Introduction to Computer Science Lecture 5.
Course A201: Introduction to Programming 09/16/2010.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter 6 Questions Quick Quiz
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 13 Conditional.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Language Find the latest version of this document at
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
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.
5b – For Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
REPETITION CONTROL STRUCTURE
Chapter 5: Loops and Files.
Logical Operators and While Loops
Control Structure Senior Lecturer
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.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Algorithm and Ambiguity
Loop Strategies Repetition Playbook.
CHAPTER 4 Iterative Structure.
Logical Operators and While Loops
Programming Concepts and Database
7 – Variables, Input and Output
6 – Miracle And “Hello World”
Repetition Statements (Loops) - 2
Python While Loops.
Chapter 13 Conditional Repetition
Programming Concepts and Database
4a- If And Else Lingma Acheson CSCI N331 VB .NET Programming
Presentation transcript:

1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI

Task: Simulate a race counting laps. Output “Now on Lap X”. For simplicity, count only Lap 1 – output statements? Same output statement, can we reuse it? Solution – “For Loop” structure

Loops A loop is used for repetitive behaviour A set of commands is placed in a code block A condition determines when the block stops repeating

The For Loop Used when you know how many times something will happen Great for counting

Parts of a For Loop Sentry Variable (counter) Initial Value (the value where the counter starts) Looping Condition (repeat the codes as long as the counter meets the condition) Increment Statement (how to increment the counter) Lap counter example: –Sentry variable (create a variable called i) –Initial value: 1 –Looping condition: i<=10 –Increment Statement: i++ (i increase by 1)

Algorithm Pseudocode: - Start a new program - Create a new variable i - Create a For loop (Conditional =>For loop) - Tell the For loop to use i as the counter - Give the starting value of i - Give the ending value of i - Give the increment method - What command to execute if the condition is true: output “Now on Lap” + i - End For loop - End the program

Miracle Loop Tracking – –i=1 Is i<=10? Yes! Execute the codes. i increase by 1. -i = 2 Is i<=10? Yes! Execute the codes. i increase by 1. -i = 3 Is i<=10? Yes! Execute the codes. i increase by 1. … -i = 10 Is i<=10? Yes! Execute the codes. i increase by 1. - i = 11 Is i<=10? No!!! Loop finishes.

Miracle Lab – Time to create the Lap Counter!

Increment Statement for(i=1; i<=10; i++){ … } A line of code Always involves sentry variable (counter) Changes value of sentry Usually i++, i--, i=i+2,… Must make it possible for condition to become false eventually – for(i=11; i>=10; i++){ //WRONG! … }

More example Counting backwards. New Year Eve Celebration – counting seconds left. E.g. for(i=10; i>=0; i--){ //output: i + “ seconds left!” } Generating even or odd numbers. E.g. for(i=0; i<=20; i=i+2){ //output: i + “ is an even number.” }

For Loop Lab – Create a program that output even or odd numbers smaller than 12.

The While Loop Another type of Loop structure Use the word “while” Logic – as long as the condition is true, execute the code block (stop executing the code block when the condition becomes false) Requires: –Sentry Variable (counter) –Initial Value (the value where the counter starts) –Looping Condition (repeat the codes as long as the counter meets the condition) –Increment Statement (how to increment the counter) 12

Practice While Using Miracle to create the Lap Counter with the While Loop structure Algorithm: Pseudocode - - Start a new program - Create a new variable i - Give the starting value of i for the while loop - Create a while loop (Conditional =>While loop) - Give the condition (i<=10) - What command to execute if the condition is true: output “Now on Lap” + i - Give the increment method (i++) - End while loop - End the program 13

0 or Infinite Loops Code block might not get executed at all, if the condition is false in the beginning E.g. var i=1; while (i<1) { Output “Now on lap ” + i; i++; } Must make sure the condition becomes false so the code block can stop executing, otherwise code goes into a infinite loop. E.g. var i=1; while (i>=1) { Output “Now on lap ” + i; i++; } 14

Compare If, For and While Compare If branch, For Loop and While Loop for(i=1; i<=10; i++){ … } i=1; While(i<=10) { … i++; } i=1; If(i<=10){ … i++; }

While Loop Lab – –Create a program that takes a user input, and output even or odd numbers within the user input, e.g. if user input is 10, output 0, 2, 4, 6, 8, 10.