Unit-2
Control Statements in c#
A control statement
in java is a statement that determines whether the other statements will be
executed or not. It controls the flow of a program. Control Statements can be divided into three categories,
namely
●
Decision Making statements
●
Iteration
(Looping) statements
●
Jump
statements
⮚ Decision
Making statements
Decision making statements help you to
make decision based on certain conditions. These conditions are specified by a
set of decision making statements having boolean expressions which are evaluated
to a boolean value true or false. There are following types of decision making
statements in C#.
1) Simple if statement: It is the most basic statement
among all control flow statements in C#. It evaluates a Boolean expression and
enables the program to enter a block of code if the expression evaluates to
true.
Syntax:
if(boolean_expression)
{
/* statement(s) will execute if the boolean
expression is true */
}
Flow Diagram
Example
using System;
namespace DecisionMaking {
class Program {
static void Main(string[] args) {
/*
local variable definition */
int a = 10;
/*
check the boolean condition using if statement */
if (a < 20) {
/*
if condition is true then print the following */
Console.WriteLine("a is less than 20");
}
Console.WriteLine("value of a is : "+ a);
}
}
}
Output
a is less than 20;
value of a is : 10
2) if else statement
If else
is an extension to the if-statement, which uses another block of code,
i.e., else block. The else block is executed if the condition of the if-block
is evaluated as false.
Syntax:
if(boolean_expression) {
/* statement(s)
will execute if the boolean expression is true */
} else {
/* statement(s)
will execute if the boolean expression is false */
}
using System;
namespace DecisionMaking {
class Program {
static void Main(string[] args) {
/*
local variable definition */
int a = 100;
/*
check the boolean condition */
if (a < 20) {
/*
if condition is true then print the following */
Console.WriteLine("a is less than 20");
} else {
/*
if condition is false then print the following */
Console.WriteLine("a is not less than 20");
}
Console.WriteLine("value of a is : {0}", a);
}
}
}
Output
a is not less than 20;
value of a is : 100
In C#, nested if
statements are if
statements placed inside
another if
or else block. This is useful
when you want to check multiple conditions
in a hierarchical or dependent way.
Syntax of Nested if
in C#:
if (condition1)
{
// Executes if condition1 is true
if (condition2)
{
// Executes if both condition1 and condition2 are true
}
else
{
// Executes if condition1 is true but condition2 is false
}
}
else
{
// Executes if condition1 is false
}
FLOW DIAGRAM
Example:
using System;
class
Program
{
static
void
Main()
{
int age =
20;
string citizenship =
"Nepali";
if (age >=
18)
{
if (citizenship ==
"Nepali")
{
Console.WriteLine(
"You are eligible to vote.");
}
else
{
Console.WriteLine(
"You are not a Nepali citizen.");
}
}
else
{
Console.WriteLine(
"You are underage.");
}
}
}
if-else if
Ladder in C#
The if-else if
ladder in C# is used when you have multiple conditions
to test, one after another. Only the first true condition will be executed,
and the rest will be skipped.
Syntax:
if (condition1)
{
// Code if condition1 is true
}
else
if (condition2)
{
// Code if condition2 is true
}
else
if (condition3)
{
// Code if condition3 is true
}
else
{
// Code if none of the above conditions are true
}
Example: Grading System
using System;
class
Program
{
static
void
Main()
{
int marks =
75;
if (marks >=
90)
{
Console.WriteLine(
"Grade: A+");
}
else
if (marks >=
80)
{
Console.WriteLine(
"Grade: A");
}
else
if (marks >=
70)
{
Console.WriteLine(
"Grade: B");
}
else
if (marks >=
60)
{
Console.WriteLine(
"Grade: C");
}
else
{
Console.WriteLine(
"Fail");
}
}
}
Switch Statement
The switch
statement is a control statement that executes one block of code
among many options based on the value of a variable.
Syntax Of Switch Statement:
switch (expression)
{
case value1:
// Code block
break;
case value2:
// Code block
break;
default:
// Default code block
break;
}
Flow diagram
using System;namespace program{
public class SwitchStatement{static void Main(string[] args){int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Sunday");
break;
case 2:
Console.WriteLine("Monday");
break;
case 3:
Console.WriteLine("Tuesday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
}}}
nested switch
A nestedswitch means using a switch
statement inside another switch
.
It's useful when you want to perform different actions based on multiple
conditions.
int category = 1;
int item = 2;
switch (category)
{
case 1:
Console.WriteLine("Fruits");
switch (item)
{
case 1:
Console.WriteLine("Apple");
break;
case 2:
Console.WriteLine("Banana");
break;
default:
Console.WriteLine("Unknown Fruit");
break;
}
break;
case 2:
Console.WriteLine("Vegetables");
switch (item)
{
case 1:
Console.WriteLine("Carrot");
break;
case 2:
Console.WriteLine("Potato");
break;
default:
Console.WriteLine("Unknown Vegetable");
break;
}
break;
default:
Console.WriteLine("Unknown Category");
break;
}
⮚
Looping
In programming, sometimes we need to execute the block of
code repeatedly while some condition evaluates to true. Loop statements are
used to execute the set of instructions in a repeated order.
The execution of
the set of instructions depends upon a particular condition.
- for loop
- while loop
- do-while loop
- nested loop
1. For Loop
For Loop is entry controlled loop. It enables us to initialize the
loop variable, check the condition, and increment/decrement
in a single line of code. We use the for loop only when we
exactly know the
number of times, we want to execute the block of code.
Syntax:
for(initialization, condition, increment/decrement) {
//block of statements
}
Example:
// C# program to illustrate for loop.
using System;
class ForLoop
{
public static void Main()
{
for(int i = 1; i <= 5; i++)
Console.WriteLine("Hello Computer Engineering Students");
}
}
2. While Loop
·
It is called an entry-controlled
loop because the condition is checked before the loop
body executes.
·
Used when
the number of iterations is not known in advance.
·
Initialization is
done before the loop, and increment/decrement is done inside
the loop body.
Syntax:
while (condition)
{
// statements
}
Example:
using System;
class
whileLoopDemo
{
public
static
void
Main()
{
int x =
1;
while (x <=
4)
{
Console.WriteLine(
"Hello Computer Engineering Students");
x++;
}
}
}
3. Do-While Loop
·
It is an exit-controlled
loop as the condition is checked after the loop
executes.
·
It executes
at least once, even if the condition is false.
·
Best used when
you want to ensure the loop runs at least once.
Syntax:
do
{
// statements
}
while (condition);
Example:
using System;
class
dowhileloopDemo
{
public
static
void
Main()
{
int x =
10;
do
{
Console.WriteLine(
"Hello Computer Engineering Students");
x++;
}
while (x <
9);
}
}
Output:
Hello Computer Engineering Students
Jump Statements in C#
Jump statements are used to change the normal flow of execution.
They are useful for breaking or skipping loop iterations.
1. Break Statement
·
Exits the current loop or switch.
·
The control jumps to the first statement after the loop.
Example:
using System;
namespace
MyApplication
{
class
Program
{
static
void
Main(
string[] args)
{
for (
int i =
0; i <
10; i++)
{
if (i ==
4)
{
break;
}
Console.WriteLine(i);
}
}
}
}
Output:
0
1
2
3
2. Continue Statement
·
Skips the current iteration and moves to the next iteration.
·
Loop does not break, only skips the remaining code for the
current pass.
Example:
using System;
namespace
MyApplication
{
class
Program
{
static
void
Main(
string[] args)
{
for (
int i =
1; i <
6; i++)
{
if (i ==
3)
{
continue;
}
Console.WriteLine(i);
}
}
}
}
Output:
1
2
4
5
ohh nice pdf
ReplyDelete