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

Post on 04-Jan-2016

224 views 0 download

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

CSIS 113A Lecture 3

Conditional & Switch

Glenn Stevenson CSIS 113A MSJC

Glenn Stevenson CSIS 113A MSJC

Operator(s) Description Associativity:: Scope resolution (C++ only) N/A++ --()[].->

Postfix increment and decrementFunction callArray subscriptElement selection by referenceElement selection by pointer

Left-to-Right

++ --+ -! ~(type)*&sizeofnew delete

Prefix increment and decrement

Unary plus and minusLogical NOT and bitwise one's complementType castIndirection (dereference)Address-of (reference)Size-ofDynamic memory (de-)allocation (C++ only)

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 ORc?t:f Ternary conditional Right-to-Left

=+= -=*= /= %=<<= >>=&= ^= |=

Direct assignmentAssignment by sum and differenceAssignment by product, dividend, and remainderAssignment by bitwise shiftAssignment by bitwise AND, XOR, and OR

, Comma Left-to-Right

Operator Precedence

Conditional Operator

• Shortcut for an if else

Glenn Stevenson CSIS 113A MSJC

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;

}

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

Switch Statement

Glenn Stevenson CSIS 113A MSJC

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

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

An Example, A simple menu

Glenn Stevenson CSIS 113A MSJC

#include <iostream>using namespace std;int main(){int selection;    cout << "Select a program to run " << endl;    cout << "1. Run Word " << endl;   cout << "2. Run Excel " << endl;   cout << "3. Run Powerpoint " << endl;   cout << "4. Exit Program " << endl;   cin >> 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; }

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

Check Vowel

Glenn Stevenson CSIS 113A MSJC

#include <iostream>using namespace std;int main(){char c;   cout << "Enter a character and I will check to see if it is a vowel " << endl;   cin >> 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;}

Switch Pitfall

Forgetting the break; No compiler error Execution simply ‘falls thru’ other cases until

break;

Glenn Stevenson CSIS 113A MSJC