Monday, 20 July 2015

Conditional statements: if-else, else-if and switch in C !

The if-else is used to express decisions.Formally, the syntax is

if(expression)
    statement1
else
    statement2

where the else part is optional. Here, the expression is evaluated; if is true, statement1 is executed. if it is false and there is an else part(else part is optional), statement2 is executed.
                                                   


Let's take a example:
#include <stdio.h>
int main()
{
   int x = 1;

   if ( x == 1 )
      printf("x is equal to one.\n");
   else
      printf("For comparison use == as = is the assignment operator.\n");

   return 0;
}


It reads like it works. If ‘x’ equals 1, print a message "x is equal to one." . Note carefully the use of the comparison operator (= =), not assignment (=)

Now take a code examples of if statements you will see in your hacker journeys :
sockfd = socket(AF_INET, SOCK_STREAM, 0);   
if (sockfd < 0)        
    perror("ERROR opening socket");
else
    printf("socket is created successfully! ");

   
Here we create a variable named sockfd to hold the socket descriptor. socket() simply create an endpoint for network communication between client and server. we will discuss about socket in detail in networking section.If socket is successfully created, simply return an integer of socket descriptor otherwise return -1 .

else-if :
The general syntax of else-if is :
if(expression)
    statement
else if (expression)
    statement
else if (expression)
    statement
else if (expression)
    statement
else
    statement

This sequence of if statements is most general way of writing multi-way decision. the expression are evaluated in order; if any expression is true, the statement associated with it is executed, and this terminates the whole chain. The last else part handles the "none of the above" or default case where none of the other condition is satisfied.

switch :
The switch is also a multy-way decision same as else-if but switch is more effective then else-if.
The syntax is :
switch(expression){
    case const-expr:
        statements
    case const-expr:
        statements
    default:
        statements
    }

Each case is labeled by constant expressions. If a case matches the expression value, execution starts at that case. All case expression must be different. the case labeled default is executed if none of the other case are satisfied. A default is optional.

switch(i)
{
    case 0:
        printf(“i equals zero!\n”);       
    case 1:
        printf(“i equals one! \n”);       
    case 2:
        printf(“i equals two!\n”);       
    case 3:
        printf(“i equals three!\n”);   
    default:
        printf(“No match found.\n”)
}


The break statement causes an immediate exit from switch.If we didn’t have a ‘break’, once execution is transferred to a ‘case’ from ‘switch’, it will continue all the way through as in previous code.
switch(i)
{
    case 0:
        printf(“i equals zero!\n”);
        break;       
    case 1:
        printf(“i equals one! \n”);
        break;       
    case 2:
        printf(“i equals two!\n”);   
        break;   
    case 3:
        printf(“i equals three!\n”);
        break;   
    default:
        printf(“No match found.\n”)
        break;
}

break and return are the most conman ways to leave a switch.As a matter of good form, put a break after the each case even though it is logically unnecessary.
Now let's take real world example of wu-ftpd 2.6.2 - Remote Root Exploit :
 switch(t_g)
    {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            command=(DEF_COMM);
            break;
        case 6:
        case 7:
        case 8:
            command=(DEF_COMM_OB);
            break;

 }


condition operator ? :
The conditional expression(if-else), written with the ternary operator "? :" provides an alternate way to write conditional statement.
The general syntax is:
expr1 ? expr2 : expr3
In the expression expr1 is evaluated first. If it is true, then the expression expr2 is evaluated. Otherwise expr3 is evaluated. Only one of expr2 and expr3 is evaluated.It is not commonly used, but can be very effective.

Points to remember:
it's good idea to use braces when there are nested ifs.For example, in
if(n>0)
    if(a>b)
        z=a;
    else
        z=b;

the else goes with inner if not with if(n>0) .If that isn't what you want, braces must be used to force proper association:
if(n>0){
    if(a>b)
        z=a;
}
else
    z=b;



If you like this post or have any question, please feel free to comment.

No comments:

Post a Comment

Blogger Widget