show_looping_structs

show_looping_structs - CHAPTER 3 C++ Looping Structures...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CHAPTER 3 C++ Looping Structures March 28, 2008 Control Structures Sequence, Selection, Repetition Flowchart Notation Sequence Selection Repetition statement; statement; { //opening } // closing ; // empty statement; Test Boolean Yes No sequence; Test Boolean Yes No sequence; Repetition Statements Repetition Statements Repetition Statements while Loop The while Loop No Yes while( expr ) { } sequence ; 1 s t r i n g i n p u t ; 2 c o n s t s t r i n g THE_END ( "end" ) ; 3 4 wh i l e ( i n p u t != THE_END ) { 5 c o u t << "Enter end to stop:" << e n d l ; 6 c i n >> i n p u t ; 7 } 8 9 c o u t << "stopped!" << e n d l ; Repetition Statements while Loop The while Loop 1 s t r i n g i n p u t ; 2 c o n s t s t r i n g THE_END ( "end" ) ; 3 4 wh i l e ( i n p u t != THE_END ) { 5 c o u t << "Enter end to stop:" << e n d l ; 6 c i n >> i n p u t ; 7 } 8 9 c o u t << "stopped!" << e n d l ; <<Interactive Program>> RUN EDIT while_example.cxx Repetition Statements do-while(); Loops The do-while(); Loops 1 d o u b l e i n p u t ( 0 ) ; 2 3 do { 4 c o u t << "Enter a decimal within " \ 5 << "(0,1]:" << e n d l ; 6 c i n >> i n p u t ; 7 8 } wh i l e ( i n p u t <=0 | | i n p u t >1 ) ; 9 10 c o u t << "Thanks , " << i n p u t << \ 11 " is within (0,1]." << e n d l ; 12 / ** 13 * n o t e t h e s p a c i n g f o r r e a d a b i l i t y i n 14 * w h i l e ( ) t e s t 15 * * / Yes No while( expr ); } do{ sequence ; Repetition Statements do-while(); Loops The do-while(); Loops 1 d o u b l e i n p u t ( 0 ) ; 2 3 do { 4 c o u t << "Enter a decimal within " \ 5 << "(0,1]:" << e n d l ; 6 c i n >> i n p u t ; 7 8 } wh i l e ( i n p u t <=0 | | i n p u t >1 ) ; 9 10 c o u t << "Thanks , " << i n p u t << \ 11 " is within (0,1]." << e n d l ; 12 / ** 13 * n o t e t h e s p a c i n g f o r r e a d a b i l i t y i n 14 * w h i l e ( ) t e s t 15 * * / <<Interactive Program>> RUN EDIT dowhile_example.cxx Repetition Statements do-while(); Loops while == do-while ? Consider these two loops do they run the same? 1 d o u b l e sum ( 0 ) ; 2 d o u b l e i n p u t ( 0 ) ; 3 4 c o u t << "Enter a (real) number: " << f l u s h ; 5 c i n >> i n p u t ; 6 wh i l e ( i n p u t > 0 ) { 7 sum += i n p u t-- ; 8 } 9 c o u t << "Calculated sum: " << sum << e n d l ; <<Interactive Program>> RUN EDIT while_not_dowhile_while.cxx 1 d o u b l e sum ( 0 ) ; 2 d o u b l e i n p u t ( 0 ) ; 3 4 c o u t << "Enter a (real) number: " << f l u s h ; 5 c i n >> i n p u t ; 6 do { 7 sum += i n p u t-- ; 8 } wh i l e ( i n p u t > 0 ) ; 9 c o u t << "Calculated sum: " << sum << e n d l ; <<Interactive Program>> RUN EDIT while_not_dowhile_dowhile.cxx Repetition Statements for Loop The for Loop expr { } sequence ; increment initialize No Yes 1 i n t i ; 2 f o r ( i = A ; i <= Z ; i ++ ) { 3 c o u t << ( c h a r ) i ; 4 i f ( ! ( i % 12 ) ) { 5 c o u t << e n d l ; 6 } 7 } 8 c o u t << e n d l ; ABCDEFGH IJKLMNOPQRST UVWXYZ RUN EDIT for_all_caps.cxx The i is called the loop counter . Repetition Statements...
View Full Document

Page1 / 32

show_looping_structs - CHAPTER 3 C++ Looping Structures...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online