Control statements
In C#,there is three types of control statements
· Selection statements
· Iterator statements
· Jump statements
Selection statements:
If and if-else:
By if statement, we can access our code through condition which will be placed in ‘if’. if that condition is true then the control goes to ’if’ block otherwise control does not execute the if statements.
If(condition)
{
///statements
}
By using if-else statement, we can get the feature ‘selection’ by accessing our code in different conditions.
Here,’If’ condition is not true then the control directly goes to else block.if ‘if’ condition is true then control executes the if block and else block does not executed.
Example:
Class P
{
Static void main (string[] args)
{
Int a,b;
a =10,b=15;
if ( a > b )
{
Console.writeline (“a is greater than b”);
}
else
{
Console.writeline (“b is greater than a”);
}
}
}
Switch ….case:
In switch-case statement, it takes a single integer value in switch statement, and that value will be compared with case statements values.
If our input does not match with case statements values then ‘default’ statement will executes.
Example:
Class SwitchExample
{
Public static void main(string[] args)
{
Int a;
Console. Write (“enter a number between 1 and3”);
Console.ReadLine (a);
Switch (a)
{
Case 1:
Console.writeline (“ur number is {0}”,a);
break;
Case 2:
Console.writeline (“ur number is{0}”,a);
break;
Case 3:
Console.writeline (“ur number is{0}”,a);
break;
default:
Console.writeline (“ur number is not between 1 and 3”);
break;
}
}
}
Iterator statements:
For:
the simplest loop statement is ‘for’ statement
Syntax for ‘for’ loop is
for ( value; condition expression ; increment or/decrement )
{
Statements
}
For statement has three parts
First part is assigning a value to variable and the second part is expression and the third part is increment or decrement
Example:
Class F
{
Static void main (string [] args)
{
for (int i = 0 ; i < 10 ; i ++)
{
Console.writeline ( i );
}
}
}
For each:
This statement uses two operands,
First operand is always the type that is adding to collection or array which is implemented in second operand.
Syntax:
foreach (type identifier in array/collection)
{
Statements
}
Example:
class FE
{
Static void main( string[] args)
{
arraylist x=new arraylist(20);
foreach(int i in x)
{
Console.writeline (i);
}
}
}
While:
Another simple loop statement is ‘while’. In ‘while’ we have the condition and if that condition is true then only the while block executes.
If the the condition is false then the control ignores the while block and executes the statements followed by while block.
Syntax;
While(expression)
{
Statements
}
Example:
Class W
{
Static void main(string[] args)
{
int x=0;
While( x < 5)
{
x++;
console.writeline (x);
}
}
}
Do-While:
The another type of while loop is do-while.
In do-while, while loop executes at least once. This is the difference between do and do-while.
Syntax:
do
{
Increment or decrement;
Statements
}while(expression)
Example:
Class DW
{
Static void main (string[] args)
{
int x=0;
Do
{
X++;
Console.writeline(x);
}while (x < 7);
} }
Jump statements:
Continue:
Continue is useful whenever execution of code in the loop should return for iteration irrespective of condition.
If we use continue in while loop, then expression in while can evaluate again and remaining code will ignored.
Example:
Class c
{
Static void main(string[] args)
{
int j=0;
While (j < 14);
{
J++;
if (j = = 5)
{
J + +;
Continue;//control goes to iteration forcefully
}
}
}
}
break:
Break statement breaks the execution code with in the block or loop. By using break , control will come outside from the class forcefully.
Example;
Class B
{
Static void main (string[] args)
{
int k=o;
While (k < 15);
{
K++;
if ( k==5)
break;
}
K++;
}
}
goto:
goto command is used in the program that it will moves the control from one place to another palce forcefully.
Example:
Class G
{
Static void main (string[] args)
{
int t=o;
While (t < 15);
{
if (t ==5)
goto cleanup;
}
Cleanup:
Console.writeline (t);
}
}
No comments:
Post a Comment