CS 106 Introduction to Computer Science I 02 / 15 / 2008 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.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
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.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 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.
Looping Yong Choi School of Business CSU, Bakersfield.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
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.
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.
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.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
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.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
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.
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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CS161 Introduction to Computer Science
Chapter 5: Control Structures II
Topic 5 for Loops -Arthur Schopenhauer
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
Arrays, For loop While loop Do while loop
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Control Statements Loops.
CS2011 Introduction to Programming I Loop Statements (II)
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
Let’s all Repeat Together
Building Java Programs
The for loop suggested reading:
Building Java Programs
Control Statements Loops.
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 / 15 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2008 Today’s Topics Comments and/or Questions? Loops for repeated execution –for loops –do-while loops –break; and continue; in loops –nested loops

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 2008

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 2008

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 Spring (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 2008

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 2008

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 2008

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 2008

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 Spring 2008

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 Spring 2008

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 Spring ,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 Spring 2008

break; We've seen how break; reacts in switch statements. 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 Spring 2008

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 Spring 2008

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, it basically skips all the code 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 Spring 2008

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 Spring 2008

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 Spring 2008

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 Spring 2008

exercise Write an application that keeps displaying in the command window the powers 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 Spring 2008

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 Spring 2008

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 Spring 2008

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 Spring 2008

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 Spring 2008

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 Spring 2008

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 Spring 2008