In C#, if statements can be used to create conditional logic in your code. The basic syntax of an if statement is as follows:
main.cs63 chars5 lines
The condition in the parentheses is a boolean expression that evaluates to either true or false. If the condition is true, the code inside the curly braces will be executed. If the condition is false, the code inside the curly braces will be skipped.
Here is an example of an if statement that checks if a number is greater than 10:
main.cs90 chars7 lines
In this example, the condition num > 10
evaluates to true because num
is 15. Therefore, the message "The number is greater than 10." will be printed to the console.
It is also possible to add an optional else clause to an if statement, which will execute if the condition is false. Here is an example:
main.cs164 chars11 lines
In this example, the condition num > 10
evaluates to false because num
is 5. Therefore, the message "The number is less than or equal to 10." will be printed to the console.
gistlibby LogSnag