Module 4 - Flow Control
Module 4 - Flow Control
Module 4 - Flow Control
Flow Control
By
SRIRAM . B
Overview
Statement
Selection – if, switch
Iteration - while, do..while, for, foreach
Jump - break, continue, goto, return
Statements
Console.WriteLine("Valid age");
else
Console.WriteLine("Invalid age");
}
}
Output of the code is:
Invalid age
Example 1 – Nested IF
using System;
class NestedIF
{
public static void Main()
{
int A=30;int B=50;int c=25;
if (A > B)
{
if (A > C)
{
Console.WriteLine(A+ “is greater
than” +C
+ “and” +B);
}
Example 1 – Nested IF..
else
if (A > C)
{
Console.WriteLine(B+ “is greater than” +C
+ “and” +A);
}
}
}
Switch Statement
A switch…case statement executes the statements that
are associated with the value of controlling expression
as follows:
Syntax:-
switch(expression)
case <label>:
Statements
case <label>:
Statements
default:
Statements
}
Example – Switch Case Statement
using System;
class Switch_case
{
public static void Main() {
int i=12;
switch(i)
{
case 12:
Console.WriteLine(“Dozen");
break;
Example – Switch Case Statement ..
case 20:
Console.WriteLine(“Score");
break;
default:
Console.WriteLine("default");
break;
}
}
}
Output of the code is:
Dozen
Iteration Statements
While Statement
Do..While Statement
For Statement
Foreach Statement
Iteration Statements
Iteration constructs are also known as loop constructs.
Loop constructs are used for the repeated execution of
statements based on a given condition.
The condition has to be an expression that returns a
Boolean value.
The execution of the statements within a loop continues
as long as a condition is true.
Syntax :-
foreach( type variable in list)
{
statements
}
Example
foreach(int I in arr)
{
statements
}
Example - Foreach Statement
using System;
class Foreach
{
public static void Main()
{
int[]arr = new int {0,1,2,5};
foreach(int i in arr)
{
Console.WriteLine(“Element is {0}”, i);
}
}
}
Jump Statements
Break Statement
Continue Statement
Goto Statement
Return Statement
Jump Statements
}
Exercise..