You are on page 1of 42

Module 4:

Flow Control

By

SRIRAM . B
Overview
 Statement
 Selection – if, switch
 Iteration - while, do..while, for, foreach
 Jump - break, continue, goto, return
Statements

 Statements are the smallest unit of execution in C#


programs. They can be grouped into sequences using
curly braces. These are called Compound Statements or
Blocks. A compound statement can be used in place of
simple statement.

 C# provides variety of statements. Most of the


statements are similar to C,C++,Java.

 The statements in C# can be categorized as:


 Selection Statements
 Iteration Statements
 Jump Statements
Selection Statements

 Selection Statements selects one of a number of


possible statements for execution based on the
value of a controlling expression.
 A selection statement can be:-
 If Statement
 Switch Statement
If Statement

 The If statement selects a statement for execution


based on the value of a Boolean expression.
Syntax:-
If(condition 1)
statement_1
Else
statement_2
statement_3

The condition is tested first. If the condition evaluates


to TRUE then statement_1 is executed otherwise
statement_2 is executed. After that statement_3 is
executed.
Example 1 – If Statements
using System;
class If_construct
{
public static void Main()
{
int var1 =10;
if (var1 > 0)
Console.WriteLine(“Your number is
greater than 0");
else
Console.WriteLine(“Your number is less
than or equal to 0”);
}
}
Example 1 – If Statements..

Output of the code is:

Your number is greater than 0


Nested If

 The If statement may be nested to many levels.


This means If statement may contain another If
statement.
 Syntax:-
If(condition 1)
If(condition 2)
statement_1
Else
statement_2
Example – Using Logical Operators in
If..else
Check the age of an applicant and display an appropriate
message as follows:

Print the message “ Valid age” if the age of the candidate


is between 18 and 25 and print “Invalid age” if the age
criterion is not satisfied.
using System;
class IF
{
public static void Main()
{
int age=26;
if (age >=18 && age <=25)
Example – Using Logical Operators in
If..else ..

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:

 The switch expression is evaluated first and acts as


the control of execution.

 If the value of the case label matches the switch


expression, then the statements following it are
executed.

 A label in a case is a fixed value.

 If no label matches the value of the switch


expression, then the statements following the
default label are executed.
Switch Statement Syntax

 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.

 When the condition specified in the loop evaluates to


false, control is transferred to the statement following the
loop.
 Some of the iteration constructs are:
 while
 do…while
 for
 foreach
Diff. Between While & Do..While
Statements
While Do..While
 Condition is tested at  Condition is tested at
the beginning of the the end of the loop
loop
 Loop will execute at
 Loop will execute once least once even the
the condition is true condition is false
While Statement
 The while statement is used to conditionally execute a
set of statements zero or more times. A while statement
can be executed as follows:-
 The boolean expression is evaluated

 If the boolean expression is true, the control is


transferred to statements, once it reaches the end
point of the statement the control is transferred to
the beginning of the while statement.

 If the boolean expression is false, controls is


transferred to the end point of while statement
 Syntax:-
while(condition)
{ statement;
}
Example 1 : While Statement
using System;
class While_construct
{
public static void Main() {
int i=0; //Initialization
while (i<3) //Condition
{
Console.WriteLine(i); //Loop body
i++; //Re-initialization
}
}
}
Example 1 : While Statement Output
Output of the code is:
0
1
2
Do..While Statement
 The do..while statement conditionally executes an statement
one or more times. A do..while statement is executes as
follows:-
 Control is transferred to statement

 When the control reaches the end point of the statement,


the condition is evaluated. If the boolean expression is
true, the control is transferred to begin of the do..while
loop, otherwise the control is transferred to the end point
of the do..while statement.
 Syntax:-
do
{
statement;
}while(condition)
Example : Do..While Statement
using System;
class Do_while
{
public static void Main() {
int i=3; //Initialization
do
{
Console.WriteLine(i); //Loop body
i++; //Re-initialization
}while(i<3);//Condition
}
}
Example : Do..While Statement
Output
Output of the code is:
3
For Statement

 The for statement consists of the keyword, for, followed


by parenthesis which contains three expressions that are
separated by a semicolon.

 The expression consists of the initialization expression,


the condition and the re-initialization expression.
 The general structure of a for construct is
for(initialization; condition;re-initialization)
{
Statements
}
Example – For Statement
using System;
class For_construct
{
public static void Main()
{
for(int i=0;i<3;i++)
{
Console.WriteLine(i);
}
}
}
Foreach Statement

 Repeats a group of statements for each element in


an array or an object collection.

 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

 Jump statements are used to unconditionally


transfer control to program statements.

 The location to which a jump statement transfers


control is referred to as the target.

 When a jump statement transfers control to a


statement outside the block, it exits the block.
Jump Statements..
Some of the jump statements are :

 The break statement is used to terminate a case in a


switch statement and terminate a loop, bypassing the
loop condition.

 The continue statement forces the next iteration of


the loop to take place, skipping any code following
the continue statement in the loop body.

 The goto statement is used to transfer control to a


statement that is marked by a label.

 The return statement is used to return control to the


caller function in which the return statement is used.
Example 1 - Break Statement
using System;
class Hello
{ public static void Main()
{
int i=1;
do
{ Console.WriteLine(i);
if(i==5)
break;
i++;
}while (i<100);
}
}
Example 1 - Continue Statement
using System;
class Hello
{
public static void Main()
{
for (int var = 1; var <= 10; var++)
{
if (var < 9)
continue;
Console.WriteLine(var);
}
}
}
Example - Goto Statement
using System;
class Hello
{
public static void Main()
{
for (int var = 1; var <= 10; var++)
{
if (var < 9)
goto finish;
Console.WriteLine(var);
}
finish:
Console.WriteLine("After finish");
}
}
Example - Return Statement
Exercise
What is the difference between while and do…
while construct?
What is the purpose of the break statement?
Predict the output of the following code
using System;
class Hello
{
public static void Main()
{
int var;
for (var = 1; var <= 10; var++);
{
Console.WriteLine(var);
}
}

}
Exercise..

Write a program to generate the first twenty


numbers in a Fibonacci series – 0 1 1 2 3 5 8 13
….

Write a program to print all even numbers from 1 to


100.

Write a program that displays the count of numbers


which are divisible by 5 in the range 1 to 100.
Flow Control Flashback
 Statement
 Selection – if, switch
 Iteration - while, do..while, for, foreach
 Jump - break, continue, goto, return
Session Ends
Exercise
Relax

You might also like