12
Lecture 01b: C++ review Topics: • if's and switch's while and for loops

Lecture 01b: C++ review Topics: if's and switch's while and for loops

Embed Size (px)

DESCRIPTION

if "0 of 1" version if (cond) code-block "1 of n" version if (cond1) code-block1 else if (cond2) code-block2 … else code-blockN "0-1 of n" version Like "1 of n" but without the else "n of m" version if (cond1) code-block1 if (cond2) code-block2 …

Citation preview

Page 1: Lecture 01b: C++ review Topics: if's and switch's while and for loops

Lecture 01b: C++ review

Topics:• if's and switch's• while and for loops

Page 2: Lecture 01b: C++ review Topics: if's and switch's while and for loops

Part I: Conditionals

• A means to select (possibly) one of (possibly) many alternative code blocks.– A single statement– Or a block of code surrounded by curly braces

• If / else structures use a conditional (boolean expression)

• Switch's are more limited and can only compare the contents of a single (numeric) variable.– They offer no speed advantage– They are uglier than if/else's

Page 3: Lecture 01b: C++ review Topics: if's and switch's while and for loops

if• "0 of 1" version if (cond) code-block

• "1 of n" version if (cond1) code-block1 else if (cond2) code-block2 … else code-blockN

• "0-1 of n" version• Like "1 of n" but without the else

• "n of m" version

if (cond1) code-block1 if (cond2) code-block2 …

Page 4: Lecture 01b: C++ review Topics: if's and switch's while and for loops

Conditions

• Simplex < 5y != 3health == 100.0f // Be careful w/ float's

• Compoundx < 5 && y != 3!strcmp(s, "ABC") && age < 65

Page 5: Lecture 01b: C++ review Topics: if's and switch's while and for loops

(Ugly) switch statementsint x;// x is filled in elsewhere...switch(x){

case 5:code // no need for bracesbreak;case -1:code // note: no break; "fall-through"case -2:codebreak;default:code // Executed if none of the othersbreak;

}

Page 6: Lecture 01b: C++ review Topics: if's and switch's while and for loops

Pitfalls

• A condition can be a boolean or…– A numeric value (0 = false, non-zero = true)int x;// x is modifiedif (x) // will be executed if x != 0{ /* … */ }

• Assignment statements– Are allowable, and are sometimes useful– But…can be an error if you meant to do comparison (==)if (x = 15) {} // Changes x and block is always executed

• Not enclosing a block in curly bracesif (health < 15)speed = 1.2f;activateRage(); // Always executed, regard. of health

Page 7: Lecture 01b: C++ review Topics: if's and switch's while and for loops

Part II: Repetition

• A way to repeat a code-block (possibly) multiple times

• 3 variants:– while: executed 0-n times– do-while: executed 1-n times– for: shorthand for common while loops

Page 8: Lecture 01b: C++ review Topics: if's and switch's while and for loops

While

int i = 5, x=0;while (i < n && x < 100){

// Do something// Get set up for next iterationi += 2;x += i * i;

}

Page 9: Lecture 01b: C++ review Topics: if's and switch's while and for loops

for

// Same effect as last slidefor (int i = 5, x = 0; i < n && x < 100; i+= 2, x += i * i){

// Do something}

Page 10: Lecture 01b: C++ review Topics: if's and switch's while and for loops

Do-While

int choice;

do{

cout << "Enter choice (9=quit): ";cin >> choice;// Do something else

} while (choice != 9);

Page 11: Lecture 01b: C++ review Topics: if's and switch's while and for loops

Break and continue

• Allow you to modify a loop’s behaviorbreak; // End current iteration + leave loopcontinue; // End current iteration

• Technically, neither is necessary– It can simplify the code a bit, though.

• [Intentionally infinite loops]• [Example: total numbers until user enters a

negative]

Page 12: Lecture 01b: C++ review Topics: if's and switch's while and for loops

Mini-Lab

• [FizzBuzz]• For points or just for “fun”?