If Else control statements and switch


If else control statements

// if else control statements


#include <stdio.h>


int main()

{

    int age;

    printf("Enter your age\n");

    //  we have taken the input age

    scanf("%d", &age);

    printf("You have entered %d as your age\n", age);

    if (age >= 18)

    {

        // if statement- In if statement condition is checked,

        // and if it is true then 'if'statement is executed

        // otherwise it is skipped and rest of the code is exected.

        printf("You can vote!");

    }


    else if (age >= 10)

    {

        // if-else statement: In if-else statement first of all 'If'

        // // condition is checked and if the condition is true then 'If'

        // statement gets executed otherwise 'else'statements get executed

        printf("You are between 10 to 18 and you can vote for kids");

    }


    else if (age >= 3)

    {

        printf("You are between 3 to 10 and you can vote for babies");

    }


    else

    {

        // when the first stage get wrong then this will be executed

        printf("You cannot vote!");

    }


    return 0;

}


#include <stdio.h>

int main()

{

    int age;

    printf("enter your age\n");

    scanf("%d", &age);

    printf("you entered your %d as your age\n", age);

    if (age >= 21)

    {

        printf("you can marry now at hkr\n");

    }

    else

    {

        printf("you can marry with kids\n");

    }


    return 0;

}

// problem for if else operators


// maths and science - 45

// science - 15

// maths - 15


// print the type of gift you are giving to them


#include <stdio.h>

int main()

{

    // i have been solved this problem from int data type

    // when the user gives input as a number

    int a;

    printf("enter 1 if you have passed the maths subject\n");

    printf("enter 2 if you have passed science\n");

    printf("enter 3 if you passed both\n");

    printf("enter any number if you have dont passed any of these subjects");

    scanf("%d", &a);

    if (a == 1)

    {


        printf("congragulations you have awarded rs 15 for passing maths\n");

    }


    else if (a == 2)

    {

        printf("congragulations you have been passed science\n");

        printf("you awarded rs 15 again\n");

    }

    else if (a == 3)

    {

        printf("congragulations you have rewarded rs 45 for completing both subjects");

    }

    else

    {

        printf("you are not eligible to get reward \n");

    }

    return 0;

}

switch statements

//switch case statements control statements

                        //Rules for switch statement


                        // 1 switch expression  must be int or char

                         //2 case value must be an integer or a character

                         //case must come inside switch

                        //  break is not a must                         

#include <stdio.h>


int main()

{

    int age, marks;

    printf("Enter your age\n");

    scanf("%d", &age);


    printf("Enter your marks\n");

    scanf("%d", &marks);


    switch (age)

    {

        case 3:

        //this cheks the age is 3 then execute next code

            printf("The age is 3\n");

            switch (marks)

            {

                //we can add switch inside switch 

                case 45:

                    printf("Your marks are 45");

                    break;

            

                default:

                    printf("your marks are not 45");

            }

            break;


        case 13:

            printf("The age is 13\n");

            break;

            //when we dont add break example

            //it checks the age is 13 then it will execute the printf and then 

            //it will also execute next case


        case 23:

            printf("The age is 23\n");

            break;


        default:

            printf("Age is not 3, 13 or 23\n");

        

    }



    return 0;

}


Comments

Popular posts from this blog

Apology

Payment form

First website