Thursday, 25 July 2013

CONTROL INSTRUCTIONS IN C LANGUAGE

What is control? 
Program is a set of instructions. We know that each instruction of the program is executed by processor. Processor executes instructions one by one. What ever we write in our program will be executed in the same order as they appear in the program. So we can say processor execute instructions in a sequential manner. At a particular instant processor is executing some line of code, we say control of processor is on that line. Processor’s control moves from one line to another. We say this movement of processor control goes in sequence.

Types of control instruction:
l  Sequence control instruction
l  Decision control instruction
l  Iterative control instruction
l  Switch case control instruction
l  goto instruction

Sequence control instruction:
Processor control moves in a sequential manner. We have to do nothing to implement sequence control instruction. This is just a concept that your program always run in a sequence.

Decision control instruction:
Decision control instruction is also known as selection control instruction. As the name implies the job is selection control instruction is to select a set of code for execution on the basis of some condition.

We can implement decision control instruction in three ways:
l  if
l if-else
l  conditional operator (Ternary Operator)

Syntax of if:

main()
{
            ..…
…..
            if(some condition)
            {
                        Statement1;
                        Statement2;
                        ….
            }
            …
}

if is a keyword which let compiler to identify decision control instruction. Immediately after if some condition is there. This condition is any valid expression in C. If the result of expression is non-zero it is considered as TRUE otherwise FALSE.
Immediately after this condition there is a block of code. Since this block is immediately after if, it is known as if block. Whatever we write in if block will be execute only when condition is TRUE.
When condition is false control skip if block and execute statements written after if block.
Example:
main()
{
   int marks;
   printf(“Enter marks ”);
   scanf(“%d”,&marks);
   if(marks>=33)
   {
            printf(“You are PASS”);
   }
  if(marks<33)
  {
            printf(“You are FAIL”);
  }
}
Sample Output:

Enter marks 45
You are PASS

Sample Output:

Enter marks 23
You are FAIL

In this program output depends on the value given by user. Variable marks hold the value entered by user. We have used two if statements. In the first if statement we use the condition marks>=33, thus if the marks are greater than or equal to 33 condition becomes TRUE, so if block executed, otherwise if block is skipped.
Whatever may the result of first if condition, control has to reach second if statement.
If marks are less than 33 condition will be TRUE and execute if block otherwise if block is skipped.

Syntax of if-else:

main()
{
            ..…
…..
            if(some condition)
            {
                        Statement1;
                        Statement2;
                        ….
            }
            else
            {
                        Statement1;
                        Statement2;
                        ….
            }
            …
}

This is similar to if but the else block is new add on. If the condition of if is TRUE if block will be executed and if the condition of if is FALSE else block will be executed.
It is important to mention that only one from the two blocks (if block and else block) can be executed as the condition may have only two results, TRUE or FALSE

You can use if statement without else block but else must have paired with if.
Else block should appear immediately after if block otherwise an error occurred during compilation.

Example:
main()
{
   int marks;
   printf(“Enter marks ”);
   scanf(“%d”,&marks);
   if(marks>=33)
   {
            printf(“You are PASS”);
   }
  else
  {
            printf(“You are FAIL”);
  }
}

The same program was discussed using only if statements. This one is refined version of program and hence better than that of the previous one.

Notice that, there is only one condition need to be evaluated, if the condition is TRUE if block will work otherwise else block will be executed.

Note: If there is only one statement in if block then mentioning block using curly braces is optional. Same rule is applied to else block.

Conditional operator ( ? :)
It is also known as ternary operator which means operator need three operands to perform its operation.

Syntax:
Expression1 ? expression2 : expression3;

Expression 1 is a condition, which is first evaluated as TRUE or FALSE. If the condition is TRUE executes expression2 otherwise execute expression3.

Conditional operator works similar to if-else, but we do not have to use keyword if and else.

Example:
main()
{
  int x,y;
  printf(“Enter two numbers”);
  scanf(“%d%d”,&x,&y);
  x>y ? printf(“%d is greater”,x) : printf(“%d is greater”,y);
}
Output:
Enter two numbers45
33
45 is greater

In this program user enters two numbers which is then get stored in x and y. Notice the last line of the program that is conditional operator, which is used to select one from the two printf() statements. If x>y then value of x get printed otherwise value of y is printed.



Selective assignment

main()
{
  int x,y,max;
  printf(“Enter two numbers”);
  scanf(“%d%d”,&x,&y);
  max=x>y ? x : y;
  printf(“Greater number is %d”,max);
}

Output is:
Enter two numbers85
100
Greater number is 100

In this program conditional operator is used to select one from x and y to assign value of either x or y in variable max.

If-else ladder:

Syntax:
if()
{
 ----------
 ---------
}
else if( )

{
 ----------
 ---------
}
else if( )
{
 ----------
 ---------
}
else
{
 ----------
 ---------
}

Example:

main()
{
   int year;
   printf(“Enter a year”);
   scanf(“%d”,&year);
   if(year%4!=0)
            printf(“Not a Leap year”);
   else  if(year%100!=0)
            printf(“Leap year”);
   else if(year%400!=0)
            printf(“Not a Leap Year”);
   else
            printf(“Leap Year”);
}

The above program is to check whether a year is leap year or not. You can analyze how we used if-else ladder in the above program.

Nested if-else:

When if-else statements written in if block or else block, it is called nested if-else.
Example:

Program to find greater among three numbers:

main()
{
            int a,b,c;
            printf(“Enter three numbers: “);
            scanf(“%d%d%d”,&a,&b,&c);
            if(a>b)
            {
                        if( a>c)
                                    printf(“%d is greater”,a);
                        else
                                    printf(“%d is greater”,c);
            }
            else
            {
                        if( b>c)
                                    printf(“%d is greater”,b);
                        else
                                    printf(“%d is greater”,c);
            }
}

In the above program if a is greater than b then if-block is executed otherwise else block is executed. Notice that another if-else reside inside if-block and else-block. This sort of structure is known as nested if-else.

1 comment: