In C#, you can use if-else
, switch-case
, and ternary conditional operators for creating conditional statements.
The if-else
statement is used to execute a block of code if the condition is true, and another block of code if the condition is false. Here is an example:
main.cs132 chars10 lines
In this example, we compare the value of the num
variable with 0. If num
is greater than 0, then the message "Number is positive." will be displayed. Otherwise, the message "Number is negative." will be displayed.
The switch-case
statement is used to select one of many code blocks to execute based on the value of an expression. Here is an example:
main.cs427 chars23 lines
In this example, the value of day
is compared with each case. If the value of day
matches one of the case values, then the corresponding message will be displayed. If the value of day
does not match any of the case values, then the default message will be displayed.
The ternary conditional operator is used to assign a value based on a condition. Here is an example:
main.cs116 chars4 lines
In this example, we check if num
is greater than 0. If num
is greater than 0, then the value of result
will be "Number is positive." Otherwise, the value of result
will be "Number is negative."
gistlibby LogSnag