Decision Making in C
Decision Making in C
0
Decision making in C
C if Statement:
if (test expression) {
statement/s to be executed if test expression is true;
}
The if statement checks whether the text expression inside parenthesis
( ) is true or not. If the test expression is true, statement/s inside the
body of if statement is executed but if test is false, statement/s inside
body of if is ignored.
Example : C if statement
Write a C program to print the number entered by user only if the
number entered is negative.
#include <stdio.h>
int main(){
int num;
printf("Enter a number to check.\n");
scanf("%d",&num);
if(num<0) {
printf("Number = %d\n",num);
}
/*If test condition is true, statement above will be executed, otherwise
it will not be executed */
printf("The if statement in C programming is easy.");
return 0;
}
Output :
Enter a number to check.
-2
Number = -2
if((num%2)==0)
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
}
Output 1:
Enter a number you want to check.
25
25 is odd.
Output 2:
Enter a number you want to check.
2
2 is even.
Output 1:
Enter two integers to check.
5
3
Result: 5 > 3
Output 2:
Enter two integers to check.
-4
-4
Result: -4 = -4