1 if Statement
The if statement is a control statement that executes a block of code
if an expression evaluates to true. It takes this form:
if (expression)
statement1
else
statement2
where
• expression is an expression that can be implicitly converted to bool or
a type
that contains overloading of the true and false operators
• statement1 is the embedded statement(s) to be executed if expression is true
• statement2 is the embedded statement(s) to be executed if expression is false
Note that unlike C++, C# and Java do not allow the expression to be
evaluated to 1 or 0. That is, the integer 1 does not stand for true, and 0 does
not stand for false.
Example:
int a=1;
if(a==i)
{
response.write("The integer value is One");
}
else
{
response.write("The integer value is not One");
}
2 The while Loop
The while statement executes a statement or a block of statements until a
specified expression evaluates to false. It takes this form:
while (expression) statement where
• expression is an expression that can be implicitly converted to bool or a type
that contains overloading of the true and false operators. The expression is
used to test the loop-termination criteria.
• statement is the embedded statement(s) to be executed.
Because the test of expression takes place before each execution of the loop,
a while loop executes zero or more times. A while loop can be terminated
when a break, goto, return, or throw statement transfers control outside the
loop. To pass control to the next iteration without exiting the loop, you
use the continue statement.
while loops can be nested. In nested while loops, a break or a goto statement
in the innermost loop merely transfers control to the outer loop. A return
statement exits from the outermost loop, and a throw statement exits from the
outer loop depending on whether the thrown exception gets caught or
ignored. Listing 5.1 shows the while loop in action.
Listing 1 C# while Loop
using System;
public class Test {
public static void Main(string[] args) {
int i = 0;
int j = 0;
while (j < 5) {
Console.WriteLine("j "+j);
while (i < 8) {
Console.WriteLine("i "+i);
++i;
j +=10;
}
}
}
}
3 The do while Loop
The do while statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false. It takes the following form:
do statement while (expression);
<<Code>>
where
<<Condition>>
expression is an expression that can be implicitly converted to bool or a type that contains overloading of the true and false operators. The expression is used to test the loop-termination criteria.
statement is the embedded statement(s) to be executed. Unlike the while statement, the body loop of the do statement is executed
at least once regardless of the value of the expression.
4 The for Loop
he for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. It takes the following form:
for ([initializers]; [expression]; [iterators]) statement
where
initializers is a comma-separated list of expressions or assignment
statements to initialize the loop counters.
expression is an expression that can be
implicitly converted to bool or a type that contains overloading of the true and false operators. The
expression is used to test the loop-termination criteria.
iterators are expression statements to increment or decrement the loop counters.
statement is the embedded statement(s) to be executed.
The for statement executes the statement repeatedly as follows:
1. The initializers are evaluated.
2. While the expression evaluates to true, the statement(s) are executed and the iterators are evaluated.
3. When the expression becomes false, control is transferred outside the loop
No comments:
Post a Comment
Thanks For your Comments Please Continue with your precious Time and greate Knowladge will be benificial for learner like me thanks a lot...........