Using construct we can execute a block of code
repeatedly or can transfer the control to another block.
Types
of construct:
- Selection construct
- Loop construct
- Jump construct
1. Selection constructs:
It execute the block of code based on Boolean
condition, which produce true or false.
Selection constructs referred to as decision- making
constructs, selection constructs in C# is similar to the selection construct in
C language.
C sharp support the following selection constructs:
- If construct
- If else construct
- If else if construct
- Nested if construct
- Switch case construct
1.
if Construct
If construct uses if keyword and execute block of code
if given condition become true.
Example:
In below example, if age is greater than and equal to
18 it print “you are welcome”
int age = 17;
if (age >= 18)
{
Console.WriteLine("you are
welcome");
}
Console.ReadLine();
2.
if else Construct
If else construct uses if and else keyword and execute
block of code if given condition become true, Otherwise it execute else block.
Example:
In below example, if age
is greater than and equal to 18 it print “you are welcome” otherwise it will
print “sorry you cannot enter”.
int age = 17;
if (age >= 18)
{
Console.WriteLine("you are
welcome");
}
else
{
Console.WriteLine("sorry you
cannot enter");
}
3.
if else if Construct
if else if construct uses two keyword if and else and
combination of “else, if” keywords, this type of construct are used for
checking multiple conditions for decision.
Example:
In below example, we are using one if condition and one
else if condition you can use multiple else if block for checking multiple
conditions.
int age = 31;
if (age >= 18
&& age<=30)
{
Console.WriteLine("you are 18
or above");
}
else if (age < 18
&& age>12)
{
Console.WriteLine("you are not
above 18");
}
else
{
Console.WriteLine("you does not
meet any criteria");
}
4.
Nested if Construct
You can also use if, else if, else block inside the if,
else if block which is called Nested if Construct.
Example:
In this example, if employee experience is greater than
and equal to 5 then it enter inside if block.
Then it check employee salary greater than and equal to
20,000, if condition meet it enter in if block otherwise enter in else block
and print “your salary is less than 20,000”.
If it enter in if (empSalary>=20000) block it check
rank greater than 10, if condition meet it enter inside if(rank > 10) block
otherwise it enter in else block and print “you are not eligible for
increment".
int empSalary=30000;
int empExperience=5;
int rank = 12;
if (empExperience
>= 5)
{
if (empSalary >=
20000)
{
if (rank > 10)
{
Console.WriteLine("you are
eligible for increment in your salary");
}
else
{
Console.WriteLine("you are not
eligible for increment");
}
}
else
{
Console.WriteLine("your salary
is less than 20,000");
}
}
5.
Switch case construct:
Switch case construct resolve the problem of too many
if statements. The main benefit of using switch construct is that it is lighter
than else if construct because it check all cases at compile time and then
immediately execute the specific case, but when you use ‘else if’ it check each
‘else if’ construct until the specific condition found.
Example:
int myvalue = 1;
switch (myvalue)
{
case 1:
Console.WriteLine(myvalue);
break;
case 2:
Console.WriteLine(myvalue);
break;
default:
Console.WriteLine("this is
default case");
break;
}
You can see above example which uses switch cases same
as C language switch case.
Case
keyword matches switch expression value then execute the code that is between
case and break statement, switch expression value can be integer string,
character etc.
Case keyword matches character in below
example
char mychar = 'A';
switch (mychar)
{
case 'a':
Console.WriteLine(mychar);
break;
case 'A':
Console.WriteLine(mychar);
break;
default:
Console.WriteLine("this is
default case");
break;
}
Break
terminates
the execution of statement sequence like in above example, because we don’t
want to perform further operation after the specific condition meet in a switch
block.
Default
keyword
is optional it execute when no case matches the switch expression value.
2.
Loop constructs:
Loop construct also referred to as iteration statement;
by the help of loops we can execute a single statement or a block of code
repetitively. If the condition is not satisfied, the loop will execute infinite
time, which is called infinite loop.
Types of loops
- For loop
- While loop
- Do While loop
- Foreach loop
1.
For loop:
For loop is used to execute the block repeatedly until
the condition of loop remains true.
For loop in C# is similar to the C language.
Example:
for (int i = 0; i < 10;
i++)
{
Console.WriteLine(i);
}
The above
code simply print 0 to 9 integer like,
0
1
2
3
4
5
6
7
8
9
Example 2:
This example prints
even number 0-10.
for (int a = 0; a <= 10;
a++)
{
if (a % 2 == 0)
{
Console.WriteLine(a+" is even
number");
}
}
0
2
4
6
8
10
We can also use nested for loop
Example 3:
Guess how many time the statement
will be execute?
for (int i = 0; i < 5;
i++)
{
for (int j = 0; j < 3;
j++)
{
Console.WriteLine("outer loop
value {0} and inner loop value {1}",i,j);
}
}
Yes it will execute 15 time because
outer loop will be execute 5 (0-4) times and inner loop will be 3 times (0-2).
You can also identify execution simply multiple
(5(final value)-0(initial value))*(3(final
value)-0(initial value)) =15
2.
While loop
While is similar to the For Loop, but it is more
friendly in writing and faster than For Loop. Because in For Loop you
initialize variable, check condition and increment/decrement the variable in a
single line but in while loop it is different, let’s see examples.
Example 1:
int i = 0;
while (i<10)
{
Console.WriteLine(i);
i++;
}
This example
also print 0 – 9 like for loop example 1 above.
Example 2:
This example
is print odd number 0-10.
int a=0;
while(a<=10)
{
if (a % 2 == 1)
{
Console.WriteLine(a);
}
a++;
}
Output
1
3
5
7
9
We can also
use nested while loop
Example 3:
int i = 0;
int j = 0;
while (i<5)
{
while (j<3)
{
Console.WriteLine("outer loop
value {0} and inner loop value {1}", i, j);
j++;
}
j = 0;
i++;
}
This example
also work same as example 3 of For Loop.
3.
Do While
Do while loop is similar to the while loop but it
always run at least one time if the condition false. You can also perform same
task done by while loop.
Example:
int age = 13;
do
{
Console.WriteLine("your age is
13 don't come again");
}
while(age>=18);
above code run one time and print “your age is 13 don't come again”.
4.
Foreach loop:
The foreach loop navigates through each value in the
specified list (like array). foreach loop will be executed until the last value
in the list. Usually use when we don’t know how many elements have the array
contains.
Example:
In this example array is use, we will see array in
detail but right now see how array object created.
3. Jump constructs:
Jump statement transfers control from one block of code
to another block of code within the program. There are four types of jump
constructs.
Types of jump statement
- Break
- Return
- Continuous
- Goto
1.
Break:
Break jump construct can be used in the selection and
loop constructs. It is commonly use in switch case construct and some time in
while loop for immediate terminate the loop.
Example;
int a = 0;
while (a <= 10)
{
Console.WriteLine(a);
a++;
break;
}
Output:
0
The loop should be executed until to become 10 but when
the while loop execute first time it see the break statement it terminate.
2.
Continue:
Uses in loop constructs, denoted by continue keyword.
It ends the current iteration of the loop and transfers the control back to the
loop.
Example:
for (int i = 0; i
< 10; i++)
{
if (i == 0)
{
continue;
}
Console.WriteLine(i);
}
Output:
It should print 0-9 time but it does not print 0 why?
Because when loop executes first
time it find ‘if condition’ which is i==0 then it enter in ‘if’ statement scope
where it see continue jump statement which tell it to go to beginning of the
loop. So in this way it does not go to Console.WriteLine(i); at first time.
1
2
3
4
5
6
7
8
9
3.
Goto:
Goto statement is denoted by goto keyword. By the goto
keyword you can directly execute a statement which have label.
Example:
int a = 1;
output:
Console.WriteLine("no
is "+a);
a++;
if (a <= 5)
{
goto output;
}
Output:
no
is 1
no
is 2
no
is 3
no
is 4
no
is 5
First time compiler does not read output label because
it will call only when goto statement call it. Then compiler print ‘no is 1’
and a is increment by 1, then it check the condition and enter in if statement
scope where it see goto statement and then go to output label, this action will
be remains until the if statement become false.
4.
Return:
The return statement returns a value of an expression,
variable etc. or is used to transfer the control to the method.
Note: method in C# is similar to the function in C
language; we will see methods in our next OOP lectures.
Example: 1
class Program
{
static void Main(string[] args)
{
int mysum = Program.myMethod(10,20);
Console.WriteLine("sum of two
varibles {0}",mysum);
Console.ReadLine();
}
static int myMethod(int a, int b)
{
return a+b;
}
}
Output:
30
In this
example, I created static myMethod() method which return sum of two variables.
Notice that I
used Program.myMethod(10,20); here program is a class, In object oriented
programming almost work done by classes and object. As you can see myMethod()
is inside the Program Class and it is static so it call by class name, we will
see static classes, static method in our OOP
lectures.
Example 2:
int
i=0;
if
(i / 2 == 0)
{
return;
}
else
{
Console.WriteLine("this is else body");
}
Result:
When the statement true the screen will terminate due
to return keyword, because return is also use for terminate the program.