Re: If else

  • From: "Littlefield, Tyler" <tyler@xxxxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 09 Feb 2011 15:35:39 -0700

He's barely getting to if statements. getting him to do that made me happy enough, so I figured I would just leave case statements. But yes, as sina said, that would do the trick as well. So, to explain: a case statement just sets up all your conditionals (and the compiler can actually optomise more). so switch is your if (direction == bit, and then you just use case statements to set up the rest of the condition.

So:
switch(exit)
{
default:
//this is your bottom else statement, if 1a, else if b, else ...
break; //you want to break, otherwise execution just falls through
case "north":
//do something with the user going north here.
break;
case "south":
//do something else.
break;
}

HTH,
On 2/9/2011 3:30 PM, Sina Bahram wrote:

Use a switch/case statement.

Take care,

Sina

*From:*programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] *On Behalf Of *Littlefield, Tyler
*Sent:* Wednesday, February 09, 2011 5:27 PM
*To:* programmingblind@xxxxxxxxxxxxx
*Subject:* Re: If else

if (direction == "north")
{
std::cout << "You go north." << std::endl;
}
else if (direction =="south")
{
std::cout << "You go south." << std::endl;
}

On 2/9/2011 3:07 PM, Kristoffer Gustafsson wrote:

Hi.

Now I've decided that I'll learn to do things both without goto, and with it. Because then I'll maybe discover that goto is bad:)

I got one last code question today.

I need so that my program can do more than one action. for example of writing a text adventure you want many.

I've managed to put an if statement in my code. for example

if direction=="south";

{

cout<<"you go south.";

}

Now if I want to go north, how can I do that?

I tried if else, but it only says "expected primary expression before else expected.

Can you help me with this please?

/Kristoffer




--
Thanks,
Ty


--

Thanks,
Ty

Other related posts: