CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Advertisements

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 04 / 2008 Instructor: Michael Eckmann.
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.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CPS120 Introduction to Computer Science Iteration (Looping)
Loops and Iteration for Statements, while Statements and do-while Statements.
Chapter 4: Loops and Files
Chapter 5 Loops.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
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,
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
The for loop.
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.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 4 Repetition Statements (loops)
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Lab5 PROGRAMMING 1 Loop chapter4.
PROGRAM FLOWCHART Iteration Statements.
Review of Previous Lesson
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.
Loops and Iteration CS 21a: Introduction to Computing I
Control Statements:.
Presentation transcript:

CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2007 Today’s Topics questions, comments? loop terminology To repeatedly execute a block of code –for loops –do - while loops

while loops While loops are used to repeat lines of code until some condition is met. e.g. while (condition) { // statements to do while condition is true. } condition is evaluated and if true the code within the curly braces executes. Then condition is tested again and if true the code within the curly braces executes. This continues until the condition is false (or a break; statement is hit.) Michael Eckmann - Skidmore College - CS Fall 2005

Terminology The body of a loop is --- the statements within the curly braces (or if it doesn't have a compound statement, the single statement is the body.) A control variable is one that is used in the condition of the loop to determine if the body should execute or if the loop should terminate. An iteration of a loop is one complete cycle through a loop while it is running. This includes checking the condition, changing the control variable, and the main body of the loop. A compound statement is 1 or more statements within curly braces. Michael Eckmann - Skidmore College - CS Fall 2005

for loops The for loop is a counter controlled repetition structure that compactly lets a programmer specify the control variable, its initial value, the condition to be checked each time through the loop and the amount to increment (or decrement) the control variable for ( expression1; expression2; expression3 ) statement or compound statement Michael Eckmann - Skidmore College - CS Spring 2007

for loops for ( expression1; expression2; expression3 ) statement or compound statement where expression1 contains the declaration and initialization of the control variable expression2 contains the continuation condition and the ending value of the control variable expression3 contains the increment or decrement of the control variable (and the amount of this change in value each time through the loop) Michael Eckmann - Skidmore College - CS Spring 2007

for loops for ( expression1; expression2; expression3 ) { statements } Order of operations: 1. expression1 executes first (and only once) 2. expression2 executes and if the result is false, program control goes to the code after the right curly brace 3. if the result is true, then the statements within the curly braces of the for loop execute in order then expression3 executes (which usually alters the value of the loop variable.) Then “goto” 2 above. Michael Eckmann - Skidmore College - CS Fall (if 2 was true)

for loop example for ( int x = 10; x >= 1; x-- ) { do some statements here.... } note that we used x-- to subtract 1 from the counter each time through the loop, we could have used: x = x - 1 or x -= 1 or --x Michael Eckmann - Skidmore College - CS Spring 2007

for loop Each of the three expressions in the for loop structure are optional. don’t need expression1 if control variable declared and initialized before the loop don’t need expression2 if you desire having an infinite loop don’t need expression3 if you change the value of the control variable within the loop Michael Eckmann - Skidmore College - CS Spring 2007

for loop for ( int cntr = 1; cntr <= 20; cntr = cntr + 1 ) { do some statements here.... } this is essentially the same thing as the following while loop int cntr = 1; while ( cntr <= 20 ) { do some statements here.... cntr = cntr + 1; } except, the variable cntr is not available to the program after the for loop, but it is available to be referenced after the while loop Michael Eckmann - Skidmore College - CS Spring 2007

exercise Write an application that calculates the product of the odd integers from 1 to 15 and then displays the results in a message dialog. Let's write it using a while loop Then let's write it using a for loop Michael Eckmann - Skidmore College - CS Spring 2007

do while loops While loops and for loops all have the possibility of having the statements inside them execute zero or more times. Zero being the key word here. When would it be possible for those loops to execute zero times? The big difference between those kinds of loops and do- while loops are that do-while loops execute their statements at least once (that is, one or more times.) Michael Eckmann - Skidmore College - CS Fall 2005

do while loops do { statements to do } while (condition); –In this kind of loop, the condition is tested after the loop executes the first time. If the condition is true it does the statements again, and so on until the condition is false. Michael Eckmann - Skidmore College - CS Fall 2005

do while loops do { statements to do } while (condition); –In this kind of loop, the condition is tested after the loop executes the first time. If the condition is true it does the statements again, and so on until the condition is false. Michael Eckmann - Skidmore College - CS Fall ,3 (if 2 was true)

do while loops int cntr = 1; while ( cntr <= 20 ) { do some statements here.... cntr = cntr + 1; } // the above is basically equivalent to: int cntr = 1; do { do some statements here... cntr = cntr + 1; } while (cntr <= 20); Michael Eckmann - Skidmore College - CS Fall 2005

Nested loops The body of a loop can contain another loop. We say that the inner loop is nested inside (or within) the outer loop. It is important to understand that the inner loop executes fully (all its iterations) within each iteration of the outer loop. For example, if the inner loop of a nested loop always iterates 10 times and the outer loop always iterates 5 times --- the code within the inner loop (its body) will end up executing a total of 50 times. Michael Eckmann - Skidmore College - CS Fall 2005

Nested loop - what prints? public class Printing { public static void main(String args[]) { for ( int i = 1; i <= 10; i++) { for ( int j = 1; j<=5; j++) { } System.out.println(); } Michael Eckmann - Skidmore College - CS Fall 2005

Nested loop - what prints? public class Printing2 { public static void main(String args[]) { int i = 1, j = 1; for ( ; i <= 10; i++) { for ( ; j<=5; j++) { } System.out.println(); } Michael Eckmann - Skidmore College - CS Fall 2005

Nested loop - what prints? public class Printing3 { public static void main(String args[]) { int i = 1, j = 1; while (i <= 10) { while ( j<=5) { j++; } System.out.println(); i++; } Michael Eckmann - Skidmore College - CS Fall 2005

Nested loop - what prints? public class Printing4 { public static void main(String args[]) { int i = 1; while (i <= 10) { int j = 1; while ( j<=5) { j++; } System.out.println(); i++; } Michael Eckmann - Skidmore College - CS Fall 2005

Pay attention to control variable The lesson is, to pay attention to the control variables. Does the inner loop's control variable get reset each time through the outer loop?, etc. Michael Eckmann - Skidmore College - CS Fall 2005

More Nesting example on page 185 Michael Eckmann - Skidmore College - CS Fall 2005

break; We've seen how break; reacts in switch statements. Well break; acts similarly within the curly braces of a while loop, for loop or do-while loop. It terminates the loop and program control continues after the loop as if it ended normally. Michael Eckmann - Skidmore College - CS Fall 2005

break; int x = 0; while (x <= 10) { if (x == 5) break; System.out.println(“x = “ + x); x++; } // what's this code going to do? Michael Eckmann - Skidmore College - CS Fall 2005

continue; The continue; is a way to cause your loop to skip the rest of the current iteration of the loop and continue on to the next iteration. So, what it does is, it basically skips all the code in the body of the loop after it and then goes to the next iteration. It acts slightly differently in for loops vs. while or do-while loops. After the rest of the current iteration is skipped, for a for loop, the next thing that happens is the expression3 then expression2 is evaluated to determine if it should go again. Michael Eckmann - Skidmore College - CS Fall 2005

continue; However, continue; within a while or a do-while loop acts like: After the rest of the current iteration is skipped, the next thing that happens is the condition is tested to determine if it should go again. Michael Eckmann - Skidmore College - CS Fall 2005

continue; for (int x = 0; x <= 10; x++) { if (x == 5) continue; System.out.println(“x = “ + x); } // what's this code going to do? Michael Eckmann - Skidmore College - CS Fall 2005

continue; int x = 0; while (x <= 10) { if (x == 5) continue; System.out.println(“x = “ + x); x++; } // what's this code going to do? Michael Eckmann - Skidmore College - CS Fall 2005

break; and continue; break; is never absolutely necessary continue; is never absolutely necessary labelled version of these are also never absolutely necessary That is, any code you write in Java that contains a break or continue can be rewritten without them. Michael Eckmann - Skidmore College - CS Fall 2005

labeled break & continue statements labeled break statements within nested structures (while, for, do while or switch structure) –A label can be given to a block of code. A block of code is defined as code enclosed in a pair of curly braces. –The labeled break statement can specify the block to “jump out of.” Non-labeled break statements could only “jump out of” the immediately enclosed structure, whereas, labeled break statements can “jump totally out of” a set of nested structures. Michael Eckmann - Skidmore College - CS Fall 2005

labeled break & continue statements labeled continue statements within nested structures (while, for, or do while structure) –A label can be given to a structure. –The labeled continue statement causes execution to skip the code in the enclosing structures and continues with the next iteration of the structure with the specified label. Michael Eckmann - Skidmore College - CS Fall 2005

labeled break & continue statements Let’s look at an example of how to label a block and nest some loops so that we can do a labeled break; or a labeled continue; Michael Eckmann - Skidmore College - CS Fall 2005

exercise Write an application that keeps displaying in the command window the multiples of the integer 2, namely 2, 4, 8, 16, 32,... Your loop should not terminate (i.e. you should create an infinite loop.) discuss: –Condition –Why program stops or doesn’t stop Michael Eckmann - Skidmore College - CS Fall 2005