Forum Topic: Need a tutorial on switch statement

(216 views • 4 replies)

This topic is 1 page long.

<< < > >>
None

Aractes

Reply To Post Reply & Quote

Posted at: 9/5/09 11:51 PM

Aractes NEUTRAL LEVEL 02

Sign-Up: 08/03/09

Posts: 10

Alright
so since i failed at my attempt in AC3, i decided to learn c++ instead.
as my first program, ive decided to make a simple calculator that add, subtracts, multiplies, and divides.
i successfully coded all the functions for the first three operations, but what i want to use for division requires the switch statement, but the book im using doesnt go very deep into it.

im asking if anyone can make a quick program for me and post the code up so that i can use it as an example.


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 9/6/09 04:21 AM

Afro-Ninja EVIL LEVEL 38

Sign-Up: 03/02/02

Posts: 13,469

the switch statement is a pretty basic construct for a lot languages- you should try googling these things first

http://www.google.com/search?q=c%2B%2B+s witch+statement

BBS Signature

None

lolomfgisuck

Reply To Post Reply & Quote

Posted at: 9/8/09 06:26 PM

lolomfgisuck EVIL LEVEL 03

Sign-Up: 07/01/03

Posts: 6,677

C#... I assume it's similar in C++. If not, blow me.

switch (variable)
{
.....case: possibleValue1
..........//Do Stuff Here
..........break;
.....case: possibleValue2
..........//Do diffrent stuff here
..........break;
}

break tells the code to exit the switch command. If you don't use break, the code will run down the line. So if possibleValue1 passes, it'll execute all that code. If there is no break, it'll move on to possibleValue2. If that's true, it'll execute it's code as well.

You could use this if you want the same code to run for the first three solutions.

IE:

switch(variable)
{
case 1:
case 2:
case 3:
//Code
break;
case 4:
//code
break;
case 5:
case 6:
//code
break;
}

Lazy response. Hope that helps.

John Rambo is my hero

BBS Signature

None

bgraybr

Reply To Post Reply & Quote

Posted at: 9/8/09 10:20 PM

bgraybr DARK LEVEL 09

Sign-Up: 06/30/08

Posts: 2,167

Think of switch statements as replacements for a lot of repetitive if statements. Instead of:

if (value == 1)
{
     print 1;
}
elif (value == 2)
{
     print 2;
}
elif (value == 3)
{
     print 3;
}

// etc...

You can have the somewhat shorter and less complicated version:

switch(value)
{
      case 1
      {
            print 1;
            break;
       }
      case 2
      {
            print 2;
            break;
      case 3
      {
            print 3;
            break;
      }
      //etc....
}

Not really a great example now that I look at it... but cases are usually shorter and better when you have a lot of if statements that test for the same thing.

The following sentence is true. The previous sentence is false. O_o PARADOX!
Linux Users Club | Linketylinklinklinklink...


None

littleMonsterGames

Reply To Post Reply & Quote

Posted at: 9/10/09 07:05 PM

littleMonsterGames DARK LEVEL 05

Sign-Up: 12/24/08

Posts: 339

RTFM: http://lmgtfy.com/?q=switch+statement
---
That said, with a switch statement, you are basically saying:

Given the variable N, do the following things in the case that the value is X, Y, Z, or etc.

switch(N){
case X: output("You chose x.");
break;
case Y: output("You chose why.");
break;
case Z: output("You chose z.");
break;
case etc: output("You chose etc.");
break;
}

Be aware that in some programming languages (maybe all, but I have no idea, and I doubt it), you can only test for numerical values with a switch statement

The LittleMonsterGames website: http://www.littlemonstergames.com - super fun, I promise :)

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 12:21 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!