Within the program a logical test be carried out at some paticular point. several possible action will then be carried out. this is known as branching.
The If Statement
The if statement is used to control the flow of execution of statement . The if statement is it two way statement, if condition is true then execute and if if condition is not true then statement is not execute.
or
The statement portion may be a group of statement or single statement if the condition is true then the statement portion will be execute otherwise the statement portion skipped and next statement will be executed.
SYNTAX OF IF STATEMENT
if(test condition)
{
statement portion;
}
next statement;
The example of if statement
EXAMPLE /*USE OF IF STATEMENT*/
#include<stdio.h>
main()
{
int number;
printf ("Enter a number less than 100\n");
scanf ("%d",number);
if (number<=100)
printf ("Well done");
}
Here on execution of the above program if we type a number less than or equal 100 then we get a message on the screen through printf() function . If we type some other number greater than 100 the program does not to anything.
#include<stdio.h>
main()
{
int quandity,discount=0;
float rate,total;
printf ("Enter quandity and rate");
scanf ("%d%f",&quandity,&rate);
if (quandity>50)
discount=5;
total=(quandity*rate)-quandity*rate*discount/100);
printf("total expenses=Rs.%f",total);
}
In this program enter is greater than 50 then the variable discount which was earlier set to zero now get a new value 5 and using this new value total expenses are calculated and printed. Again if quantity is less than 50 discount will be 0 and hence expression after the minus sign evaluate to zero there by offering no discount.
56 5
total expense =Rs. 266.00000
Enter quandty and rate
40 4
total expense=Rs 160.000000
The If Statement
The if statement is used to control the flow of execution of statement . The if statement is it two way statement, if condition is true then execute and if if condition is not true then statement is not execute.
or
The statement portion may be a group of statement or single statement if the condition is true then the statement portion will be execute otherwise the statement portion skipped and next statement will be executed.
SYNTAX OF IF STATEMENT
if(test condition)
{
statement portion;
}
next statement;
The example of if statement
EXAMPLE /*USE OF IF STATEMENT*/
#include<stdio.h>
main()
{
int number;
printf ("Enter a number less than 100\n");
scanf ("%d",number);
if (number<=100)
printf ("Well done");
}
Here on execution of the above program if we type a number less than or equal 100 then we get a message on the screen through printf() function . If we type some other number greater than 100 the program does not to anything.
EXAMPLE /* Use of if statement */
#include<stdio.h>
main()
{
int quandity,discount=0;
float rate,total;
printf ("Enter quandity and rate");
scanf ("%d%f",&quandity,&rate);
if (quandity>50)
discount=5;
total=(quandity*rate)-quandity*rate*discount/100);
printf("total expenses=Rs.%f",total);
}
In this program enter is greater than 50 then the variable discount which was earlier set to zero now get a new value 5 and using this new value total expenses are calculated and printed. Again if quantity is less than 50 discount will be 0 and hence expression after the minus sign evaluate to zero there by offering no discount.
OUTPUT
Enter quandity and rate56 5
total expense =Rs. 266.00000
Enter quandty and rate
40 4
total expense=Rs 160.000000


0 Comments