Jump to content
  • Advertisement
Sign in to follow this  
DanaS

Question about C# if statements for Simple Game

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

If you intended to correct an error in the post then please contact us.

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)
            {
                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?

Edited by DanaS

Share this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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
{
  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.

Edited by Alpha_ProgDes

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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. 

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!