Showing posts with label If-else statement and Looping in Razor. Show all posts
Showing posts with label If-else statement and Looping in Razor. Show all posts

Friday, 8 November 2013

If-else statement and Looping in Razor in MVC


If-else statement and Looping in Razor

In my last article I discussed how we can use programming statement in Razor. In my last article we declared some variable and perform some calculation on it. In this tutorial I am going to discuss how we can use if-else statement and looping.

If-Else Statement in Razor

As we declared variable using @, similarly we can declare if else statement using @.

Note: - I am taking previous article code as an example. If you are directly reading it then kindly go through last article first.

For example:-

In this example I am taking two numbers and calculating the difference between them.

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
  
   @{
      
       int num1 = 18, num2 = 107;
       int num3;
      
    }

  @if (num1 > num2)
  {

      num3 = num1 - num2;

  }
  else
  {

      num3 = num2 - num1;
  }

    The difference between <b> @num1</b> and <b>@num2</b> is <b>@num3</b>

</body>
</html>

The output of this code



Figure 1