Chap 2 CPP 6 TH
Chap 2 CPP 6 TH
Flow of Control
• Truth tables
– Display 2.2 next slide
• Short-circuit evaluation
– (x >= 0) && (y > 1)
– Be careful with increment operators!
• (x > 1) && (y++)
– Illegal: if (d == 0)
– Legal: if (d == Days::Wed)
– do-while
• Least flexible
• Always executes loop body at least once
– for
• Natural "counting" loop
• Flow of Control
– Recall how loops provide "graceful" and clear flow of
control in and out
– In RARE instances, can alter natural flow
• break;
– Forces loop to exit immediately.
• continue;
– Skips rest of loop body
• These statements violate natural flow
– Only used when absolutely necessary!
Copyright © 2017 Pearson Education, Ltd. All rights reserved. 2-40
Nested Loops
• Recall: ANY valid C++ statements can be
inside body of loop
• This includes additional loop statements!
– Called "nested loops"
• Requires careful indenting:
for (outer=0; outer<5; outer++)
for (inner=7; inner>2; inner--)
cout << outer << inner;
– Notice no { } since each body is one statement
– Good style dictates we use { } anyway