9.29.2011

Intro if-else

In C language there are three major decision making instruction as if statement, the if-else statement, and the switch statement. And one also minor decision making instruction is conditional operator.

The keyword if tells the compiler that what follows is a decision control 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, and If the condition is not true, then statement is not executed.

Let's demonstrates the use of if-else with c program:

/*demonstrates of if-else statement*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int n=10;
 if(n>=15)
  printf("n is greater!!");
 else
  printf("n is less than!!");
 getch();
}
Output :
n is less than!!

On execution of this program, when compiler check condition 10>=15, the condition is false so its execute else part statement i.e. n is less than!!.

Now try to write below program for more familiar with if-else:
Q. Write a C program to read quantity and price for each item. Calculate the total expenses, and if the quantity is more than 1000 than giving discount is 20%.

Ans: To see solved program click here

No comments:

Post a Comment