Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC.

Similar presentations


Presentation on theme: "CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC."— Presentation transcript:

1 CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC

2 Operator(s)DescriptionAssociativity ::Scope resolution (C++ only) N/A ++ -- () []. -> Postfix increment and decrement Function call Array subscript Element selection by reference Element selection by pointer Left-to-Right ++ -- + - ! ~ (type) * & sizeof new delete Prefix increment and decrement Unary plus and minus Logical NOT and bitwise one's complement Type cast Indirection (dereference) Address-of (reference) Size-of Dynamic memory (de-)allocation (C++ only)one's complement Right-to-Left.* ->*Pointer to member (C++ only) Left-to-Right * / % Multiplication, division, and modulus (remainder) + -Addition and subtraction >Bitwise left shift and right shift >= Relational “less than” and “less than or equal to” Relational “greater than” and “greater than or equal to” == !=Relational “equal to” and “not equal to” &Bitwise AND ^Bitwise XOR (exclusive or) |Bitwise OR (inclusive or) &&Logical AND ||Logical OR c?t:fc?t:fTernaryTernary conditional Right-to-Left = += -= *= /= %= >= &= ^= |= Direct assignment Assignment by sum and difference Assignment by product, dividend, and remainder Assignment by bitwise shift Assignment by bitwise AND, XOR, and OR,CommaLeft-to-Right Operator Precedence

3 Conditional Operator Shortcut for an if else Glenn Stevenson CSIS 113A MSJC

4 An example Glenn Stevenson CSIS 113A MSJC bonusAmt = amountSold <= 35000 ? amountSold *.035 : 100 + amountSold *.075; Is equivalent to: If(amountSold <= 35000) { bonusAmt = amountSold *.035; } else { bonusAmt = 100 + amountSold *.075; }

5 Multiple Choice Nested if & ladder style if / else / if –Provide a way to do multi-way branching Can make code convoluted –Hard to read switch might be a better choice Glenn Stevenson CSIS 113A MSJC

6 Switch Statement Glenn Stevenson CSIS 113A MSJC

7 Switch Continued Uses an integer expression to evaluate switch –Works like a telephone operator or railway switch –Looks for matching case Control of code branches –Can have any number of statements following each case label –break statement is used to end each case –Default statement works like else part of ladder style if / else /if statements Glenn Stevenson CSIS 113A MSJC

8 How it Works 1. Integer expression is evaluated 2. list of labeled constants is scanned –Looking for an exact match If a match is found c++ begins executing the first line of code following the matching case label Each line of code is executed in order –Until a break statement is found or all of the statements in the switch body have been executed 3. if no match is found –Execution jumps to the statement following the default keyword default is optional. If no default code resumes on the first line of code following the last brace of the switch statement. Glenn Stevenson CSIS 113A MSJC

9 An Example, A simple menu Glenn Stevenson CSIS 113A MSJC #include using namespace std; int main() { int selection; cout > selection; switch(selection) { case 1: cout << "Running word " << endl; break; case 2: cout << "Running Excel " << endl; break; case 3: cout << "Running Powerpoint " << endl; break; case 4: cout << "Good Bye " << endl; exit(0); default: cout << "Invalid Entry " << endl; } return 0; }

10 The break statement Omitting the break statement –Causes code to fall through to the next case Sometimes this is wanted Allows you to execute the same code for multiple constant conditions –Lets look at an example Glenn Stevenson CSIS 113A MSJC

11 Check Vowel Glenn Stevenson CSIS 113A MSJC #include using namespace std; int main() { char c; cout > c; switch(c) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'o': case 'O': case 'u': case 'U': cout << c << " is a vowel " << endl; break; default: cout << c << " is not a vowel " << endl; } return 0; }

12 Switch Pitfall  Forgetting the break;  No compiler error  Execution simply ‘falls thru’ other cases until break; Glenn Stevenson CSIS 113A MSJC


Download ppt "CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC."

Similar presentations


Ads by Google