Control Instructions and if,else if and nested if in C Language


 Control Instructions in C

Program is a set of instructions. We know that each instruction of the program is executed by processor. Processor executes instructions one by one. Whatever we write in our program is executed in the same order as they appear in the program. We can say processor executes instructions in a sequential manner. At a particular instant processor is executing some line of code, we say control pf processor is on that line. Processor's control moves from one line to another. This movement of control is known as flow of the program.

Sometimes, it is required that the program's flow shouldn't be sequential. For example, we want to execute first line of our program, then second and third line but after executing third line, we may want to skip fourth line and jump to the fifth line. In such situations, we use control instructions.
Control instruction gives power to decide the flow of the program.

There are four types of control instructions:



DECISION CONTROL INSTRUCTION









if  Statement

The if statement evaluates the test expression inside the parenthesis.

If the test expression is evaluated to true (nonzero), statements inside the body of  if  is executed.

If the test expression is evaluated to false (0), statements inside the body of  if  is skipped from execution.


EXAMPLE OF IF 

























OUTPUT 1  -- 


Enter a number 23

Positive number


OUTPUT 2 --


Enter a number  -2

Non positive number



if else statement 



The  if else  statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).


Syntax of   if else 



if test expression is true, codes inside the body of  if  statement is executed and, codes inside the body of   else  statement is skipped.

if test expression is false, codes inside the body of   else  statement is executed and, codes inside the body of  if  statement is skipped.

Example --






















OUTPUT --1

Enter a number 20

Positive Number


OUTPUT --2

Enter a number -3

Non Positive number



Nested if else statement (if ..elseif..else statement)



The if..else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.


The nested if..else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.

Syntax of if..elseif.. else statement


if (test expression1)
{
// statements executed if test expression 1 is true
}
else if(test expression 2)
{
// statements executed if test expression 1 is false and  test expression 2 is true.
}
else if(test expression 3)
{
// statements executed if test expression 1 test expression 2 are false and  test expression 3 is true.
}
else
{
//  all statements are false then this statement is executed
}

Example of Nested if..else statement


#include <stdio.h>
int main()
{
int num1, num2;
printf("enter two integers : ");
scanf("%d %d, & num1, &num2);

if (num1 == num2)
{
printf("Result : %d = %d", num1,num2);
}
else if (num1 = = num2)
{
printf(" Result: %d = %d", num1, num2);
}
else
{
printf(" result: %d < %d", num1, num2);
}
return 0;
}

Output 


Enter two integers: 12 23

result: 12< 23

Comments

Popular posts from this blog

Notepad, Wordpad and Paint

HOW TO PASS CCC EXAM WITH ONE ATTEMPT (NIELIT)

Inheritance in C++ Language(part 1)