CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.

Slides:



Advertisements
Similar presentations
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Advertisements

ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Computer Science 1620 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.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Exam 2 – Nov 18th Room ACIV 008. Project 2 Update  Your code needs to use loops to create the multiplication table. Hint: use nested for loop (Lecture.
Nested LOOPS.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
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.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Lesson #5 Repetition and Loops.
REPETITION CONTROL STRUCTURE
C++ Programming: CS150 For.
Lesson #5 Repetition and Loops.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
INC 161 , CPE 100 Computer Programming
Looping.
Lesson #5 Repetition and Loops.
Week 6 CPS125.
UMBC CMSC 104 – Section 01, Fall 2016
Alternate Version of STARTING OUT WITH C++ 4th Edition
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 64 Tree Implementation
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
More Loops Topics Counter-Controlled (Definite) Repetition
CS150 Introduction to Computer Science 1
ECE 103 Engineering Programming Chapter 18 Iteration
Lesson #5 Repetition and Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from ECE 103 material by prof. Phillip PSU

Syllabus Nested while and for Loops Tracing Through a Loop Examples

2 Nested Loops A loop (e.g., while, do-while, for ) can be placed within the body of another loop Nested loops can be multiple levels deep (e.g., loop within a loop within a loop, etc.) For a nested loop, a break statement only exits out of the loop that contains the break

3 Nested while Loop: Typical 2-level nested while loop: while ( expr1 ) { : while ( expr2 ) { : Update expr2 ; } : update expr1; } ← outer loop ← inner loop

4 Example: 2-level nested while loop #include int main (void) { // main int row, col; row = 0; while( row < 3 ) { // note: magic number! col = 0; while( col < 3 ) { // ditto: Magic number! printf( "(%2d %2d) ", row, col ); col++; } //end while printf( "\n” ); row++; } //end while return 0; } //end main ( 0 0) ( 0 1) ( 0 2) ( 1 0) ( 1 1) ( 1 2) ( 2 0) ( 2 1) ( 2 2)

5 Nested for Loop: Typical 2-level nested for loop: for ( expr1a; expr2a; expr3a ) { : for ( expr1b; expr2b; expr3b ) { : } : } ← outer loop a ← inner loop b

6 Example: 3-level nested for loop for( level = 1; level <= 3; level++ ) { printf( "Level = %d\n", level ); for( R = 0; R < 3; R++ ) { for( C = 0; C < 3; C++ ) printf( "%3d ", R*C*level ); printf( "\n” ); } //end for

7 Example: 3-level nested for loop for( level = 1; level <= 3; level++ ) { printf("Level = %d\n", level ); for( R = 0; R < 3; R++ ) { for( C = 0; C < 3; C++ ) { printf( "%3d ", R*C*level ); } //end for printf( "\n” ); } //end for Level = Level = Level =

8 Example: for loop inside while loop /* computes factorial( num ) */ #include int main( void ) { // main int num; /* Input value */ long prod = 0; /* Accumulator for partial product */ int i, Done = 0; /* Index and flag */ while( !Done ) { printf( "Enter number: ” ); scanf( "%d", &num ); if( num < 0 ) { // Exit if user enters negative value Done = 1; }else{ prod = 1; for( i = 1; i <= num; i++ ) { prod *= i; } //end for printf( "factorial of %d = %d\n\n", num, prod ); } //end if } //end main

9 Tracing Through a Loop You can get a good understanding of loops by writing out a table of values The table shows the current state at the start of each iteration The columns of the table list the variables or calculations of interest at each iteration

10 Example: for( m = 1; m <= 3; m++ ) { s = m*m; printf("%d %d\n", m, s); } //end for Iteration #ms = m*mprintf(…)

11 Suppose you have a 2-level nested loop. The outer loop performs m iterations, and the inner loop does n iterations: This means:  For each iteration of the outer loop, the inner loop is executed n times  Total number of iterations is m × n

12 Example: for( m = 1; m <= 3; m++ ){ s = 0; for( n = 1; n <= 3; n++ ) { // number iterations SAME s += m*n; printf( "%d %d %d\n", m, n, s ); } //end for Iteration #mns += m*nprintf(…)

13 Example: for( m = 1; m <= 3; m++ ) { s = 0; for( n = m; n <= 3; n++ ) { s += m*n; printf("%d %d %d\n", m, n, s);} //end for Iteration #mns += m*nprintf(…) Here the number of iterations in the inner loop is controlled by the outer loop index