- ASP.NET Web Pages - Tutorial
- ASP.NET Web Pages - Adding Razor Code
- ASP.NET Web Pages - Page Layout
- ASP.NET Web Pages - Folders
- ASP.NET Web Pages - Global Pages
- ASP.NET Web Pages - HTML Forms
- ASP.NET Web Pages - Objects
- ASP.NET Web Pages - Files
- ASP.NET Web Pages - Databases
- ASP.NET Web Pages - Helpers
- ASP.NET Web Pages - The WebGrid Helper
- ASP.NET Web Pages - The Chart Helper
- ASP.NET Web Pages - The WebMail Helper
- ASP.NET Web Pages - WebSecurity Object
- ASP.NET Web Pages - Publishing The Website
- ASP.NET Web Pages - Classes
- ASP.NET Razor - Markup
- ASP.NET Razor - C# And VB Code Syntax
- ASP.NET Razor - C# Variables
- ASP.NET Razor - C# Loops And Arrays
- ASP.NET Razor - C# Logic Conditions
- ASP.NET Razor - VB Variables
- ASP.NET Razor - VB Loops And Arrays
- ASP.NET Razor - VB Logic Conditions
- ASP Tutorial
- ASP Syntax
- ASP Variables
- ASP Procedures
- VBScript Conditional Statements
- VBScript Looping
- ASP Forms And User Input
- ASP Cookies
- ASP Session Object
- ASP Application Object
- ASP Including Files
- ASP The Global.asa File
- ASP AJAX
- ASP Sending E-mail With CDOSYS
- VBScript Functions
- VBScript Keywords
- ASP Response Object
- ASP Application Object
- ASP Session Object
- ASP Server Object
- ASP ASPError Object
- ASP FileSystemObject Object
- ASP TextStream Object
- ASP Drive Object
- ASP File Object
- ASP Folder Object
- ASP Dictionary Object
- ASP AdRotator Component
- ASP Browser Capabilities Component
- ASP Content Linking Component
- ASP Content Rotator Component (ASP 3.0)
- ASP Quick Reference
- ADO Introduction
- ADO Database Connection
- ADO Recordset
- ADO Display
- ADO Queries
- ADO Sort
- ADO Add Records
- ADO Update Records
- ADO Delete Records
- ADO Demonstration
- ADO Speed Up With GetString()
- ADO Command Object
- ADO Connection Object
- ADO Error Object
- ADO Field Object
- ADO Parameter Object
- ADO Property Object
- ADO Record Object
- ADO Recordset Object
- ADO Stream Object
- ADO Data Types
ASP.NET Razor - C# Logic Conditions
The If Condition
C# lets you execute code based on conditions.
To test a condition you use an if statement. The if statement returns true or false, based on your test:
- The if statement starts a code block
- The condition is written inside parenthesis
- The code inside the braces is executed if the test is true
Example
<html>
<body>
@if (price>30)
{
<p>The price is too high.</p>
}
</body>
</html>
Practice Excercise Practice now
The Else Condition
An if statement can include an else condition.
The else condition defines the code to be executed if the condition is false.
Example
<html>
<body>
@if (price>30)
{
<p>The price is too high.</p>
}
else
{
<p>The price is OK.</p>
}
</body>
</html>
Practice Excercise Practice now
The Else If Condition
Multiple conditions can be tested with an else if condition:
Example
<html>
<body>
@if (price>=30)
{
<p>The price is high.</p>
}
else if (price>20 && price<30)
{
<p>The price is OK.</p>
}
else
{
<p>The price is low.</p>
}
</body>
</html>
In the example above, if the first condition is true, it will be executed.
If not, then if the next condition is true, this condition will be executed.
You can have any number of else if conditions.
If none of the if and else if conditions are true, the last else block (without a condition) covers "everything else"
Practice Excercise Practice now
Switch Conditions
A switch block can be used to test a number of individual conditions:
Example
var weekday=DateTime.Now.DayOfWeek;
var day=weekday.ToString();
var message="";
}
<html>
<body>
@switch(day)
{
case "Monday":
message="This is the first weekday.";
break;
case "Thursday":
message="Only one day before weekend.";
break;
case "Friday":
message="Tomorrow is weekend!";
break;
default:
message="Today is " + day;
break;
}
<p>@message</p>
</body>
</html>
The test value (day) is in parentheses. Each individual test condition has a case value that ends with a colon, and any number of code lines ending with a break statement. If the test value matches the case value, the code lines are executed.
A switch block can have a default case (default:) for "everything else" that runs if none of the cases are true.
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2025 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP