ECE 103 Engineering Programming Chapter 19 Nested Loops

Slides:



Advertisements
Similar presentations
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Advertisements

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.
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
Computer Science 1620 Loops.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
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.
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 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.
1 Standard Version of Starting Out with C++, 4th Brief Edition 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.
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.
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.
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.
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.
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.
Repetition statements
Lesson #5 Repetition and Loops.
Lecture 7: Repeating a Known Number of Times
C++ Programming: CS150 For.
Lesson #5 Repetition and Loops.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
INC 161 , CPE 100 Computer Programming
Looping.
CS1100 Computational Engineering
Lesson #5 Repetition and Loops.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
ECE 103 Engineering Programming Chapter 32 Array Parameters
Week 6 CPS125.
UMBC CMSC 104 – Section 01, Fall 2016
Alternate Version of STARTING OUT WITH C++ 4th Edition
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 64 Tree Implementation
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
More Loops Topics Counter-Controlled (Definite) Repetition
Herbert G. Mayer, PSU CS Status 7/19/2015
More Loops Topics Counter-Controlled (Definite) Repetition
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
ECE 103 Engineering Programming Chapter 22 Selection
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

ECE 103 Engineering Programming Chapter 19 Nested Loops Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Syllabus Nested while and for Loops Tracing Through a Loop Examples

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

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

Example: 2-level nested while loop #include <stdio.h> 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++; return 0; } //end main ( 0 0) ( 0 1) ( 0 2) ( 1 0) ( 1 1) ( 1 2) ( 2 0) ( 2 1) ( 2 2) 4

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

Example: 3-level nested for loop // level and R are defined 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 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 ); } //end for printf( "\n” ); Level = 1 0 0 0 0 1 2 0 2 4 Level = 2 0 4 8 Level = 3 0 3 6 0 6 12 7

Example: for loop inside while loop /* computes factorial( num ) */ #include <stdio.h> 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 8

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

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

Suppose you have a 2-level nested loop 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

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 } //end for Iteration # m n s += m*n printf(…) 1 1 1 1 2 3 1 2 3 6 1 3 6 4 2 1 2 5 2 2 6 12 2 3 12 7 3 1 3 8 9 3 2 9 18 3 3 18 12

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 } //end for Here the number of iterations in the inner loop is controlled by the outer loop index Iteration # m n s += m*n printf(…) 1 1 1 1 2 3 1 2 3 6 1 3 6 4 2 2 4 5 10 2 3 10 9 3 3 9 13