Download presentation
Presentation is loading. Please wait.
Published byAudrey Boyd Modified over 8 years ago
1
CONTROL STATEMENTS if-then, if-then-else, switch/case For, While, Do-while, goto Break, continue
2
if-then int price = 8; if (day == “Tuesday”) { price = price/2; // half price on Tuesday } The price is 8 but then if today is Tuesday, the price becomes 4. There is no “then” in the if-then statement. The part in purple is considered the if statement and the part in blue is considered the “then” part.
3
if-then-else if (hour 6) { time = “happy hour”; } else { time = “regular prices”; } The price is 8 but then if today is Tuesday, the price becomes 4. There is no “then” in the if-then statement. The part in purple is considered the if statement and the part in blue is considered the “then” part. The part in green is considered the else part
4
Compound if-then-else if (today == “Monday”) { special = “burgers”; } else { if (today == “Tuesday”) { special = “Chicken Wings”; } else { if (today == “Wednesday”) { special = “Pizza”; }
5
Switch (aka Jump to Label) switch (location) { case “Long Branch“ : city = “Long Branch”; case “New Jersey“ : state = “New Jersey; case “USA” : country = “USA” }
6
For loop format for (initialization; termination; increment) { statement(s) } The initialization expression initializes the loop; it's executed once, as the loop begins. When the termination expression evaluates to false, the loop terminates. The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
7
For loop example sum = 0; for(int i=1; i<11; i++) { sum = sum + i; } // sum will equal 1+2+3+4+5+6+7+8+9+10 = 55
8
While loop format initialization; while (termination) { statement(s) increment } The initialization expression initializes the loop; it's executed once, as the loop begins. When the termination expression evaluates to false, the loop terminates. The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
9
Do-While loop format initialization; do { statement(s) increment } while (termination) The initialization expression initializes the loop; it's executed once, as the loop begins. When the termination expression evaluates to false, the loop terminates. The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
10
Break firstX = 0; for (int i = 0; i < max; i++) { // interested only in p's firstX++; if (A[i] == ‘X') break; // no point in going further }
11
Labeled Break Outer: for(int outer=0; outer < 10 ; outer++) { Inner: for(int inner=0; intInner < 10; inner++) { if(A[outer][inner] == 100) { found = true; break Outer; } }
12
Continue (skip remainder of loop) firstX = 0; for (int i = 0; i < max; i++) { // interested only in p's firstX++; if (A[i] == ‘X') continue; // skip doLotsOfStuff but keep looping doLotsOfStuff(); }
13
Switch with breaks (aka Compound if-then-else) switch (today) { case “Monday“ : special = “Burgers; break; case “Tuesday“ : special = “Chicken Wings; break; case “Wednesday” : special = “Pizza” break; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.