Monday, May 21, 2007

Flow Control

Boolean Logic:
The bool type can only hold either true or false value.
Boolean Comparison requires the use of Boolean operators (also known as relational operators).

The Relational Operators are as follows:
Operator --> Category --> Example Expression --> Result
== Binary Var1 = var2 == var3 Var1 is true if var2 = var3 or else var1 is false.
!= Binary Var1 = var2 != var3 Var1 is true if var2 is not equal to var3 or else var1 is false.
< var1 =" var2"> Binary Var1 = var2 > var3 Var1 is true if var2 is greater than var3 or else var1 is false.
<= Binary Var1 = var2 <= var3 Var1 is true if var2 is less than or equal to var3 or else var1 is false. >= Binary Var1 = var2 >= var3 Var1 is true if var2 is greater than or equal to var3 or else var1 is false.

Other Boolean Operators are:
Operator --> Category --> Example Expression --> Result
! Unary Var1 = ! var2 Var1 is true if var2 is false or else var2 is false. (Logical NOT)
& Binary Var1 = var2 & var3 Var1 is true if var2 and var3 both are either true. Otherwise var1 is false. (Logical AND)
Binary Var1 = var2 var3 Var1 is true if either var2 or var3 or both are true or else var1 is false. (Logical OR)
^ Binary Var1 = var2 ^ var3 Var1 is true if either var2 or var3, but not both, are true or else var1 is false. (Logical XOR or Exclusive OR).

Conditional Boolean Operators are:
Operator --> Category --> Example Expression --> Result
&&amp; Binary Var1 = var2 && var3 Var1 is true if var2 and var3 both are either true. Otherwise var1 is false. (Logical AND)
Binary Var1 = var2 var3 Var1 is true if either var2 or var3 or both are true or else var1 is false. (Logical OR)

Note: the results of operators && and are exactly the same as & and , but there is an important difference in the way this result is obtained, which can result in better performance.
Both of these operators (&&, ) first look at the value of their first operand (var2) and based on the value of this operand many not need to proceed to process the second operator (var3) at all. This isn’t the case for the & and operator. Because of this conditional evaluation of operands, you will see a small performance increase if you use && and operators instead of & or operators.

How will the conditional operators increase the performance?
For ex: if the value of the first operand of the && operator is false, then there is no need to consider the value of the second operand, because the result will be false regardless of the value of the second operand. Similarly, the operator will return true if its first operand is true, regardless of the value of the second operand.

Why do we still have the & and operator when the && and operators improve the performance?
These operators may be used to perform operations on numeric values. They operate on the series of bits stored in a variable rather than the value of the variable.

Bitwise Shift Operators are:
Operator --> Category --> Example Expression --> Result
>> Binary Var1 = var2 >> var3 Var1 is assigned the value obtained when the binary content of var2 is shifted var3 bits to the right.
<< var1 =" var2" var2 =" 10," var3 =" 2;" var1 =" var2" var1 =" 40" 101000 ="=""> Category --> Example Expression --> Result
&= Binary Var1 &= var2 Var1 is assigned the value that is the result of var1 & var2.
= Binary Var1 = var2 Var1 is assigned the value that is the result of var1 var2.
^= Binary Var1 ^= var2 Var1 is assigned the value that is the result of var1 ^ var2.
These work with both Boolean and numeric values in the same was as &, , and ^
Note: &= and = use the & and operators and not the && and operators

Bitwise Shift Assignment Operators are:
Operator --> Category --> Example Expression --> Result
>>= Unary Var1 >>= var2 Var1 is assigned the value obtained when the binary content of var1 is shifted var2 bits to the right.
<<= Unary Var1 <<= var2 Var1 is assigned the value obtained when the binary content of var1 is shifted var2 bits to the left. Operator Precedence Table: Precedence --> Operators
Highest ++, -- (used as prefixes); (), +, - (unary), !, ~
*, /, %
+, -
<<, >>
<, >, <=, >=
==, !=
&
^
&&
=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, =
Lowest ++, -- (used as suffixes)

The goto statements:
What are the benefits and problems of using the goto statement?
Benefit: Very simple way of controlling what code is executed when.
Problem: Excessive use of this technique can result in difficult to understand spaghetti code.
The goto statement is used as follows:
goto
and labels are defined in the following way:
:
Example:
int myInteger = 5;
goto myLabel;
myInteger += 10;
myLabel:
Console.WriteLine(“myInteger={0}”, myInteger);

What is Branching in C#?
The Branching is the act of controlling which line of code should be executed next. The line to jump to is controlled by some kind of conditional statement. The three branching techniques available in C# are:
· The ternary Operator
· The if statement
· The switch statement

