3.control Structure
3.control Structure
Structure
Control Structure
conditio
n
int x = 45;
if( x > 95)
“Student is brilliant”
If Statement
An if statement consists of a boolean expression followed by one or
more statements.
If …… else Statement
An if statement can be followed by an optional else statement, which executes
when the Boolean expression is false
The switch statement is used to select one of many code blocks to be executed.
It is based on the execution of the conditional code referenced the value of the
variable specified in the switch statement.
so A switch statement allows a variable to be tested for equality against a list
of values.
If Statement
If (boolean_expression)
{
/* statement(s) will execute if
the boolean expression is true */
}
If … else statement
if(boolean_expression)
{
/* statement(s) will execute if the boolean
expression is true */
}
else
{
/* statement(s) will execute if the boolean
expression is false */
}
Switch Statement
switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
Conditional
Code
If condition is true
conditio
n
If condition is false
While loop type
While loop
It repeats a statement or a group of statements while
a given condition is true. It tests the condition before
executing the loop body.
while(condition)
{
statement(s);
}
While loop Example
using System;
namespace Loops
{
class Program
{
{
int a = 10;
{
a++;
}
Console.ReadLine();
}
}
}
Do … While Loop type
Do … While loop
It is similar to a while statement, except that it tests
the condition at the end of the loop body.
do
{
statement(s);
} while( condition );
For Loop type
For loop
It executes a sequence of statements multiple times
and abbreviates the code that manages the loop
variable.
class Program
{
static void Main(string[] args)
{
/* for loop execution */
for (int i = 10; i < 20; i = i+ 1)
{
Console.WriteLine("value of i: {0}", i);
}
Console.ReadLine();
}
}
}
For each
Example
We declare a list of strings fruits.
The foreach loop iterates over each element in the fruits list and
prints it to the console.
Example
using System;
class Program
{
static void Main()
{
string[] cars = { "Volvo", "BMW", "Ford", "Mazda" };
foreach (string car in cars)
{
Console.WriteLine(car);
}
}
}
Break and continue
Certainly! In C#, break and continue are control flow statements used within loops to alter
the flow of execution.
The break statement is used to exit from the nearest enclosing loop or switch statement.
When a break statement is encountered, the loop or switch terminates immediately, and
control is transferred to the statement following the terminated statement.
Example:
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break; // Exit the loop when i is 5
}
Console.WriteLine(i);
}
// Output: 0 1 2 3 4
Break and continue
Break Statement
The break statement is used to terminate the closest
enclosing loop or switch statement. When
a break statement is encountered, the control is
transferred to the statement immediately following the
terminated loop or switch.
Continue Statement
The continue statement is used to skip the current
iteration of the closest enclosing loop and proceed with
the next iteration. When a continue statement is
encountered, the remaining code inside the loop for the
current iteration is skipped, and the loop proceeds with the
next iteration.
END