Control Statements In C-Language

 

Control Statements In C-Language

Control statements are used to random execution of the program .These are classified into following types
  1. Conditional Statements
  2. Looping Statements
  3. Unconditional Statements

Conditional Statements

    Conditional Statements are used to check the condition whether it is true or false.These are classified into following types
    1. if Condition
    2. if else Condition
    3. else if Condition
    4. nested if Condition
    5. switch Expression
  1. if Condition:
  2. if is a keyword in c language which is used to execute one block of statements by neglecting some other statements
    Syntax:
    if(Condition)
    {
    ------
    ------
    }
    
    In the above syntax
    if Condition is true the statements of if block is execute
    if Condition is false the if block is terminated

    2.if else Condition:

    if else is a keyword in c language which is used to execute one block of statements between two blocks.
    if(Condition)
    {
    ------
    ------
    }
    else
    {
    ------
    ------
    }
    
    In the above syntax
    if Condition is true the statements of if block is execute
    if Condition is false the statements of else block is execute
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int x;
    printf("Enter x value");
    scanf("%d",&x);
    if(x%2==0)
    {
    printf("Even Number");
    }
    else
    {
    printf("Odd Number");
    }
    getch();
    }

    3.else if Condition:

    else if is a keyword in c language which is used to execute one block of statements among multiple blocks.
    if(Condition)
    {
    ------
    ------
    }
    else if(Condition)
    {
    ------
    ------
    }
    else if(Condition)
    {
    ------
    ------
    }
    else if(Condition)
    {
    ------
    ------
    }
    .
    .
    .
    .
    else
    {
    ------
    ------
    }
    
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int age;
    printf("Enter your age");
    scanf("%d",&age);
    if(age>0 && age<=12)
    {
    printf("You are Child");
    }
    else if(age>12 && age<=19)
    {
    printf("You are Teen");
    }
    else if(age>19 && age<=40)
    {
    printf("You are Younger");
    }
    else if(age>40 && age<=80)
    {
    printf("You are Old");
    }
    else if(age>80 && age<=`00)
    {
    printf("You are Too Old");
    }
    else
    {
    printf("Invalid age");
    }
    getch();
    }

    4.Nested if Condition:
    One if condition with in another if condition is known as nested if.
    if(Condition)
    {
        if(Condition)
        {
        ......
        ......
        }
        else
        {
        ......
        ......
        }
    }
    else
    {
    ------
    ------
    }
    
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int x;
    printf("Enter x value");
    scanf("%d",&x);
    if(x>0)
    {
        if(x%2==0)
        {
        printf("Even Number");
        }
        else
        {
        printf("Odd Number");
        }
    }
    else
    {
    printf("Please Enter Positive values");
    }
    getch();
    }

    5.Switch Expression:

    switch is a keyword in c language, which is used to execute one block of statements among multiple blocks.Here switch is a choice Expression.
    Syntax :
    switch(choice)
    {
    case 1:
    	Statements;
            break;
    case 2:
    	Statements;
            break;
    case 3:
    	Statements;
            break;
          .
          .
          .
          .
          .
    default:
    	Statements;
    }
    
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int x,y;
    printf("Enter x value");
    scanf("%d",&x);
    y=x%2;
    switch(y)
    {
    case 0:
    	printf("Even number");
        break;
    case 1:
    	printf("Odd Number");
        break;
    defalut:
    	printf("Invalid Choice");
    }
    getch();
    }


No comments:

Post a Comment