Phemmy Google Search

Tuesday, August 5, 2008

C# Flow Control

There are two methods of controlling program flow discussed in this section of our learning.
These are :
  1. Branching : This is where code are executed conditionally depending on the outcome of an evaluation such as "only execute this code if myVal is less than 10".
  2. Looping :This is repeatedly executing the same statement( for a certain number of times or until a test condition has been reached)
Boolean comparisons require the sue of Boolean comparison operators( also known as relational operators). In the examples below var1 is a bool type variable, while the type of var2 and var3 may vary.

1. == (var1=var2 ==var3;) var1 is true if var2 is equal to var3 or false otherwise.
2. != (var1=var2 != var3;) var1 is true if var2 is not equal to var3 or false otherwise.
3. < ( var1= var2 < var3;) var1 is true if var2 is less than var3 or false otherwise.
4. > ( var1= var2 > var3;) var1 is true if var2 is greater than var3 or false otherwise.
5. <= (var1 = var2 <= var3:) var1 is true if var2 is less or equal to var3 or false otherwise. 6. >= (var1 = var2>=var3;) var1 is true if var2 is greater or equal to var3 or false otherwise.

You might use operators such as these on numeric values in code such as :

bool isLessThan10;

isLessThan10=myVal < 10; This code result will be true if myVal store a value less than 10 or false otherwise. You can also use these compariosn operators on their types, such as strings: bool isKarli; isKarli =myString=="Karli"; In this example isKarli will only be true if myString stores the string "Karli"

There are some other Boolean operators that are intended specifically for working with Boolean values, shown below:
1. ! (var1 =! var2;) var1 is true if var2 is false or false if var2 is true (Logical NOT);
2. & (va1= var2 & var3;) var1 is true if var2 and var3 are both true or false otherwise (Logical AND);
3. | (var1= var2 | var3;) var1 is true if either var2 or var3 or both are true or false otherwise (Logical OR); and
4. ^ (var1 = var2 ^ var3;) var1 is true if either var2 or var3 but not both, are true or false otherwise (Logical XOR or exclusive OR).

The & and | operators also have two similar operators, known as conditional Boolean operators, as shown below:
1. && (var1 = var2 && var3;) var1 is true if var2 and var3 are both true or false otherwise (Logical AND) and

2. || (var1 = var2||var3;) Var1 is true if either var2 or var3 (or both) are true or false otherwise. The use of && and || aid better performance than & and | so always use && and || where possible.

Olufemi

No comments: