Control statements are the statements which are used to control the flow of the program. Many times we need to make some decision, skip some code, repeat the same code, or switch from one block to other block of code. Control statements are of two types:
Selection Statements
The selection statements are blocks of code which execute after a condition is satisfied. It consists of:
if statement:
If statements hold the condition to be checked for a piece of code to execute. If the condition is true then it will execute the if part otherwise it skips the bock of code inside if and jumps to the else statement (if else block is available). The syntax of the if statement is:
If(Condition)
{
//Do something;
}
Else
{
//Else work;
}
Example:
- If(answer == ”yes”)
- {
- Console.WriteLine(“YES”);
- }
- Else
- {
- Console.WriteLine(“No”);
- }
Here, if the resultant answer will be yes then YES will be printed otherwise NO will get printed.
Switch statement:
When a block of code is needed to be executed based on some matching context , then those statements are called switch statements. When multiple statements are there depending on a variable, or there are many results on different values of a variable then a switch case statement is used. Its syntax is:
Switch(value)
{
Case1:
{
//Do something;
break;
}
Case2:
{
//Do work;
break;
}……
Casen:
{
//Do work;
break;
}
default:
{
//work;
break;
}
}
Example:
- Switch(roll)
- {
- Case1:
- {
- Console.WriteLine(“student 1”);
- break;
- }
- Case2:
- {
- Console.WriteLine(“student 2”);
- break;
- }
- default:
- {
- Console.WriteLine(“student”);
- break;
- }
- }
Here, the print value will be according to the input roll no. and if the input roll number does not match any of the case, the default value will be printed.
Iterative Statements:
When we want to repeat a block of code more than once, the statements which iterate these code are called iterative statements. We can repeat a block of code zero or more times & each time it executes it is counted as one iteration. They are of four types: - While Statement: If a block of code is to be executed again & again till the condition is true , we use while statement. The syntax is:
while(condition)
{
//repeat the code until the condition is false.
}
Example:
- while(counter>10)
- {
- counter++;
- }
Here, the counter will gert increased until it is less than 10.
- Do While Statement: If a block of code is to be executed again & again till the condition is true , but it should execute at least once, we use do while statement.
do
{
//repeat the code until the condition is false.
}
while(condition)
Example:
- do {
- counter++;
- }
- while (counter > 10)
Here, the counter will increase at least once & then if condition is satisfied then it will increase till it reaches 10.
- For Statement: Instead of while block we can also use for statement where we repeat the code by initializing a variable, putting condition for the iteration & incrementing it. The syntax is:
for (initialization; condition; incrementation)
{
// do work;
}
Example:
- for (i=0; i<10; i++)
- {
- Console.WriteLine(“repeat”);
- }
Here, i is initialized to 0 then it prints repeat and then I is incremented by 1 and again the loop continues until it reaches 10. It will print repeat till i is 9 , when it reaches 10 it breaks and come out of the loop.
- foreach Statement: It is similar to for statement but it can loop through collections such as arrays. Its syntax is:
foreach(var data in collection)
{
//do work
}
Example:
- foreach (var i in arr)
- {
- Console.WriteLine(“arr[i]”);
- }
Here, i is the count of the total values in array arr and it will print each array value one by one.
Hope this blog has helped you in understanding conditional statements.