Sign in to follow this  
DanaS

Question about C# if statements for Simple Game

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

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

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
What frob says is correct. There are two things that will read your code: Computers and Humans.

When a computer reads your code, it follows a strict rule-set called a "grammar". If you've seen MadLibs, which are sentences with blanks for you to fill in your own words, and under the blank it says (adjective) or whatever, grammars are defined like that, recursively. A fragment of this grammar might look like this, where the things in quotes are actual text you would type in, and the non-quoted parts are places where you can type in anything which matches that particular grammar rule:
 
statement -> "if" "(" expression ")" statement
statement -> "{" statement* "}"
statement -> expression ";"

expression -> expression "&&" expression
expression -> expression "||" expression
expression -> "-" expression
expression -> "!" expression
expression -> identifier
expression -> int_literal
expression -> bool_literal

identifier -> [A-Za-z_][0-9A-Za-z_]*
int_literal -> [0-9]+
bool_literal -> "false" | "true"

// etc.
But when a human reads your code, they don't follow the grammar rules exactly. They see the layout and indentation first, keywords and variable names next, and only start following the grammar if they're looking for bugs.


When the computer's and human's interpretation of the code differs, that's a bug. Edited by Nypyren

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this