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.

Slides:



Advertisements
Similar presentations
Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.
Advertisements

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.
CS0004: Introduction to Programming Repetition – Do Loops.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 Repetition structures Overview while statement for statement do while statement.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
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.
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.
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.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Repetition Statements while and do while loops
CS 100 Introduction to Computing Seminar October 7, 2015.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Topic 5 for Loops -Arthur Schopenhauer
Repetition Structures (Loops)
Topics Introduction to Repetition Structures
Chapter 4 Control structures and Loops
Iteration with While You can say that again.
Programming Fundamentals Lecture #6 Program Control
Alternate Version of STARTING OUT WITH C++ 4th Edition
INC 161 , CPE 100 Computer Programming
Conditional Construct
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Loop Strategies Repetition Playbook.
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Introduction to Repetition Structures
Topics Introduction to Repetition Structures
Repetition Statements (Loops) - 2
Repetition Statements
Introduction to Computer Science I.
Loops CGS3416 Spring 2019 Lecture 7.
Looping Structures.
LOOP Basics.
Chapter 4: Loops and Iteration
Presentation transcript:

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 love Java”); ▫ That wouldn’t be pretty Can we make that happen more easily?

2 Types of Loops Counter-Controlled Loops ▫ use a counter to determine how many times the loop body executes ▫ “Enter 10 numbers” ▫ “Print this that and the other 25 times” Sentinel Controlled ▫ Wait for a special value (sentinel value) to be entered ▫ “Enter a positive number” (anything negative is sentinel) ▫ “Enter -1 to quit” ( -1 is the sentinel value)

Repetition Executing a sequence of statements multiple times governed by a condition ▫ Loop will execute while condition is true. Three looping constructs in Java ▫ We’ll start with the while loop while(conditionIsTrue) //execute some logic while(conditionIsTrue) { //execute multiple //lines of logic }

While Loop - Flow Chart Condition Some Code true false int i = 0; while(i < 125) { System.out.println(“I love Java”); i = i + 1; }

Let’s Trace int i = 0; while(i < 4) { System.out.println(“I love Java”); i = i + 1; } 0 i I love Java I love Java

Let’s Trace int i = 0; while(i < 4) { System.out.println(“I love Java”); i = i + 1; } 1 i I love Java I love Java I love Java

Let’s Trace int i = 0; while(i < 4) { System.out.println(“I love Java”); i = i + 1; } 2 i I love Java I love Java I love Java I love Java

Let’s Trace int i = 0; while(i < 4) { System.out.println(“I love Java”); i = i + 1; } 3 i I love Java I love Java I love Java I love Java I love Java

Let’s Trace int i = 0; while(i < 4) { System.out.println(“I love Java”); i = i + 1; } 4 i I love Java

Uh Oh.. What’s wrong? int i = 0; while(i < 4); { System.out.println(“I <3 Java”); i = i + 1; } Creates an infinite loop

Infinite Loops Loops that start but never stop – stopping condition is wrong or missing int i = 0; while(i >= 0) { System.out.println(“I <3 Java”); i = i + 1; } This condition will never be false …never ever Usually want it to be true for a while,