Since if...else is also a statement, you can use it under if...else statement (nesting), like:
The else on line 7 is clearly related to if on line 3 while else on line 13 belongs to if on line 1. Finally, do note (C/C++ programmers especially) that if statements expect only Boolean expressions and not integer values. It's an error to write:
Instead, you can either use:
Or,
The keys to avoiding confusion in the use of any complex combination of if...else are:
Habit of using { } brackets with every if and else.
Indentation: aligning the code to enhance readability. If you are using Visual Studio.Net or some other editor that supports coding, the editor will do indentation for you . Otherwise, you have to take care of this yourself.
I strongly recommend you follow the above two guidelines.
The Switch...case Statement
If you need to perform a series of specific checks, switch...case is present in C# just for this . The general structure of the switch...case statement is:
It takes less time to use switch...case than using several if...else if statements.Let's look at it with an example:
using System;
// To execute the program write "SwitchCaseExample 2" or
// any other number at command line,
// if the name of .exe file is "SwitchCaseExample.exe"
namespace CsharpProgramming
{
class SwitchCaseExample
{
//Demonstrates the use of switch...case statement along with
// the use of command line argument
public static void Main(string [] userInput )
{
int input = int.Parse(userInput [0]);
// convert the string input to integer.
// Will throw a run-time exception if is no input at run-time or if
// the input is not castable to integer.
switch (input) // what is input?
{
case 1: // if it is 1
Console.WriteLine("You typed 1(one) as the first command line argument");
break; // get out of switch block
case 2: // if it is 2
Console.WriteLine("You typed 2(two) as the first command line argument");
break; // get out of switch block
case 3: // if it is 3
Console.WriteLine("You typed 3(three) as the first command line argument");
break; // get out of switch block
default: // if it is not any of the above
Console.WriteLine("You typed a number other than 1,2 and 3");
break; // get out of switch block
}
}
}
}
The program must be supplied with an integer command line argument. First, compile the program (at the command line or in Visual Studio.net). Suppose we made an exe with name "SwitchCaseExample.exe", we would run it at the command line like this:
Or:
If you did not enter any command line arguments or gave a non-integer argument, the program will raise an exception:
Let's get to internal working. First, we converted the first command line argument (userInput[0]) into an int variable input. For conversion, we used the static Parse() method of the int data type. This method take a string and returns the equivalent integer or raises an exception if it can't. Next we checked the value of input variable using a switch statement:
Later, on the basis of the value of input, we took specific actions under respective case statements. Once our case specific statements end, we mark it with the break statement before the start of another case (or the default) block.
case 3: // if it is 3
Console.WriteLine("You typed 3(three) as first command line argument");
break; // get out of switch block
If all the specific checks fail (input is none of 1,2 and 3), the statements under default executes.