4.program Control Statements
4.program Control Statements
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
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());
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());
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());
else
Console.WriteLine("Invalid Mark");
8
Nested if Statement
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
10
switch Statement
Syntax
switch(expression)
{
case value1: statement1; 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':
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
Example
14
Looping Statements
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
{
//code block for execution
}
16
for Loop
Example
int sum = i + j;
Console.WriteLine(sum);
17
foreach Loop
Syntax
18
foreach Loop
Example
)
{
Console.WriteLine( st );
19
while Loop
Syntax
while ( condition)
{
statement
20
while Loop
Example
Console.WriteLine("Enter number");
int num = int.Parse(Console.ReadLine());
Console.WriteLine("Enter number");
num = int.Parse(Console.ReadLine());
}
21
do while Loop
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());
1. break
2. continue
3. goto
4. return
5. throw
24
break Statement
Syntax
break;
25
break Statement
Example
if (num % 2 != 0)
{ break; }
}
26
continue Statement
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
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
Example
31
32