Programming Logic: Execute conditional-based code.
If condition
C # allows you to execute conditional-based code.
If you want to test a condition, you can use the if statement . The IF statement returns TRUE or FALSE based on your test:
- If statement start code block
- The condition is in parentheses
- If the condition is true, execute the code in curly braces
@{var price=50;} Else conditionAn If statement can contain an else condition .
The else condition defines the code that executes when the condition is false.
@{var price=20;}
Note: In the above example, if the price is not greater than 30, the remaining statements are executed.
Else If ConditionYou can test multiple conditions with the else if condition :
@{var price=25;}
In the example above, if the first condition is true, the first block of code is executed.
Otherwise, if the next condition is true, the second block of code is executed.
You can set any number of else if conditions.
If the IF and else if conditions are not true, the last else code block is executed.
Switch conditionA switch code block can be used to test a series of specific conditions:
@{var Weekday=datetime.now.dayofweek;var Day=weekday. ToString (); var message= "";}
The test value (day) is in parentheses. Each specific test condition begins with a case keyword, ends with a colon, and then allows any number of lines of code to end with a break statement. If the test value matches the case value, the line of code is executed.
The switch code block sets a default case (default:) for the remainder, allowing code to be executed when all cases are not true.
ASP. NET MVC Razor Syntax (3)