Selection
The statement following the control expression is executed
if the value of the control expression is true (nonzero). An
#include<stdio.h>
int main(){
int a=2;
if (a%2==0){
printf ("even");
}
else {
printf ("Odd");
}
return 0;
}
The output is
even
why because the computer will do the aritmatics operation of 2%2 =0 and the statement for 2%2 =0 is even so the output is even.
if statement can be written with an optional
else clause that is executed if the control expression
is false (0). Example :#include<stdio.h>
int main(){
int a=2;
if (a%2==0){
printf ("even");
}
else {
printf ("Odd");
}
return 0;
}
The output is
even
why because the computer will do the aritmatics operation of 2%2 =0 and the statement for 2%2 =0 is even so the output is even.
Comments
Post a Comment