Single if statement in C


An if statement is a single selection or decision statement. It is used when we have only one alternative.


Syntax:-

if(expression)
{
statement 1
statement 2
statement..n
}

the statement is executed when the condition is true.

Note: The keyword if must be followed by an expression and the expression must be enclosed within parentheses.

Q.) W.A.P to print the biggest number using simple if statement.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int a, b, big;
 printf(“enter two numbers:<\n”);
 scanf(“%d%d”,&a,&b); 
 big=a;
 if(b>big)
 {
 big=b;
 }
 printf(“largest number=%d”,big);
 getch();
 }
Output
enter two numbers:
3 4
largest number= 4

Program Explanation

Step 1:-
We declared three integer variables called a , b and big.
Step 2:-
A Message will give for entering their own values for a and b:- Enter Two No
Step 3:-
User Enter two integer No Let`s We Enter :- 6 3
Step 4:-
hear we will have to print largest elements using only if condition not else. so we take an extra variable i.e big and we assume a is the biggest element and assign a value to big. next If condition checks whether big is greater than b or not. If this condition is True, then b variable value assigns to a big variable. if the condition is not true then it will print big value i.e a is greater than b.
hear we used the Conditional Operator(i.e > greater than) to check whether big is greater than b or not. If this condition is True, then it will be executed if part, which is variable an (b value assign to big). If the first condition fails, then it will be executed else part, which will give output as a is greater than b
Step 5:-
in this program, the condition is true because a is greater than b os if a part will be executed because we have enter 6 and 3 i.e 6 is greater than 3
if we enter 3 and 6 then 3 is less than 6 so else part will be executed
Step 6:-
So Output will be :- a is grater than b