on
Italy
- Get link
- X
- Other Apps
if, if...else and nested if...else
statement are used to make one-time decisions in C Programming, that
is, to execute some code/s and ignore some code/s depending upon the
test expression.if (test expression) {
statement/s to be executed if test expression is true;
}
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.
#include
int main(){
int num;
printf("Enter a number to check.\n");
scanf("%d",&num);
if(num<0 0="" above="" be="" c="" checking="" code="" condition="" easy.="" executed="" f="" he="" if="" in="" is="" it="" less="" not.="" not="" num="" number="" or="" otherwise="" printf="" programming="" return="" statement="" test="" than="" true="" umber="%d\n" whether="" will="">0>
Enter a number to check. -2 Number = -2 The if statement in C programming is easy.When user enters -2 then, the test expression
(num<0 code=""> becomes true. Hence, Number = -2 is displayed in the screen.0>Enter a number to check. 5 The if statement in C programming is easy.When the user enters 5 then, the test expression
(num<0 code=""> becomes false. So, the statement/s inside body of if is skipped and only the statement below it is executed.0>if...else statement is used if the programmer wants
to execute some statement/s when the test expression is true and execute
some other statement/s if the test expression is false.if (test expression) {
statements to be executed if test expression is true;
}
else {
statements to be executed if test expression is false;
}
#include
int main(){
int num;
printf("Enter a number you want to check.\n");
scanf("%d",&num);
if((num%2)==0) //checking whether remainder is 0 or not.
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
}
Output 1Enter a number you want to check. 25 25 is odd.
Enter a number you want to check. 2 2 is even.
if...else statement is used when program requires more than one test expression.if (test expression1){
statement/s to be executed if test expression1 is true;
}
else if(test expression2) {
statement/s to be executed if test expression1 is false and 2 is true;
}
else if (test expression 3) {
statement/s to be executed if text expression1 and 2 are false and 3 is true;
}
.
.
.
else {
statements to be executed if all test expressions are false;
}
How nested if...else works?if...else statement has more than
one test expression. If the first test expression is true, it executes
the code inside the braces{ } just below it. But if the first test
expression is false, it checks the second test expression. If the second
test expression is true, it executes the statement/s inside the braces{
} just below it. This process continues. If all the test expression are
false, code/s inside else is executed and the control of program jumps below the nested if...else#include
int main(){
int numb1, numb2;
printf("Enter two integers to check\n");
scanf("%d %d",&numb1,&numb2);
if(numb1==numb2) //checking whether two integers are equal.
printf("Result: %d = %d",numb1,numb2);
else
if(numb1>numb2) //checking whether numb1 is greater than numb2.
printf("Result: %d > %d",numb1,numb2);
else
printf("Result: %d > %d",numb2,numb1);
return 0;
}
Enter two integers to check. 5 3 Result: 5 > 3
Enter two integers to check. -4 -4 Result: -4 = -4
Comments
Post a Comment