Talk:C++/Conditional Statements

awesome :)

Examples
Could you please add some examples for the switch case? I'm not really sure on how to use it.

--Timmeh 1250 23:51, 21 January 2009 (UTC)

Switch statements are often used in place of massive if/else blocks, both to shorten them, and to make them more readable. For example, say you're writing code for a game, in which the player has four possible states - dead, crouching, standing, and jumping - that correspond to four integers respectively (I.E. 0=dead, 1=crouching, etc.) A piece of code telling the game what to do when the player does x action, may need to do something different, depending on the players current state. This could be done with if statements, like so:

if (state==0) { //Dead code here } if (state==1) { //Crouching code here } if (state==2) { //Standing code here } if (state==3) { //Jumping code here }

However, it would be easier and more efficient to use a switch statement, like so:

switch (state) { case 0: //Dead code here break; case 1: //Crouching code here break; case 2: //Standing code here break; case 3: //Jumping code here break;

Sorry for any C++ Syntax or terminology errors, I've been using computer logic for some time now, but I'm still learning C++.

Real world Examples?
I feel like it'd be nice to have a pseudo-real world example in here, just to relate to the reader. For example, Have the user input how much money they have in their wallet. If they have over $200 in it, have the console say "That's a lot of money!" Otherwise, it would say "That's neat." .....don't judge me. My right brain has been feeling weak lately. =) 64.118.9.189 23:08, 17 January 2010 (UTC).