C language : Chapter 2

 Decision Control Instructions

There are 3 major decision control instructions.

  1. if
  2. if-else
  3. switch
  4. the condition operators [ ternary operators]
Syntax of If
if(expression)
execute the statement;                         //  ; statement terminator

If the expression is true, execute the immediate next statement. 
If the expression is false skip the statement.

How do we evaluate if the expression is true or false.
with the help of relational operators, we will evaluate that the expression is true or false.
we will determine whether two statements are equal, unequal or one is greater than other.





In C language, all arithmetic expressions are valid. They are considered as true or non-zero values.

In C language, every test expression is evaluated as zero or non-zero value. Zero is equal to false and non-zero is equal to True.

Scope of if
By default, scope of if is the immediate next statement after it. If we want more than one statement to get executed on the satisfaction of the condition, then it should be included within the pair of braces {}.
otherwise only immediate next statement will get executed.
The group of statements after if upto but not included if is called if block.
The group of statements after else is called else block.
The group of statements in the if block and else block must be indentated to the right.

Please note- Inculcate the habit of indentation, otherwise we will end up writing a program which nobody can understand including us at later stage.

Else-if reduces the indentation of statements.

Logical Operators

Logical NOT     !

Logical AND &&

Logical OR          || 

&&   ||           are called binary operators

& |                 are bitwise operators.

!                     unary operators.


The NOT operator
The not operator reverses the result of the operations it operates on.
If the test expression is evaluated as zero value (false), applying not operator to it results to non-zero value (true)
If the test expression is evaluated as non-zero (True), applying not operator to it results to zero value (false)

@example-
!(y<10)   means y should not less than 10 then expression is true.
we can also write above expression like (Y>=10)

Another common mistake we should avoid.
if(i==5);
printf("You entered 5");
Since the default scope of is the immediate next statement following the if condition and Next statement nothing is there before semicolon(;) so compiler will not execute anything.

The conditional Operators
The conditional operators ? : are called ternary operators, since they take three arguments. If-then-else
They form a kind of shortened. [ if-then-else] 
Expression1? Expression2:Expression3
If expression 1 is true return expression 2 otherwise return expression 3

@example

1. It is not necessary that ternary operators should always be used with arithmetic opeartors.



2. The ternary operators can also be nested.


@example
a>b?g=a:g=b
//It will gives an error - L value required.
Solution
a>b?g=a:(g=b);
In the absence of parenthesis, the compiler believes that b is being assigned to the result of the expression to the left of the second = i.e.  (g=a), hence reports an error.

The limitation of conditional operators is that only one C statement can occur after ? or :
In serious programming, ternary operators are rarely used like we rarely use if -else ladder.  

Summary: -
  1. There are 3 major decision-making instructions.
    1. if
    2. if-else
    3. switch
    4. The conditional operators.
  2. The default scope of if statement is the immediate next statement after if.
  3. Pair of braces should be used if more than one statement need to be executed.
  4. An if block need not to be associated with else block always, but else block should always be associated with if block.
  5. if the outcome of if else ladder is always one of the two statements, if should be replaced with else-if or logical operators.
  6. && || are binary operators and & | are bit wise operators , ! are unary operator.
  7. In C language every test expression is evaluated as zero or non-zero value. Zero equals false, non-zero equals true.
  8. Assignment operators used with ternary operators must be enclosed with a pair of parentheses.

 







Comments

Popular posts from this blog

C Language: Chapter 4 The case control structure.