Copyright © Texas Education Agency, 20131 Computer Programming For Loops.

Slides:



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

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
CS0004: Introduction to Programming Repetition – Do Loops.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 5: Loops and Files.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.
Looping Yong Choi School of Business CSU, Bakersfield.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
CPS120 Introduction to Computer Science Iteration (Looping)
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
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.
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,
Repetition Statements while and do while loops
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
CS 100 Introduction to Computing Seminar October 7, 2015.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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 Looping 5. The Increment and Decrement Operators 5.1.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Lecture 6 Repetition Richard Gesick.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Topic 5 for Loops -Arthur Schopenhauer
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
Debugging October 4, 2006 ComS 207: Programming I (in Java)
FLUENCY WITH INFORMATION TECNOLOGY
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSCI 1100/1202 February 6, 2002.
Looping and Repetition
Presentation transcript:

Copyright © Texas Education Agency, Computer Programming For Loops

List some tasks that you have to do over and over again Mow the lawn Brush your teeth Take out the trash Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]2

Wouldn’t it be great if you only had to do the task one time, then it repeated itself automatically? Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]3

Looping is a central idea in programming ‘Looping’ is the programming term for repeating a task more than once. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]4

The ‘for’ loop is a basic structure for repetition for ( int j = 0; j < 7; j++) System.out.println(“Counting ” + j); // The loop above goes from 0 to 6 Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]5

for loops are used when… The same task must be executed several times. You know how many iterations* are needed * An iteration is a single execution of a loop, which may execute many times. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]6

The ‘for’ loop starts with the reserved word… for ( int j = 0; j < 7; j++) System.out.println(“Counting ” + j); Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]7

Then comes a header with 3 parts… for ( int j = 0; j < 7; j++) System.out.println(“Counting ” + j); Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]8

The first header item is the initialization expression for ( int j = 0; j < 7; j++) System.out.println(“Counting ” + j); This determines where the loop starts counting. In this case, the loop starts at 0. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]9

The second header item is the condition or control expression for ( int j = 0; j < 7; j++) System.out.println(“Counting ” + j); This determines where the loop stops counting. In this case, the loop stops when j is not less than 7. The condition must be true in order for the loop to keep running. If the condition is not true to start with, the loop will never run. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]10

The third header item is the step expression for ( int j = 0; j < 7; j++) System.out.println(“Counting ” + j); This determines how much the loop changes by. In this case, ‘j’ changes by 1 each time. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]11

Let’s see a for loop in action Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]12

for ( int j = 0; j < 5; j++) System.out.println(“Counting ” + j); Counting 0 Counting 1 Counting 2 Counting 3 Counting 4 Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]13

The initialization does not have to start with 0 for ( int j = 0; j < 7; j++) for ( int j = 4; j < 7; j++) for ( int j = 1; j < 32; j++) Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]14

The control must evaluate to true/false for ( int j = 0; j < =7; j++) for ( int j = 4; j*2 < 100; j++) for ( int j = 1; j < x; j++) Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]15

The step must move the condition towards false for ( int j = 0; j < =7; j++) for ( int j = 400; j > 0; j--) for ( int j = 1; j < 100; j=j+4) Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]16

When the condition is false, the loop is finished running Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]17 for ( int j = 0; j < 7; j--) System.out.println(“Counting ” + j); The loop above will never stop running, because j is becoming more negative, it will always be less than 0. This is known as an infinite loop.

When the loop condition begins as false, the loop will never run Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]18 for ( int j = 0; j > 7; j++) System.out.println(“Counting ” + j); The loop above will never start running, because j is not more than 7.

There is special step syntax for (int j = 0; j < 100; j=j+1) for (int j = 0; j < 100; j++) // means the same thing for (int j = 0; j < 100; j=j+8) for (int j = 0; j < 100; j+=8) // means the same thing for (int j = 0; j < 100; j=j*2) for (int j = 0; j < 100; j*=2) // means the same thing Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]19

Do you know the special syntax for the expressions below? j = j – 1 j = j – 2 j = j / 2 Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]20

Did you guess right? j = j – 1 same as j - = 1 or j -- j = j – 2 same as j -= 2 j = j / 2 same as j /=2 j = j +1 same as j += 1 or j++ Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]21

How do you make more than one line execute? for ( int j = 0; j < 7; j++) System.out.println(“Counting ” + j); System.out.println(“Printing ” + j); // Only the “Counting” line is in the for loop. // The “Printing” line runs after the loop ends Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]22

Use curly brackets to make a block statement for ( int j = 0; j < 7; j++) { System.out.println(“Counting ” + j); System.out.println(“Printing ” + j); } // Now both “Counting” and “Printing” execute inside the loop. The line or lines that execute inside a loop are known as the ‘body’ of the loop. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]23

Initialization Variables for ( int j = 0; j < 7; j++) The initialization variable tells the loop where to start. Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]24

The variable ‘j’ is only valid inside the for loop, where it was declared, but not outside the loop for ( int j = 0; j < 7; j++) { System.out.println( j); } // System.out.println(j); // this would not work Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]25

The variable ‘k’ is valid outside the for loop, where it was declared, as well as inside the loop int k; for ( k = 0; j < 7; k++) { System.out.println( k); } System.out.println(k); // this works Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]26

The loop count variable ‘k’ is incremented until the condition fails -- when it fails the loop is over int k; for ( k = 0; j < 3; k++) { System.out.println( k); // k=0, 1, 2 } System.out.println(k); // k is 3 Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]27

Review Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]28

What is this part called and what does it do? for ( int j = 4; j < 9; j++) System.out.println(“Counting ” + j); Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]29

What is this part called and what does it do? for ( int j = 2; j < 9; j++) System.out.println(“Counting ” + j); Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]30

What is this part called and what does it do? for ( int j = 14; j > 7; j -=2) System.out.println(“Counting ” + j); Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]31

Can you write a for loop? Copyright © Texas Education Agency, 2013IT: [Computer Programming] - [For Loops]32