DOC PREVIEW
UMBC CMSC 104 - More Loops

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

More LoopsCounter-Controlled Repetition (Definite Repetition)Counter-Controlled Repetition (con’t)Event-Controlled Repetition (Indefinite Repetition)Event-Controlled Repetition (con’t)The 3 Parts of a LoopThe for Loop Repetition StructureWhen Does a for Loop Initialize, Test and Modify?A for Loop That Counts From 0 to 9We Can Count Backwards, TooWe Can Count By 2’s ... or 7’s … or WhateverThe do-while Repetition StructureExampleAn Equivalent while LoopAn Equivalent for LoopSo, Which Type of Loop Should I Use?Nested LoopsNested for LoopsThe break StatementExample break in a for LoopThe continue StatementExample continue in a for LoopCMSC 104, Version 9/01 1More LoopsTopics•Counter-Controlled (Definite) Repetition•Event-Controlled (Indefinite) Repetition•for Loops•do-while Loops•Choosing an Appropriate Loop•Break and Continue Statements Reading•Sections 4.1 - 4.6, 4.8, 4.9CMSC 104, Version 9/01 2Counter-Controlled Repetition (Definite Repetition)•If it is known in advance exactly how many times a loop will execute, it is known as a counter-controlled loop.int i = 1 ;while ( i <= 10 ){printf(“i = %d\n”, i) ;i = i + 1 ;}CMSC 104, Version 9/01 3Counter-Controlled Repetition (con’t)• Is the following loop a counter-controlled loop?while ( x != y ){ printf(“x = %d”, x) ; x = x + 2 ;}CMSC 104, Version 9/01 4Event-Controlled Repetition(Indefinite Repetition)•If it is NOT known in advance exactly how many times a loop will execute, it is known as an event-controlled loop.sum = 0 ;printf(“Enter an integer value: “) ;scanf(“%d”, &value) ;while ( value != -1) {sum = sum + value ;printf(“Enter another value: “) ;scanf(“%d”, &value) ;}CMSC 104, Version 9/01 5Event-Controlled Repetition (con’t)•An event-controlled loop will terminate when some event occurs.•The event may be the occurrence of a sentinel value, as in the previous example.•There are other types of events that may occur, such as reaching the end of a data file.CMSC 104, Version 9/01 6#include <stdio.h>int main (){int i = 1 ; initialization of loop control variable/* count from 1 to 100 */while ( i < 101 ) test of loop termination condition{printf (“%d “, i) ;i = i + 1 ; modification of loop control} variable return 0 ;}The 3 Parts of a LoopCMSC 104, Version 9/01 7The for Loop Repetition Structure•The for loop handles details of the counter-controlled loop “automatically”.•The initialization of the the loop control variable, the termination condition test, and control variable modification are handled in the for loop structure.for ( i = 1; i < 101; i = i + 1){initialization modification} testCMSC 104, Version 9/01 8When Does a for Loop Initialize, Test and Modify?•Just as with a while loop, a for loopoinitializes the loop control variable before beginning the first loop iteration,omodifies the loop control variable at the very end of each iteration of the loop, andoperforms the loop termination test before each iteration of the loop.•The for loop is easier to write and read for counter-controlled loops.CMSC 104, Version 9/01 9A for Loop That Counts From 0 to 9for ( i = 0; i < 10; i = i + 1 ){printf (“%d\n”, i) ;}CMSC 104, Version 9/01 10We Can Count Backwards, Toofor ( i = 9; i >= 0; i = i - 1 ){printf (“%d\n”, i) ;}CMSC 104, Version 9/01 11We Can Count By 2’s ... or 7’s … or Whateverfor ( i = 0; i < 10; i = i + 2 ){printf (“%d\n”, i) ;}CMSC 104, Version 9/01 12The do-while Repetition Structuredo{statement(s)} while ( condition ) ;•The body of a do-while is ALWAYS executed at least once. Is this true of a while loop? What about a for loop?CMSC 104, Version 9/01 13Exampledo{printf (“Enter a positive number: “) ;scanf (“%d”, &num) ;if ( num <= 0 ){printf (“\nThat is not positive. Try again\n”) ;}} while ( num <= 0 ) ;CMSC 104, Version 9/01 14An Equivalent while Loopprintf (“Enter a positive number: “) ;scanf (“%d”, &num) ;while ( num <= 0 ){printf (“\nThat is not positive. Try again\n”) ;printf (“Enter a positive number: “) ;scanf (“%d”, &num) ;}•Notice that using a while loop in this case requires a priming read.CMSC 104, Version 9/01 15An Equivalent for Loopprintf (“Enter a positive number: “) ;scanf (“%d”, &num) ;for ( ; num <= 0; ){ printf (“\nThat is not positive. Try again\n”) ; printf (“Enter a positive number: “) ; scanf (“%d”, &num) ;}• A for loop is a very awkward choice here because the loop is event-controlled.CMSC 104, Version 9/01 16So, Which Type of Loop Should I Use?•Use a for loop for counter-controlled repetition.•Use a while or do-while loop for event-controlled repetition.oUse a do-while loop when the loop must execute at least one time.oUse a while loop when it is possible that the loop may never execute.CMSC 104, Version 9/01 17Nested Loops•Loops may be nested (embedded) inside of each other.•Actually, any control structure (sequence, selection, or repetition) may be nested inside of any other control structure.•It is common to see nested for loops.CMSC 104, Version 9/01 18Nested for Loopsfor ( i = 1; i < 5; i = i + 1 ){ for ( j = 1; j < 3; j = j + 1 ) {if ( j % 2 == 0 ){ printf (“O”) ;}else{ printf (“X”) ;} } printf (“\n”) ;}How many times is the “if” statement executed?What is the output ?CMSC 104, Version 9/01 19The break Statement•The break statement can be used in while, do-while, and for loops to cause premature exit of the loop. •THIS IS NOT A RECOMMENDED CODING TECHNIQUE.CMSC 104, Version 9/01 20Example break in a for Loop#include <stdio.h>int main ( ){int i ;for ( i = 1; i < 10; i = i + 1 ){if (i == 5){ break ;}printf (“%d “, i) ;}printf (“\nBroke out of loop at i = %d.\n”, i) ; return 0 ;}OUTPUT: 1 2 3 4Broke out of loop at i = 5.CMSC 104, Version 9/01 21The continue Statement•The continue statement can be used in while, do-while, and for loops.•It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop.•THIS IS NOT A RECOMMENDED CODING TECHNIQUE.CMSC 104, Version 9/01 22Example continue in a for Loop#include <stdio.h>int main ( ){int i ;for ( i = 1; i < 10; i = i + 1 ){if (i == 5){ continue ;}printf (“%d ”, i) ;}printf (“\nDone.\n”) ; return 0 ;}OUTPUT:1 2 3 4 6 7 8


View Full Document

UMBC CMSC 104 - More Loops

Download More Loops
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view More Loops and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view More Loops 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?