Conditional Statement

Conditional Statements are one of my favorite chapter in C.

Conditional statements are programming constructs that allow a program to execute a block of code based on whether a certain condition is true or false.

There are 2 types of Conditional Statements

  1. if-else

  2. if-else if

  3. Switch

    The syntax is

    if(Condition){

    //do something if True

    }

    else if(another condition){

    //do something else

    }

    Q1 Let's write a code to tell if a person is above 18 or above 21 in c

int main() {
    int age;

    printf("Enter age=\n");
    scanf("%d",&age);

    if (age>18){
        printf("You are above 18 \n");
    }
    else if (age>=18 && age<21){
        printf("You are less than 21 but greater than 18\n");
    }
    else 
    {printf("You are below 18\n");
    }
}

Output

Switch Statment

In C, the switch case statement is used for executing one condition from multiple conditions. It is similar to an if-else-if ladder. The switch statement consists of conditional-based cases and a default case.

Syntax

switch(number) {

case C1: //do something

break;

case C2 : //do something

break;

default : //do something

}

switch Properties

a. Cases can be in any order

b. Nested switch (switch inside switch) are allowed

Q2 A program to input a character and tell the day of the week in c.

int main() {
    char day ;
    printf("Enter the first letter of a character from the day eg s for sat and S for sunday\n");
    scanf("%c",&day);

     switch(day){
        case 'm' : printf("Monday");
        break;
        case 't': printf("Tuesday");
        break;
        case 'w':printf("Wednesday\n");
        break;
        case 'T': printf("Thursday\n");
        break;
        case 'f': printf("Friday\n");
        break;
        case 's': printf("Saturday\n");
        break;
        case 'S': printf("Sunday\n");
        break;
        default :printf("Wrong text");
     }
}

Output

Q2 A program to take an input and cheak if its greater than 0 and if its an odd in C.

int main() {
   int n;
   printf("Enter a number to cheak if its and odd number or even\n");
   scanf("%d",&n);

   if (n>=0)
   {
    printf("Number is positive\n");
    if(n % 2==0)
    {printf("Number is number is even");}
    else{printf("number is odd");}
   }
   else 
   {
    printf("Number is negetive\n");
    if(n % -2==0)
    {printf("Number is number is even");}
    else{printf("number is odd");}
   }

}

Output

Q3 Write a program if the student passed or failed in c.

int main() {
   int t;
   printf("Enter the percent you have got\n");
   scanf("%d",& t);

   if (t>30)
   {printf("congratulations you have passed the test\n");}
   else if(t<=30)
   {printf("I am sorry you have failed the test\n");}

   // Or if you want to use Ternery to cheak 
   // the conditions
   //t<=30 ? printf("Fail") : printf("Pass");
}

Output

Q4 Write a program to give grades to a student

marks <30 is c

30<=marks<70 is B

70<=marks<90 is A

90<= marks<=100 is A+ in C.

#include <stdio.h>

int main() {
  int t;
   printf("Enter the percent you have got\n");
   scanf("%d",& t);


   if (t>30)
   {printf("Congratulations you have passed\n");

   if (t<30)
   {printf("Grades=C");}

   else if (30<=t && t<70)
   {printf("Grade=B");}

   else if(70<=t && t<90)
   {printf("Grades=A");}

   else if(90<=t && t<=100)
   {printf("Grades=A+");}
   }
   else if(t<=30)
   {printf("Im sorry you have failed\n");}
}

Output

Q5 Write a program to cheak if the alphabet is upper case or not. in C.

int main() {
    char ch;
 printf("Enter a character\n");
 scanf("%c",&ch);

 if(ch>='a' && ch<='z')
 {printf("This is a Small letter");}

 else if (ch>='A'&& ch<='Z')
 {printf("The letter you have entered is upper case");}

else 
{printf("Error");}

}

Output

Hope you enjoyed learning conditional statements in C. This my favourite concept in C and is really useful. Hope this Blog was helpful.

Thankyou...