Programmer's Picnic · Learn With Champak
Razor course · Lesson 2 of 4

Variables, conditions and loops

Read the explanation, run the sample, and try the exercise.

Variables and display

@{
    string student = "Asha";
    int score = 87;
}
<h2>Hello @student</h2>
<p>Score: @score</p>

Conditions

@if (score >= 40)
{
    <p>Passed</p>
}
else
{
    <p>Try again</p>
}

Loops

@{
    var topics = new[] { "C#", "HTML", "Razor" };
}
<ul>
@foreach (var topic in topics)
{
    <li>@topic</li>
}
</ul>

Exercise: change the topics and display four items.

← PreviousNext lesson →