{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect07_Loops

# lect07_Loops - Loops Loops CSIS1117 Computer Programming Co...

This preview shows pages 1–12. Sign up to view the full content.

Loops SIS1117 Computer Programming CS S Co pute og a g 1 c1117 lecture 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ontent Content hile loop While loop Type casting For loop Increment/decrement Nested loop o- hile loop Do while loop Break statement Switch statement c1117 lecture 7 2
ooping constructs Looping constructs onsider a problem: we want to find the m f Consider a problem: If we want to find the sum of 3 input positive integers. You may write something like: int total =0; int x; cout << "Enter 3 positive integers: "; cin >> x; total = total + x; in >> x; total = total + x; c; aa ; cin >> x; total = total + x; cout << “The sum is ” << total << endl; How about finding the sum of an arbitrary no. of ositive tegers? c1117 lecture 7 3 positive integers?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ooping constructs Looping constructs ow we want to find the m f a sequence of Now we want to find the sum of a sequence of positive integers. The user can type as many input as he wants until zero is entered. Then you may want something like: int total =0; int x; cin >> x; if( x > 0) { total = total + x; cin >> x; } if( x > 0) total = total + x; cin >> x; } We don’t know how { total total + x; cin >> x; } if( x > 0) { total = total + x; cin >> x; } many times it takes!! c1117 lecture 7 4 ...
hile loop While loop hile loop elp to execute ne r group f While loop help to execute one or a group of statements (the body ) repeatedly while a ondition tisfied condition is satisfied . The previous program can be written as: int total =0; int x; cin >> x; Condition while(x > 0){ total = total + x; in >> x; Body cin >> x; } cout << "The sum is: " << total << endl; c1117 lecture 7 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
hile loop While loop e um- oop.cc see sum loop.cc In evaluating a while loop the condition is evaluated first If the result is false , the body of the loop is skipped and e hile loop rminated the while loop is terminated . If the result is true , the body is executed once , then the hole hile loop executed gain whole while loop is executed again . Each round of execution of a loop is called an eration iteration . c1117 lecture 7 6
hile loop While loop hile condition e while body is executed only if While condition the while body is executed only if the condition is evaluated to be true while ( condition ) { hile condition ) group_statement1; group_statement2; while ( condition ) single_statement1; ext statement; .... } next_statement; next_statement; hile body can be a single or a block statement(s) c1117 lecture 7 7 While body it can be a single or a block statement(s).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
hile loop While loop int total =0; int x; cin >> x; 10 x while(x > 0){ total = total + x; in >> x; total cin >> x; } 10 Main memory ommand prompt c1117 lecture 7 8 Command prompt
hile loop While loop true! int total =0; int x; cin >> x; 10 x while(x > 0){ total = total + x; in >> x; total cin >> x; } ondition return 10 Main memory Condition return true, the body is executed. ommand prompt c1117 lecture 7 9 Command prompt

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
hile loop While loop int total =0; int x; cin >> x; 15 x while(x > 0){ total = total + x; in >> x; 10 total cin >> x; } ead in the 2 nd put 10 Main memory ommand prompt Read in the 2 input 15 c1117 lecture 7 10 Command prompt
hile loop While loop true!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 48

lect07_Loops - Loops Loops CSIS1117 Computer Programming Co...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online