07 - Predicates and Intro to Loops

# 07 - Predicates and Intro to Loops - 1 Engineering 101...

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

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.

Unformatted text preview: 1 Engineering 101 Predicates and Intro to Loops Quote of the Day - Gandhi It is unwise to be too sure of one's own wisdom. It is healthy to be reminded that the strongest might weaken and the wisest might err. One more bit of selection: Switch Statements n A switch statement is used to decide between a number of cases n switch (integer_expression){ case value: expression case value: expression default: expression } n The integer expression is evaluated and the thread of execution starts from the matching case. If none matches it starts from the default. switch (weekday){ case 1: cout << “Monday”; break; case 2: cout << “Tuesday”; break; case 3: cout << “Wednesday”; break; case 4: cout << “Thursday”; break; case 5: cout << “Friday”; break; default: cout << “Weekend”; } breaks are needed. Otherwise switch drops through to next expression. One more bit of selection: Switch Statements Using operators to construct conditional expressions n Start on some given day. While the day is not a weekend, add 8 hours to my timeclock and work, otherwise rest. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Su M T W Th F Sa 20 21 22 23 24 25 26 27 28 29 30 31 Using operators to construct conditional expressions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Su M T W Th F Sa 20 21 22 23 24 25 26 27 28 29 30 31 int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; int timeclock=0, day = 9; firstday tells which day of the week the first day of the month falls upon 2 Using operators to construct conditional expressions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Su M T W Th F Sa 20 21 22 23 24 25 26 27 28 29 30 31 int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; int timeclock=0, day = 9; How do we determine whether it is a weekday? Using operators to construct conditional expressions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Su M T W Th F Sa 20 21 22 23 24 25 26 27 28 29 30 31 int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; int timeclock=0, day = 9; (firstday+day-1) % 7 != Sun When is the above true? Using operators to construct conditional expressions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Su M T W Th F Sa 20 21 22 23 24 25 26 27 28 29 30 31 int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; int timeclock=0, day = 9; while ( (firstday+day-1) % 7 != Sun and (firstday+day-1) % 7 != Sat ) { timeclock = timeclock + 8; cout << “I’m working.” << endl; day = day + 1; } This is only true on weekdays Using operators to construct conditional expressions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Su M T W Th F Sa 20 21 22 23 24 25 26 27 28 29 30 31 int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; int timeclock=0, day = 9; while ( (firstday+day-1) % 7 != Sun and (firstday+day-1) % 7 != Sat ) { timeclock = timeclock + 8; cout << “I’m working.” << endl; day = day + 1; } What is the initialization?...
View Full Document

## This note was uploaded on 01/30/2012 for the course ENGR 101 taught by Professor Ringenberg during the Winter '07 term at University of Michigan.

### Page1 / 8

07 - Predicates and Intro to Loops - 1 Engineering 101...

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

View Full Document
Ask a homework question - tutors are online