C# tutorial-Conditional statements-if else |
|||||||||||||||||||||||||||
C# conditional statements-if/else-if/else statementif statement will evaluate the condition. If it is true, it will execute the statements that follow it. Otherwise, it will execute the statements in else block.if statement without else: if (condition) {
Statement Statement } Or if statement with else: if (condition) {
Statement Statement } else {
Statement Statement } Or if (condition) {
Statement Statement } else if {
Statement Statement } else {
Statement Statement } Example:
int age; else Console.WriteLine("You are not allowed to play the game.");
The below table is the list of operators which can be used in if/else statement.
If you want to put more than one statements in an if/else statement then you must write them in brackets. Example:
int age; Note: if/else statements can be nested.
Example:
string user, password; else Console.WriteLine("Invalid password! Try again");
else Console.WriteLine("Invalid user name! Try again");
If you want to evaluate more than one condition you can use many else if blocks as possible. Example: Console.WriteLine("Enter a letter:"); char c =(char)Console.Read(); if (Char.IsDigit(c)==true) Console.WriteLine("A number"); else if(char.IsLower(c)==true) Console.WriteLine("A lower case letter"); else if(char.IsUpper(c)==true) Console.WriteLine("An upper case letter");
|
|||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||