C#
has two constructs for braching code – the if
statement, which allows us to test whether a specific condition is met, and
the switch statement, which allows
us to compare an experssion with a number of different values.
The if
statement is one of the single most important statements in every programming language. In C#, the if statement is very simple to use. The if statement needs a
boolean result that is true or false.
The switch
statement is like a set of if statements. It will be familiar to C++ and Java programmers and is similar to select case statement in Visual Basic.
- The if statement
if (condition)
statement(s)
else
statement(s)
Example:
namespace IfStatement{class Program{static void Main(string[] args){int mValue;Console.WriteLine("Enter a Value between 0 and 5");mValue = int.Parse(Console.ReadLine());if (mValue <= 5)Console.WriteLine("Value is between 0 and 5!");elseConsole.WriteLine("Value greater than 5!!");Console.ReadLine();}}}
- The switch statement
Example:
namespace SwitchStatements{class Program{static void Main(string[] args){int mValue;Console.Write("Value is: ");mValue = int.Parse(Console.ReadLine());switch (mValue){case 1:Console.WriteLine("Value = 1");break;case 2:Console.WriteLine("Value = 2");break;case 3:Console.WriteLine("Value = 3");break;case 4:Console.WriteLine("Value = 4");break;default:Console.WriteLine("Value is not 1,2,3 or 4");break;}Console.ReadLine();}}
}
No comments:
Post a Comment