about if if-else and conditional operator (AND&&,OR||, NOT! ) in programming language

                          DECISION CONTROL CONTROL INSTRUCTION

A decision control instruction can be implemented in c language.
1) The if statement
2) The if-else statement
3) The conditional statement
Now we understand each one topic separately.
       
                           THE IF STATEMENT
structure of if statement
if(the condition is true)
than execute this statement.

structure of if statement, if program
if statement structure

The keyword if tells the compiler that what follows the keyword is a decision control structure instruction. The condition following the keyword if is always enclosed within a pair of parentheses.if the condition, whatever it is ,is true ,then the statement is executed .
if the condition is not true,then the statement is not executed.instead the program skips past it. But how do we express a condition using C's  'relational' operators. The relational operator allow us to compare two values to see whether they are equal to each other ,unequal or whether one is grater than the other. Here's how they look  and how evaluated in c programming language.

the expression  is  true if
X==Y                 X is equal to Y
Y!=X                  X is not equal to Y
X>Y                   X is grater than Y                           
X<=Y                 X is less than or equal Y
X>=Y                 X is grater than or equal to Y.
The relational operator should be familiar to you except for the equality operator== and the inequality operator != . note that '=' is used for assignment,where ,== is used for comparison of two quantities.
               
                      MULTIPLE STATEMENT WITHIN C
It may so happen that in a program we want more than one statement to be executed if the expression following if is satisfied.
If such multiple statement are to be executed , then they must be placed within a pair of braces.

                         THE IF-ELSE STATEMENT
The if statement by itself will executed a single statement, or a group of statement, when the expression following if evaluates to true .it does nothing when  the expression evaluates to false.Can we execute one group of statement if the expression evaluates to true and another group of statements if the expression evaluates to false? 
if else statement ,if else program
if else statement
                           NESTED IF -ELSE 
   It is perfectly all right if we write an entire if-else construct within either the body of the if statement or the body of an else statement. 
This is called 'nesting ' of ifs.

                    USE OF LOGICAL OPERATOR
   C allows usage of three logical operator , namely, &&,||, and !
these are to be read as 'AND' 'OR' and 'NOT' respectively. In electronics language these are known as gate.
   There are several things to note about these logical operator. Most obviously , two of them are composed of double symbols: || and &&. don't use the single symbol | and &. These single symbols also have  meaning, They  are bit wise operators.The first two operator ,&& and ||, allow to or more conditions to be combined in an if statements.
               
                            THE ! OPERATOR
So far we have use only the logical operator is the NOT operator, written && and ||. The third logical operator reverse the result of the expression evaluates to a non zero value, then applying ! operator to it makes it 1 is considered to be false or true respectively. Here is an example of the NOT operator applied to a relational expression.  ex:  !(y<10 font="">
       
                    THE CONDITIONAL OPERATOR
The conditional operator ? and : are sometimes called ternary operators since they take three arguments . In fact , They form a kind of for shortened if then else. their general form is,
expression 1 ? expression 2 : expression 3
What this expression says is " if expression 1 is true (that is, if its value is non zero) then the value returned will be expression 2. otherwise the value returned will be expression 3."

             HIERARCHY OF OPERATORS REVISITED 

 OPERATOR           TYPE

   !                         logical not
* / %                    arithmetic and modulus 
+ -                        arithmetic
<>  <=, >=           relational
== !=                    relational
&&                       logical AND
||                            logical OR
=                            assignment                       

                  


Previous
Next Post »
}; //]]>