Friday 20 November 2015

Use If statement in asp.net

Introduction:

I going to explain here how to use if statement in asp.net with c# code.

Explanation:

If condition is used to check the condition and execute the statement if condition is  true  then execute statement of true else if condition is false then execute false statement as below syntax.

if (condition)
statement(s)
else
statement(s)

If more than one statement is to be executed as part of either condition, these statements will need to be joined together into a block using curly braces ({ ... })



bool is Zero;
if (i == 0)
{
isZero = true;
Console.WriteLine(“i is Zero”);
}
else
{
isZero = false;
Console.WriteLine(“i is Non-zero”);
}

Visual Basic developers should note that C# does not have any statement corresponding to Visual Basic’s EndIf. Instead, the rule is that each clause of an if contains just one statement. If you need more than one statement, as in the above example, you should enclose the statements in braces, which will cause the whole group of statements to be treated as a single block statement.

If we want to, we can use an if statement without a final else statement. We can also combine else if clauses to test for multiple conditions.
using System;

class MainEntryPoint
{
static void Main(string[] args)
{
Console.WriteLine(“Type in a string”);
string input;
input = Console.ReadLine();
if (input == “”)
{
Console.WriteLine(“You typed in an empty string”);
}
else if (input.Length < 5)
{
Console.WriteLine(“The string had less than 5 characters”);
}
else if (input.Length < 10)
{
Console.WriteLine(“The string had at least 5 but less than 10
characters”);
}
Console.WriteLine(“The string was “ + input);
}
}
}

There is no limit to how many else if’s we can add to an if clause. You’ll notice that in the previous example, we declare a string variable called input, get the user to enter
text at the command line, feed this into input, and then test the length of this string variable. The code also shows us how easy string manipulation can be in C#. To find the length of input, for example, use input.Length.

One point to note about if is that we don’t need to use the braces if there’s only one statement in the conditional branch:

if (i == 0)
Console.WriteLine(“i is Zero”); // This will only execute if i == 0
Console.WriteLine(“i can be anything”); // Will execute whatever the
// value of i

However, for consistency, many programmers prefer to use curly braces whenever they use an if statement.

The if statements we have presented also illustrate some of the C# operators that compare values. Note in particular that, like C++ and Java, C# uses == to compare variables for equality. Do not use = for this purpose. A single = is used to assign values.

In C#, the expression in the if clause must evaluate to a Boolean. C++ programmers should be particularly aware of this; unlike C++, it is not possible to test an integer (returned from a function, say) directly. In C#,we have to convert the integer that is returned to a Boolean true or false, for example by comparing the value with zero or with null:

if (DoSomething() != 0)
{
// Non-zero value returned
}
else
{
// Returned zero
}

This restriction is there in order to prevent some common types of run-time bugs that occur in C++. In particular, in C++ it was common to mistype = when == was intended, resulting in unintentional assignments. In C# this will normally result in a compile-time error, since unless you are working with bool values, = will not return a bool.

 

No comments:

Post a Comment