Sunday 6 April 2014

Explain boolean logic.


Boolean logic-They are used to store result of a comparison.It can hold only two values true or false.
Comparison operators-These are used on numeric values,string and boolean values.
Here var1 is bool type,var2 and var3 may vary
        operator      category     example         result
  • = =      binary         var1=var2==var3           var1 value is true if var2 is equal to var3 otherwise value is false
  • !=        binary         var1=var2!=var3            var1 value is true if var2 is not equal to var3 otherwise value is false
  • <        binary           var1=var2<var3            var1 value is true if var2 is less than var3 otherwise value is false
  • >        binary           var1=var2>var3            var1 value is true if var2 is greater than var3 otherwise value is false
  • <=        binary         var1=var2<=var3        var1 value is true if var2 is less than equal to var3 otherwise value is false
  • >=        binary        var1=var2>=var3          var1 value is true if var2 is greater than equal to var3 otherwise value is false       
Examples
comparison operator use on numeric values
  • bool var1;
var1=var2<10;
here if var2 is less than 10 then var1 value is true else false.
comparison operator use on string
  • bool var1;
var1=var2=="lechu";
if var2 value is lechu then var1 value is true else false.
comparison operator use on boolean values
  • bool var1;
var1=var2==true;
if var2 value is true then var1 value is true else false
Note-common error-if var1<var2 it doesn't mean that var2>var1 it may be var1=var2.So don't assume the result.

Boolean operators-These are used on boolean values.

operator    category    example            result
  • !        unary        var1=!var3        var1 value is true if var2 is false otherwise value is false
  • &        binary         var1=var2&var3        var1 value is true if var2,var3 both are true otherwise value is false
  • |        binary        var1=var2|var3        var1 value is true if var2,var3 both or either one is true otherwise value is false
  • ^        binary        var1=var2^var3        var1 value is true if var2 or var3 is true not both otherwise value is false
Example
Boolean operator on boolean values
  • bool var1;
var1=var2&true
here var1 is true if var2 is also true else var1 is false.
conditional boolean operators-This operate same as &,! but difference is,the value of first operand is false there is no need to consider value of second operator.Difference in the way result obtained gives better performance.
operator    category    example            result
  • &&        binary         var1=var2&&var3        var1 value is true if var2,var3 both are true otherwise value is false
  • ||        binary        var1=var2||var3        var1 value is true if var2,var3 both or either one is true otherwise value is false
Example
var1=(var2!=0)&&(var3/var2>2);

Explain flow control and its 2 types branching and looping.

Flow control-There are two methods of controlling program flow ie, controlling order of execution of lines in C# code.They are branching and looping.These methods use boolean logic.
Branching-Here code is executed when the condition given is true.For example-only execute code if my val is less than 10.Result is shown by true or false.
Looping-Here same statement will be executed repeatedly for a certain amount of times,until a test condition is reached.Its type is bool.