Advertisement

Question about C# if statements for Simple Game

Started by October 07, 2015 10:44 AM
9 comments, last by Nypyren 9 years, 3 months ago

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)
            {
                rightNumber = int.Parse(Console.ReadLine());

                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.ReadLine();
            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?

If you make if-statement which is more than 1 line, you'll need to add brackets.

Works:


if(someCondition)
    oneLiner++;

Doesn't work:


if(someCondition)
    oneLiner++;
    anotherLine--;

Works:


if(someCondition) {
    oneLiner++;
    anotherLine--;
}

So your if-else should look like this (I used brackets on single line else also just for consintancy) :


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!");
    }
}
Advertisement

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.

Hello to all my stalkers.

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!!!

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.

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
{
  inputtedNumber = int.Parse(Console.ReadLine());

  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.ReadLine();
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.

Beginner in Game Development?  Read here. And read here.

 

Advertisement

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.

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.
My current game project Platform RPG

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.

My golden rule with brackets on while,if etc is that always assume they are never optional and put them in. So it adds a couple of extra characters to the code and 1-2 lines depending on your bracket style but it is explicit and harder to forget if you add that extra line later :)

This topic is closed to new replies.

Advertisement