COMP 14: Looping (part 2) May 31, 2000 Nick Vallidis.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CS0007: Introduction to Computer Programming
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
CS201 - Repetition loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14: switch, Developing Programs June 2, 2000 Nick Vallidis.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
COMP 14: Control Flow: if, if-then May 26, 2000 Nick Vallidis.
COMP 14: Miscellaneous :) May 30, 2000 Nick Vallidis.
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.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Copyright © Texas Education Agency, Computer Programming For Loops.
COMP 14: Writing Classes June 6, 2000 Nick Vallidis.
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.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
CONTROLLING PROGRAM FLOW
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,
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Repetition Statements while and do while loops
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
The Assignment operator tMyn1 The Assignment Operator The result of a calculation can be stored in a variable using the assignment operator =. Because.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
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.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Operators.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING While Loop.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
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.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Chapter 4 Repetition Statements (loops)
Loop Structures.
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.
Chapter 5: Repetition Structures
CiS 260: App Dev I Chapter 4: Control Structures II.
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Unary Operators ++ and --
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Instructor: Craig Duckett
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Control Statements:.
Presentation transcript:

COMP 14: Looping (part 2) May 31, 2000 Nick Vallidis

Announcements zAssignment P2 is due today! zAssignment P3 goes out today and is due next Tuesday (June 6th)

Review zHow does a while loop work? zWhat is a sentinel value? zWhat is an infinite loop? zWhat does it mean for a program to be robust? while (condition) statement;

Today  do loop  for loop

The do loop  Much like the while loop, but the statement is always executed at least once. do statement; while (condition);

The do loop  Much like the while loop, but the statement is always executed at least once. do statement; while (condition); Java Reserved Words

The do loop  Much like the while loop, but the statement is always executed at least once. do statement; while (condition); Java Reserved Words A boolean expression

The do loop  Much like the while loop, but the statement is always executed at least once. do statement; while (condition); Java Reserved Words A boolean expression Execute the statement, then test the condition. If it's true then execute the statement again and test the condition...

The do loop  Much like the while loop, but the statement is always executed at least once. do statement; while (condition); Don't forget to indent!

do loop example final int LIMIT = 5; int count = 5; do { count = count + 1; System.out.println(count); } while (count < LIMIT);

do loop control flow do statement; while (condition); true condition evaluated statement false

do can be written as while  A do loop can be easily re-written as a while loop. do statement; while (condition); statement; while (condition) statement; turns into

do can be written as while  A do loop can be easily re-written as a while loop. do statement; while (condition); statement; while (condition) statement; turns into These are exactly the same statement.

The for loop  do and while loops are good when you don't know how many times the loop will run.  for loops are good when you know the exact number of iterations.

The for loop for (initialization; condition; increment) statement;

The for loop for (initialization; condition; increment) statement; Java reserved word

The for loop for (initialization; condition; increment) statement; Java reserved word This statement is executed before the loop starts

The for loop for (initialization; condition; increment) statement; Java reserved word This statement is executed before the loop starts A boolean expression to indicate when to stop

The for loop for (initialization; condition; increment) statement; Java reserved word This statement is executed before the loop starts A boolean expression to indicate when to stop Statement that you want to execute multiple times.

The for loop for (initialization; condition; increment) statement; Java reserved word This statement is executed before the loop starts A boolean expression to indicate when to stop Statement that you want to execute multiple times. Executed after statement, but before the condition is tested again

The for loop for (initialization; condition; increment) statement; Don't forget to indent!

Using the for loop zRemember this example? final int LIMIT = 5; int count = 1; while (count <= LIMIT) { System.out.println(count); count = count + 1; } System.out.println("Done");

Using the for loop  Same thing as a for loop final int LIMIT = 5; int count; for (count=1; count<=LIMIT; count=count+1) { System.out.println(count); } System.out.println("Done");

for control flow for (initialization; condition; increment) statement; statementtrue condition evaluated false increment initialization

For can be written as while for (initialization; condition; increment) statement; initialization; while (condition) { statement; increment; } turns into

More operators zThe most common of these additional operators are the increment and decrement: yincrement is ++ ydecrement is --

Increment/decrement zThese can be put either before or after the variable name: int i; i++; //postfix form ++i; //prefix form

Postfix vs. Prefix zThese do different things! zPrefix increments the variable before the rest of the statement is executed zPostfix increments the variable after the rest of the statement is executed

Postfix vs. Prefix int i = 7; System.out.println(++i); // prints 8 i = 7; System.out.println(i++); // prints 7

Assignment operators zThese are just shortcuts for commonly used expressions: zWe probably won't be using the rest of these... x += y is the same as x = x + y x *= y is the same as x = x * y x -= y is the same as x = x - y x /= y is the same as x = x / y x %= y is the same as x = x % y

Conditional Operator zYou can read about this in the book (on pg. 130) if you want to learn it.  It is just a shortcut for the if-else statement and so you don't need to know it for COMP 14 Looks like this: condition ? expression : expression

Examples

Homework zRead 3.3, 3.5,