Conditions IFElse if If condition statement Else if condition statement Else if

Conditions ifelse if if condition statement else if

This preview shows page 18 - 21 out of 21 pages.

Conditions IF…Else if If (condition) statement; Else if (condition) statement; Else if (condition) statement; Else if (condition) statement; Else if (condition) statement;
Image of page 18
Switch Control substitute for long if statements that compare a variable to several "integral" values. expression is any legal C++ expression . statements are any legal C++ statements or block of statements. switch evaluates expression and compares the result to each of the case values. evaluation is only for equality; relational operators may not be used here, nor can Boolean operations. switch ( expression ) { case valueOne: statement ; break; case valueTwo: statement ; break; .... case valueN: statement ; break; default: statement ; }
Image of page 19
Switch If one of the case values matches the expression, execution jumps to those statements and continues to the end of the switch block, unless a break statement is encountered. If nothing matches, execution branches to the optional default statement. If there is no default and there is no matching value, execution falls through the switch statement and the statement ends. switch ( expression ) { case valueOne: statement ; break; case valueTwo: statement ; break; .... case valueN: statement ; break; default: statement ; }
Image of page 20
int input; cout<<"1. Play game\n"; cout<<"2. Load game\n"; cout<<"3. Play multiplayer\n"; cout<<"4. Exit\n"; cout<<“Enter Your Selection: "; cin>> input; switch ( input ) { case 1: // Note the colon, not a semicolon Cout<<“press enter to play game”; break; case 2: Cout<<“enter you name to load game”; break; case 3: Cout<<“enter all players names to load game”; break; case 4: Cout<<“press esc to exit game”; break; Default: cout<<" bad input, quitting\n"; break; }
Image of page 21

You've reached the end of your free preview.

Want to read all 21 pages?

  • Fall '18
  • Zia Ul Rehman
  • Programming constructs, Bitwise operation

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes