Conditional Statements.

Types of Conditional Statements.



1. if-else

if statement performs the operation when the condition is 1(true).

else statement performs the operation when the condition is 0(false).

syntax :

 if(condition) {

//performs the operation when the condition is 1(true)

}

else{

//else statement performs the operation when the condition is 0(false)

}

NOTE: else is an optional block.


a. else-if

else-if statement performs the operation when the if condition is 0(false).


Syntax :

if(condition) {

//performs the operation when the condition is 1(true)

}

else if{

//else if statement performs the operation when the if condition is 0(false)

}


b. Nested if

use of if inside if.

Example :

#include<stdio.h>
#include<math.h>

int main()

{
int number;
printf("enter number\n");
scanf("%d", &number);
    if(number>=0){
   
    printf("number is +ive\n");
}
if(number%2==0){

    printf("number is even\n");
}
else{
printf("number is odd");
}
return 0;

Output:
enter number
5
number is +ive
number is odd
--------------------------------
Process exited after 4.562 seconds with return value 0
Press any key to continue . . .


2. switch

A switch allows the program to make a decision from the number of choices.

Syntax :

switch(expression){

case 1: // do operation
          break;

case 2: // do operation
          break;

case 3: // do operation
          break;

default: // do operation
}

Example :

#include<stdio.h>
#include<math.h>

int main()

{
int grade;
printf("enter grade\n");
scanf("%d", &grade);
    switch(grade){
   
    case 1: printf(" grade A++");
    break;
   
    case 2 : printf(" grade A+");
    break;
   
    case 3 : printf(" grade A");
    break;
   
    case 4 : printf(" grade B++");
    break;
   
    case 5 : printf(" grade B+");
    break;
   
    case 6 : printf(" grade B");
    break;
   
    case 7 : printf(" grade C++");
    break;
   
    case 8 : printf(" grade C+");
    break;
   
    case 9 : printf(" grade C");
    break;
   
    case 10 : printf(" grade D");
    break;
   
    default : printf("fail");
   
}
return 0;
}

Output :
enter grade
5
 grade B+
--------------------------------
Process exited after 7.907 seconds with return value 0
Press any key to continue . . .

Properties of Switch

a. Case can be in any order.

b. Nested switch (Switch inside switch) is allowed.

3. Conditional Operators

a. Ternary operator

The ternary operator is used to perform the operation when the first condition is true and run the second condition when the first condition is false.

Syntax :

condition ? runs code if TRUE : runs if FALSE;

Example :

#include<stdio.h>

int main()

{
int age;
printf("enter age\n");
scanf("%d", &age);
    age>=18 ? printf("eligible for vote\n") : printf("not eligible for vote");
return 0;
}

Comments

Popular posts from this blog

Write a program to check if a number is prime or not.

Write a program to print the grades to a student.

write a program (keep taking numbers as input from user until user enters an Odd number)