What is Ternary Operator?
The simplest way of performing a comparison is to use the ternary (or conditional) operator.
Unary operator works on one operand. Binary Operator works on two operands. The Ternary Operator works on three operands.
The syntax is as follows:
? :
Example:
String result = (myInteger <10) ? “Less than 10” : “Greater then or equal to 10”; Note: This operator is fine for simple assignments such as this, but isn’t really suitable for executing larger amounts of code on a comparisons. A much better way of doing this is to use the “if “statement. How is If Statement different from the Ternary Operator? The if statement is a far more versatile and useful way of making decisions/ Like Ternary Operator the if statements don’t have a result; instead use the if statements to conditionally execute other statements.
Syntax:
If ()
{
is true>;
}
else
{
is false>;
}

Syntax:
If (var1 == 1)
{
// Do something
}
else if (var1 == 2)
{
// Do something else
}
else if (var1 == 3 var1 == 4)
{
// Do something else
}
else
{
// Do something else.
}
Note: When making multiple comparisons such as the above syntax, it can be worth considering the switch statement.

How is the Switch statement different from If statement?
The switch statement is very similar to the if statement (in this it executes code conditionally based on the value of a test). However, switch allows you to test for multiple values of a test variable in one go, rather than just a single condition.
Synatx:
Switch ()
{
case :
== >
break;
case :
== >
break;
……………
case :
== <> >
break;
default:
!= <> >
break;
}
The value in is compared to each of the values (specified with case statements), and if there is a match, then the code supplied for this match is executed. If there is no match, then the code in the default section is executed.

What is the purpose of adding break point with the break, goto, or a return in the Switch Case statements?
Adding a break point with break, goto, or return ensures that a valid execution path exists through the structure in all cases.

Do switch statements have any limit on the amount of case: sections?
Switch statements have no limit on the amount of case: sections they contain.

What is Looping in C#.NET?
Looping is when statements are executed repeatedly. It means that you can repeat operations as many times as you want without having to write the same code each time.
Types of Loops available in C# are:
· Do loops
· While loops
· For loops
· Interrupting loops
· Infinite loops

Explain the do Loops?
In do loops, the code you have marked out for looping is executed, then a Boolean test is performed, and codes executes again if this test condition is true, and so on and the loop exits when the test condition evaluates to false.
Synatx:
do
{

} whilr ();
where the evaluates to a Boolean value.
The semicolon after the while statement is required.

Example:
Int i = 1;
do
{
Console.WriteLine(“{0}”, i++);
} while (i<= 10); How is While loop different from the do loop? While loops are very similar to do loops. The Boolean test in a while loop takes place at the start of the loop cycle, not the end. If the test evaluates to false then the loop cycle is never executed. Instead, program execution jumps straight to the code following the loop Syntax: while ()
{

}

Example:
int i;
while (I <= 10) {

Console.Writeline(“{0}”, i++)
}

Explain the For Loop?
For Loop is the one that executes a set number of times and maintains its own counter. To define a for loop you need the following information:
· A starting value to initialize the counter variable
· A condition for continuing the loop, involving the counter variable
· An operation to perform on the counter variable at the end of each loop
Syntax: for (; ; )
{
}
This works in exactly the same way as the following while loop:

while loop Syntax:
while ()
{
}
Note: the format of the for loop makes the code easier to read, because the syntax involves the complete specification of the loop in one place, rather than divide it over several statements in different areas of the code.

What are the Interrupting Loops?
Interrupting Loops provide the finer-grained control over the processing of looping code. Four commands for the Interrupting Loops are as follows:


· break --> Causes the loop to end immediately, and execution continues at the first line of code after the loop.
Example:
int i;
while (I <= 10) {

if (i == 6)
break;
Console.Writeline(“{0}”, i++)
}

· continue --> causes the current loop cycle to end (stop) immediately, not the whole loop (execution continues with the next loop cycle).
Example:
int i;
for (i = 1; i <= 10; i++) {

if ((i % 2) == 0)
continue;
Console.Writeline(i);
}

· goto --> Allows jumping out of a loop to a labeled position (not recommended if you want your code to be easy to read and understand).
Example:
int i;
while (I <= 10) {

if (i == 6)
goto exitPoint;
Console.Writeline(“{0}”, i++) }
Console.WriteLine(“This code will never be reached.”);
exitPoint:
Console.WriteLine(“This code is run when the loop is exited using goto.”);

· return --> jumps out of the loop and its containing function

What are the Infinite Loops?
The loops that never end, are called as infinite loops.
Example:
While (true)
{
// code in loop
}
You can always exit infinite loops using code such as break statements or manually by using the Windows Task Manager.

No comments: