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

This topic is closed to new replies.

Advertisement