University of Computer
Studies
Chapter(3)
Program Control Statements
1
Contents
Looping Statements
for
Decision Statements foreach
if statement
while
if…..else statement do….while
if….else if…..else statement
nested if statement Jump Statements
break
switch statement
continue
Ternary operator ( ?: )
goto
return
2
Decision Statements
to determine one or more conditions evaluated
by the program at run-time
a statement or statements to be executed if the
condition is determined to be true
other statements to be executed if the condition
is determined to be false
3
if Statement
if the condition becomes true, then the block of statements enclosed within curly braces will get
executed
Syntax
if(condition)
{
// code for execution
}
Example
Console.WriteLine("Enter your mark");
int mark = int.Parse(Console.ReadLine());
if (mark >= 50)
{ Console.WriteLine("Pass"); }
4
if…..else Statement
if the condition becomes true, then the block of statements enclosed within curly braces will get
executed
otherwise, the block of code associated with the else will get executed
Syntax
if(condition)
{
// if condition is true
}
else
{
// if the condition becomes false
}
5
if…..else Statement
Example
Console.WriteLine("Enter your mark");
int mark = int.Parse(Console.ReadLine());
if (mark >= 50)
{ Console.WriteLine("Pass "); }
else
{ Console.WriteLine("Fail"); }
6
if…..else if…..else Statement
Execute a single block based on one condition from multiple conditional statements
The conditions are checked one by one in the if statements from the top and will execute that block whose condition
is
evaluated to true
Syntax
if(first-condition)
{
} // when first condition becomes true
else if(second-condition)
{
// when second condition becomes true
}
else if(third-condition)
{
// when third condition becomes true
}
else
{ // when all the conditions }
7
are false
if…..else if…..else Statement
Example
Console.WriteLine("Enter your mark");
int mark = int.Parse(Console.ReadLine());
if (mark >= 50 && mark<65)
Console.WriteLine("Pass");
else if (mark >= 65 && mark < 80)
Console.WriteLine("Pass with Credit");
else if (mark >= 80 && mark<=100)
Console.WriteLine("Pass with Distinction");
else if(mark<50 && mark>=0)
Console.WriteLine("Fail");
else
Console.WriteLine("Invalid Mark");
8
Nested if Statement
if statement will be nested or inside another if or else statement.
multiple conditions have to be evaluated as true only then the nested block associated with multiple
"if conditions" will be executed.
Syntax
if (first-condition)
{
// when first condition becomes true
if (second-condition)
{
if second condition becomes true, code to be executed
}
}
9
switch Statement
It acts as an alternative to the "if-else-if" statement This keyword specifies some code to run if there is no case
Each value is called a case. match.
The value of the expression is compared with the The switch expression is of integer type such as int, char,
values of each case. byte, or short, or of an enumeration type, or of string type.
If there is a match, the associated block of code is Duplicate case values are not allowed
executed. The data type of the variable in the switch and value of a
The switch expression is evaluated once. case must be of the same type
Each case is exited by a break statement that helps the The value of a case must be a constant or a literal.
program flow move out of the switch case blocks. Variables are not allowed
A switch statement can have an optional default case,
which must appear at the end of the switch.
10
switch Statement
Syntax
switch(expression)
{
case value1: statement1; break;
case value2: statement2; break;
case value3: statement3; break;
.
.
.
default: statmentDefault; break;
11
switch Statement
Example
Console.WriteLine("Enter your grade");
char grade =char.Parse(Console.ReadLine());
switch (grade)
{ case 'A': Console.WriteLine("Excellent"); break;
case 'B':
case 'C': Console.WriteLine("Well Done"); break;
case 'D': Console.WriteLine("Pass"); break;
case 'F': Console.WriteLine("Fail"); break;
default: Console.WriteLine("Invalid Grade");
} break; 12
Switch Exercises
Write a program to test whether a user input letter is a vowel or not a vowel?
Ternary operator ( ?: )
a decision-making operator ?:
Also called conditional operator or ternary operator
short form of the if else conditions
Syntax
(condition)? statement1 : statement2
Example
string result = ( mark >= 50 )? “Pass” : “Fail”;
Console.WriteLine( result);
14
Looping Statements
to execute a statement or a group of statements
multiple times
15
for Loop
For loop is used when we clearly know the number of iteration required by the loop
to define the exact number of iterations to be performed
contains initialization and iteration expressions along with the Boolean expression to provide a condition for the
loop
Syntax
for ( initialization; condition; increment/decrement )
{
//code block for execution
}
16
for Loop
Example
for( int i=1 , j=10 ; i<=10 && j>=1; i++ , j-- )
{
int sum = i + j;
Console.WriteLine(sum);
17
foreach Loop
It is used when a program needs to iterate through the contents of a list.
It can be used for collections like array lists or arrays.
It is read-only, hence it doesn’t allow you to modify the iteration variable during execution.
Syntax
foreach( variable in data collection)
{
statements
}
18
foreach Loop
Example
string[ ] student = { "Su Su", "Yu Yu", "Aung Aung" };
foreach( string st in student
)
{
Console.WriteLine( st );
19
while Loop
The while loop loops through a block of code as long as
a specified condition is True
Syntax
while ( condition)
{
statement
20
while Loop
Example
Console.WriteLine("Enter number");
int num = int.Parse(Console.ReadLine());
while (num >= 0)
{
Console.WriteLine("Number = " + num);
Console.WriteLine("Enter number");
num = int.Parse(Console.ReadLine());
}
21
do while Loop
It works similar to a while loop.
The Boolean expression is evaluated at the end of the loop instead of
the beginning.
Syntax
do
{
statement
}while ( condition);
22
do while Loop
Example
Console.WriteLine("Enter number");
int num = int.Parse(Console.ReadLine());
do
{
Console.WriteLine("Number = " + num);
Console.WriteLine("Enter number");
num = int.Parse(Console.ReadLine());
} while (num >= 0);
23
Jump Statements
used to transfer control from one point to another
point in the program due to some specified code
while executing the program
five keywords in the Jump Statements:
1. break
2. continue
3. goto
4. return
5. throw
24
break Statement
used to break loop or switch statement
immediately stops the innermost loop containing the statement and
passes control to the end of the loop
Syntax
break;
25
break Statement
Example
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("Enter even number");
int num = int.Parse(Console.ReadLine());
if (num % 2 != 0)
{ break; }
Console.WriteLine("Even Number = " + num);
}
26
continue Statement
It used to skip over the execution part of the loop on a
certain condition.
After that, it transfers the control to the beginning of the
loop.
It skips its following statements and continues with the next
iteration of the loop.
Syntax
continue;
27
continue Statement
Example
f
o
r
(
i
n
t
if (num % 2 != 0)
i { continue; }
=
Console.WriteLine("Even Number = " + num);
1
}
;
i
28
goto Statement
known jump statement
used to transfer control to the labeled statement in the program
unconditionally jumps to the specified label
used to transfer control from deeply nested loop or switch case
label
Syntax
goto label;
29
goto Statement
Example
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Enter even number");
int num = int.Parse(Console.ReadLine());
if (num % 2 != 0)
{ goto output; }
Console.WriteLine("Even Number = " + num);
}
output: Console.WriteLine("Input is Odd number");
30
return Statement
It is useful to terminate the execution of the method in which it
appears
It returns the control back to the calling method
Methods return only one value
If the type of method is void, then the return statement can be excluded
Example
public static int Sum( )
{
int result = 20 + 30;
return result;
}
31
32