➔ If-Else Statement
Syntax ➔
if (condition to be checked) {
Statement-if-the-condition-true;
}
else {
Statements-if-condition-false;
}
// else block is not necessary but optional.
example : Program to check a Number is Odd or Even :
int num ;
printf("Enter Number : ");
scanf("%d", &num);
if (num%2==0) {
printf("%d is a Even Number", num); }
else {
printf("%d is a Odd Number", num); }
else if clause ➔ Instead of using multiple if statement we can also
use else-if along with if
Syntax ➔ else if (condition to be checked) {
Statement-if-the-condition-true; }
example ➔ Program to check greater among two numbers
int num1, num2;
printf("Enter First number : ");
scanf("%d", &num1);
printf("Enter Second Number : ");
scanf("%d", &num2);
if (num1 > num2) {
printf("%d is greater than %d", num1, num2); }
else if (num1 == num2) {
printf("%d is equal to %d", num1, num2); }
else{
printf("%d is lesser than %d", num1, num2); }
Short If-else ➔
Syntax ➔
(condition)? <expression-if-true> : <expression-if-false> ;
where '?' and ':' are Ternary Operators
example ➔ Program to check a Number is Odd or Even Using Short If-Else
int num;
printf("Enter any Number : ");
scanf("%d", &num);
(num%2==0)? printf("%d is a Even Number", num) :
printf("%d is a Odd Number ", num);
Switch Case Control Instruction
Syntax ➔
Switch(expression) {
case (constant-expression): //we can use too many cases as we want
code;
break;
default: // if all cases are wrong then it will run default
code;
break; }
example ➔ Program to Enter rating of any App between 1 to 3 :
int rating;
printf("Enter Rating (1-3) : ");
scanf("%d", &rating);
switch (rating) {
case 1 :
printf("Rating is 1 ");
break;
case 2 :
printf("Rating is 2");
break;
case 3 :
printf("Rating is 3 ");
break;
default :
printf("Invalid Rating !");
break; }