• 9
• 9
• 11
• 13
• 9

# Question about C# if statements for Simple Game

This topic is 925 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, so I've done all my programming in C++ up to this point and the syntax and logic surround IF ELSE statements seems to be a bit different.  I'm learning the rules around C# now, and I can't seem to figure out why my little 20 line "pick a number" game isn't working.  Maybe someone can clue me in?  Here's the code.

            Console.WriteLine("Let's play a game!  I'm looking for a number 1-10");
int rightNumber = -100;
int points = 100;

while (rightNumber < 3 || rightNumber > 3)
{

if (rightNumber > 3)
Console.WriteLine("Too high!");
points = points - 1;
else
if (rightNumber < 3)
Console.WriteLine("Too Low!");
points = points - 1;
else
Console.WriteLine("You go it!");

}

Console.WriteLine("Your points equal " + points);

So basically, it's a loop that continues to loop until you select the correct number.  If you get the wrong number, you are notified and then a point is subtracted from your score.  I want each section of my IF ELSE statement to do two things: 1. Notify the player that they were too high/low/correct and 2: if they were too low or two high, a point is added to their score.  In C++, IF a statement was true, you could have it generate multiple effects.  For some reason, I'm having a hard time making that happen in C#.  Can anyone clue me in?

Edited by DanaS

##### Share on other sites

Note that this is the same as in C++.

This can cause quite interesting bug, a recent high-profile one being Apple's "goto" bug.

##### Share on other sites

Ohhh, I see.  Yeah, so it appears that in C++ you always need the brackets whether its one line or not.  In C# you only need brackets if there is more than one line, is that the idea?   I realize this is true also for loops, etc.

Edit: And Thanks!!!

Edited by DanaS

##### Share on other sites
You don't need to always use brackets in C++ either, those bracket rules I said above apply for C++ also.

And yes, those brackets rules apply for while and for loops also.

##### Share on other sites
Console.WriteLine("Let's play a game! I'm looking for a number 1-10");
int rightNumber = 3;
int inputtedNumber = 0;
int points = 100;

do
{

if (rightNumber < inputtedNumber) {
Console.WriteLine("Too high!");
points = points - 1;
}
else if (rightNumber > inputtedNumber) {
Console.WriteLine("Too Low!");
points = points - 1;
}
else {
Console.WriteLine("You got it!");
}
} while (rightNumber != inputtedNumber && points > 0)

Console.WriteLine("Your points equal " + points);

Sorry, there were several things bothering me about the code and I just felt compelled to provide my own version.

Edited by Alpha_ProgDes

##### Share on other sites

You don't need to always use brackets in C++ either, those bracket rules I said above apply for C++ also.

And yes, those brackets rules apply for while and for loops also.

Hm, I guess that just means I was using brackets in C++ even when I didn't need to.

##### Share on other sites

Hm, I guess that just means I was using brackets in C++ even when I didn't need to.

Some style guides suggest you always use brackets to avoid the bug you are running into. Adding brackets doesn't really have any downsides either.

##### Share on other sites

Ohhh, I see.  Yeah, so it appears that in C++ you always need the brackets whether its one line or not.  In C# you only need brackets if there is more than one line, is that the idea?   I realize this is true also for loops, etc.

Nope.

It follows the pattern:

[Control Statement] [Action]

The action is whatever single thing follows the control statement.  It can be a single expression, or it can be a block of expressions surrounded by curly brackets.

if(whatever) single_action;

if(whatever) { action; action; action; }

This pattern is true in all the languages in the extended language family; C, C++, Java, C#, JavaScript, Pascal, Ada, Scheme, and many more.

Many programming style guides recommend that for consistency you always use brackets for the action around control structures, even for a single action.