C# Self Tutor (chapter 3)
C# STATEMENTS
BLOCKS:
The blocks are set by left hand brace ({ ) and right hand brace ( }). It allows multiple lines to act singularly. Blocks are used to specify the boundaries in different language elements like classes, structures and interfaces.
LABELS and GOTO:
Labels are used as identifiers within a program. The control can jump to any location within a program by using the goto and label statement. A label is any valid identifier within colon ( : ).
CONST DECLARATION:
Local constants can be declared using const keyword. Its scope is valid within the block declaring the variable.
IF STATEMENT:
The if statement executes statement based on the value of a Boolean expression.
Using class
Class ebony
{
Public static void main (string [] args)
{if (args.length==0)
Console.writeline ("null string");
Else if (args.length! =0)
Console.writeline ("not a null string");
}
}
Output
Null string
The switch statement is a control statement that handles multiple selections by passing control to one of the case statement within its body. The switch statement takes the form
Switch (expression)]
{
Case constant-expression:
Statement
Jump statement
[default: statement]
}
Where:
Expression: an integral or string type expression.
Statement: the embedded statements to be executed if control is transferred to the case of the default.
Jump-statement: a jump statement that transfers control out of the case body.
Constant-expression: control is transferred to a specific case according to the value of this expression.
Control is transferred to the case statement whose constant-expression matches expression. The switch statement can include any number of case instances, but no two cases constant within the same switch statement can have the same value. Execution of the statement body begins at the selected statement and proceeds until the jump-expression control out of the case body.
Unlike C++ switch statement, C# does not support an explicit fall thru from one case label to another. If expression does not match any constant-expression, control is transferred to the statement that follows the default label. If there is no default label, control is transferred outside the switch.
DO STATEMENT:
The do 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);
Where:
Expression: 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: the embedded statements are executed.
Unlike while statements, the body of loop of the "do" statement is executed at least once regardless of the value of the expression.
FOR STATEMENT:
The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false; takes the following form:
For ( [initializes]; [expression]; [iterations])
Statements;
Where:
Initializes: a comma separated list of expressions or assignment statements to initialize the loop counters.
Expression: 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.
Iterations: expression statements to increment or decrement the loop counters.
Statement: the embedded statements to be executed.
The FOR statement executes the statements repeatedly as follows:
First, the initializes are evaluated
Then, while the expression evaluates to true, the statements are executed and the iterators are evaluated.
When the expression becomes false, control is transferred outside the loop.
Because the test of expression takes place before the execution of the loop, a for statement executes zero or more.
FOREACH, IN STATEMENT:
The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate thru the collection to get the desired information, but should not be used change the contents of the collection to avoid unpredictable side effects. The statements take the following form:
Foreach (type identifier in expression)
Statement;
Type: the type pf the identifier
Identifier: the iteration variable that represents the collection element.
Expression: object collection or array expression. The type of the collection element must be convertible to the identifier type.
Statement: the embedded statements to be executed.
The embedded statements continue to execute for each element in the array or collection. After the iteration has been completed for the all elements in the array or collection, control is transferred to the next statement following the foreach.
When used with an array, the foreach statement repeats the embedded statements for each element in the array.
Example:
Foreach (int i in array)
{
If (i%2==0) even++;
}
The first type of loop that java provides is called a while loop. A while loop checks to see if a condition is true and if so, executes a statement or set of statements. After executing those statements, the condition is again checked. Of it is still true, it executes the statements over again, continuing in the same fashion until the condition is finally false.
The basic form of the while loop is:
While (expression) statement
Expression: 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: the embedded statements to be executed.
Because the test expression takes place before execution of the loop, a while loop executes zero or more.
A while loop can be terminated when a break, goto, return or throw statement transfers control outside the loop; pass control to the next iteration without exiting the loop, use the continue statement.
JUMP STATEMENTS:
Branching is performed using jump statements, which cause an immediate transfer of the program control. The following keywords are used in jump statements:
Break
Continue
Default
Goto
Return
The break statement terminates the closest enclosing loop or conditional statement in which it appears. Control is passed to the statement that follows the terminated statement, if any. The break statement takes the form:
Using system;
Class ebony
{
Public static void main ()
{
For (int i=1; i<=10; i++)
{
If (i==5)
Break;
Console.writeline (i);
}
}
}
Output
1
2
3
4
The continue statement passes control to the next iteration of the enclosing iteration in
Which it appears. It takes the form:
Using system;
Class ebony
{
Public static void main ()
{
For (int i=1; i<=5; i++)
{
If (i==4)
Continue;
Console.writeline (i);
}
}
}
Output:
1
2
3
5
The goto statement transfers the program control directly to a labeled statement. It takes one of the following forms:
Goto identifier;
Goto case constant-expression;
Goto default;
Identifier: a label.
Constant-expression: a switch-case label
In the first form, the identifier indicates a label located in the current body, the same lexical scope, or an enclosing scope of the goto statement.
A common use of goto is to transfer control to a specific switch-case label; or the default label in a switch statement.
The goto statement is also useful to get out of deeply nested loops.
RETURN STATEMENTS:
The return statement terminates execution of the method in which it appears and returns control to the calling. It can also return the value of the optional expression. If the method is of the type void, the return statement can be omitted. The return statement takes the form:
Return [expression];
Where: expression: the value returned by a method. The expression is not used with methods of the type void.
THROW and CATCH STATEMENTS:
The throw statement is used to throw an exception during the program execution. The throw exception is usually of type system.exception or a type of class derived from system.exception. The example below throws the new exception divide by zero.
Using system
Class ebony
{
Public static void main ()
{
Values (3, 0);
}
Static int values (int a, int b)
{
if(b==0)
throw new exception ("divide by zero");
return a/b;
}
}
Output:
Unhandled exception: system.exception: divide by zero
At test. values (int32 a, int32 b)
At test. main ()
Usually throw statement is used with try-catch statements. The try statements provide a mechanism for catching the exception that occur during the execution of the code of the block. The code to handle the exception is provided within the catch block. The code within the finally block is always executed when the control leaves the try statement.
Using system
Class ebony
{
Public static void main ()
{
Try
{int [] a={1,2,3,4,5};
Console.writeline (a [36]);
}
Catch (IndexOutOfRangeException ff)
{
Console.writeline (f.message);
}
}
}
Output:
Index was outside the bounds of the array.
The message property of the exception is used to display the description of the execution.

3 Comments:
Great work!
[url=http://dauhpatc.com/xmiu/qthx.html]My homepage[/url] | [url=http://iszxqsuj.com/fuot/pkke.html]Cool site[/url]
12:14 PM
Thank you!
My homepage | Please visit
12:14 PM
Good design!
http://dauhpatc.com/xmiu/qthx.html | http://gnnbiiao.com/wtyg/qqjv.html
12:14 PM
Post a Comment
<< Home