are int or enum values casey statement1 default is

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: -else and switch •  Standard iteration (loops): do-while, while-do, and for •  A for-each variant (differs from C++) CPSC 324 ‐‐ Spring 2010 40 20 1/14/10 If-then-else if (boolean condition) statement •  Example: if (yourSales >= target) bonus = 100; –  You can also add braces: If there is only one statement in the block, then you don’t need braces if (yourSales >= target) { bonus = 100; } What type must condition be? Can it be an int? CPSC 324 ‐‐ Spring 2010 41 If-then-else if (boolean condition) { statement1; statement2; …; } •  Example: if (yourSales >= target) { bonus = 100; raise = true; } For multiple statements in a block, you must use braces CPSC 324 ‐‐ Spring 2010 42 21 1/14/10 If-then-else if (boolean condition) block1 else block2 •  Example: if (yourSales >= target) { bonus = 100; } else { bonus = 0; } Do we need the braces here? CPSC 324 ‐‐ Spring 2010 43 If-then-else if (boolean condition) block1 else if block2 •  Example: if (yourSales >= target) { bonus = 100; } else if (yourSales >= (target/2)){ bonus = 50; } CPSC 324 ‐‐ Spring 2010 44 22 1/14/10 If-then-else if (boolean condition) block1 else if block2 •  Will this compile? if (yourSales >= target) bonus = 100; else if (yourSales >= (target/2)) bonus = 50; Yes! These blocks have only one statement each CPSC 324 ‐‐ Spring 2010 45 If-then-else if (boolean condition) block1 else if block2 else block2 •  Example: Any number of else if’s if (yourSales >= target) { are allowed bonus = 100; } else if (yourSales >= (target/2)) { bonus = 50; } How many else’s else { can there be? bonus = 0; } CPSC 324 ‐‐ Spring 2010 46 23 1/14/10 Switch Statements •  I personally try never to use these, but they have been know to come in handy … choice is an integer (variable) switch (choice) { or enumerated constant (we’ll case x: talk about these...
View Full Document

Ask a homework question - tutors are online