2 Conditional Statement C#
2 Conditional Statement C#
You can use these conditions to perform different actions for different decisions.
1. The if Statement
Use the if statement to specify a block of C# code to be executed if a condition is True
other block will not executed if the condition is false.
Syntax
if (condition)
{
// block of code to be executed if the condition is True
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
Example-- In the example below, we test two values to find out if 20 is greater than
18. If the condition is True, print some text:
if (20 > 18)
{
Console.WriteLine("20 is greater than 18");
}
OUTPUT-
20 is greater than 18
int x = 20;
int y = 18;
if (x > y)
{
Console.WriteLine("x is greater than y");
}
OUTPUT-
20 is greater than 18
Example explained
In the example above we use two variables, x and y, to test whether x is greater than y
(using the > operator). As x is 20, and y is 18, and we know that 20 is greater than 18,
we print to the screen that "x is greater than y".
Syntax
if (condition)
{
// block of code to be executed if the condition is True
}
else
{
// block of code to be executed if the condition is False
}
Example-
Outputs
Good evening.
Example explained
In the example above, time (20) is greater than 18, so the condition is False. Because
of this, we move on to the else condition and print to the screen "Good evening". If
the time was less than 18, the program would print "Good day".
Example-
Example explained
In the example above, time (22) is greater than 12, so the first condition is False. The
next condition (time is greater than 16), in the else if statement, is also False, The next
condition (time is greater than 20), in the else if statement, is also False, so we move
on to the else condition since condition1, condition2 and condition3 are False - and
print to the screen "Good night".
However, if the time was 14, our program would print "Good afternoon."
4. Switch construct
The switch statement allows you to handle program flow based on predefined sets of
conditions. It takes a switch argument followed by a series of case clauses. The syntax
of the switch construct is as in the following:
Syntax
switch(argument)
{
case 1:
// Do anything
break;
case 2:
// Do anything
break;
default:
// Do anything
break;
}
When the expression in the switch argument is evaluated, the code immediately
following the case clause executes and it marks the end of statements by the break
clause. If the expression evaluates to none of the other clause then you can include a
default clause.
Note: The order of case doesn't matter; you can put the default case first.
The following example performs some short math operations like addition,
multiplication and so on. You can ask the user at run time in form of choices what
operation he wants to do. Then we read two numeric values from the console and
execute the operation as selected earlier.
switch (op)
{
case 1:
Console.WriteLine("Add=" + (x + y));
break;
case 2:
Console.WriteLine("Subs=" + (x - y));
break;
case 3:
Console.WriteLine("Multiply=" + (x * y));
break;
default:
Console.WriteLine("wrong choice");
break;
}
Console.ReadKey();
}
Output
Depends on the user inupt
For practice-
Example 1
int data=10;
if (data !=5)
{
Console.WriteLine("value is not equal");
}
Example 2
For example in the following program we want to determine whether or not a string
is longer than zero characters:
static void Main(string[] args)
{
if (str.Length > 0)
Console.WriteLine("lenght is > 0");
else
Console.WriteLine("lenght is < 0");
Console.ReadKey();
}
Example 3
Example 4
switch (country)
{
case "india":
goto case "Canada";
case "USA":
break;
case "Canada":
break;
}