Control statement decides the flow of control in a program. There are several types of control statement.
1. if Statement:
The code inside the if block runs if the condition is satisfied.
using System;
namespace ControlStatement{
class Program{
int num = 10;
/* Syntax,
if(condition){
Statements...
}
*/
if(num>9){
Console.WriteLine("Number is greater than 5.")
}
}
}
2. if-else Statement:
If the condition is satisfied, then the code inside the if block run otherwise the code in else block runs.
using System;
namespace ControlStatement{
class Program{
int num = 10;
/* Syntax,
if(condition){
Statements...
}
else{
Statements...
}
*/
if(num>9){
Console.WriteLine("Number is greater than 5.");
}
else{
Console.WriteLine("Numebt is less than 9.");
}
}
}
3. if-else-if Statement:
In if-else-if statement multiple conditions can be check. If one condition is not satisfied then next condition is checked.
using System;
namespace ControlStatement{
class Program{
int num = 5;
/* Syntax,
if(condition){
Statements...
}
else-if(condition){
Statements...
}
else{
Statements...
}
*/
if(num>9){
Console.WriteLine("Number is greater than 5.");
}
else-if(num >=5 && num < 9){
Console.WriteLine("Number is 5");
}
else{
Console.WriteLine("Numebt is less than 5.");
}
}
}