Description

Control statement in the C programming language, it does its work as it is named Control statement means that it controls the statement at the time of execution, which statement is executed at the time of control execution, after which statement is executed Which statement is to be executed and after that.

Topics of Control Statement

  • C if else
  • C switch
  • C loops
  • C do while loop
  • C while loop
  • C for loop
  • C break
  • C continue
  • C goto

if statement in C

The if else statement in c is used to perform the conditional statement in programs or decision making in program.  This statement is used for when we have given condition that this is condition then executed should be this otherwise execute should be this.

Types of if statement

  1. if-statement
  2. if-else statement
  3. if else-if statement

If Statement

 The if statement is used to perform to check the condition in program and perform the some operation which depends upon condition correct or not. Syntax of if statement in c

if(/* condition */){
    // Code Here
}

For Example of if statement

#include<stdio.h>

int main(){
    int age;
    printf("Enter Age : ");
    scanf("%d", &age);
    if(age>18){
        printf("You can vote");
    }
    if(age==18){
        printf("Your age is 18 , but you can vote");
    }
    if(age<18){
        printf("You can not vote");
    }

Output :

Enter age : 29
You can vote

if-else statement

The if else statement is used for when two condition(operation) in perform in program. The if else statement is perform two different operation in c programming language. if condition is true then executed the correct condition statement otherwise executed else statement. Syntax of if-else statement...

if(/* condition */){
    // Code executed the if condition is true
}else{
    // Code executed the if condition is false
}

For Example :

#include<stdio.h>

int main()
{
    int num;
    printf("Enter the number : ");
    scanf("%d", &num);
    if(num>25){
        printf("Given Number is greater than 25");
    }else{
        printf("Given Number is smaller than 25");
    }
return 0;
}

Output :

Enter the number : 30
Given number is greater than 25

if else-if statement

This statement is used for when we have multiple cases to be perfromed the statement for different condition. In this statement has been cases multiple. if condition is true then the executed the if statement block and other cases is not executed. suppose the if condition is not correct then go to check the else-if statement there are multiple else-if statement. first else-if statement is false(not true) then go to other else-if statement and the condition this is work when all else if statement is checked then to all else-if statement is checked , suppose one else-if statement is true then execute the block of statement the particular else-if statement otherwise default statement is executed , else statement is execute, else statement is exist in program. Syntax of if else-if statement...

if(/* condition */){
    // Code executed if condition is true
}
else if(/* condition 1*/){
    // Code executed condition 1 is true
}
else if(/* condition 2 */){
    // Code executed condition 2 is true
}
.... ...
else{
    // Code excuted when all codition is false this is default statement
}

For Example :

#include<stdio.h>

int main()
{
    int age;
    printf("Enter the age : ");
    scanf("%d", &age);

    if(age==10)
    {
        printf("Age is 10");
    }
    else if(age>10 && age<50)
    {
        printf("Age is between 10 to 50");
    }
    else if(age==50)
    {
        printf("Age is 50");
    }
    else{
        printf("Age is below 10 or above 50");
    }
return 0;
}

Output :

Enter the age : 30
Age is between 10 to 50

Switch statement

1. switch case statement is a multiple branch selection statement, which successively tests the value of an expression against a list of integer or constant.

2. It allows us to choose a blok of statements among several alternatives.

3. The switch statement is often used to process keyword commands such as menu selection.

4. The switch statement is especially usedful when the selection is based on the value of single variable or of a simple expression. the value of this expression may be of type in or char, but not of type float or double.

Syntax of switch statement

switch(/*expression*/)
{
    case constant 1:
        // Code here case 1
        break;
    case constant 2:
        // Code here case 2
        break;
    case constant 3:
        // Code here case 3
        break;
     ... ... ..
    default :
        // Code here defualt statement
}

For example :

#include<stdio.h>

int main(){
    
    int num;
    printf("Enter the Number : ");
    scanf("%d", &num);
    
    switch(num)
    {
        case 1:
            printf("One");
            break;
        case 2:
            printf("Two");
            break;
        case 3:
            printf("Three");
            break;
        case 4:
            printf("Four");
            break;
        case 5:
            printf("Five");
            break;
        default:
            printf("Still learning!!");
    }
    return 0;
}

Output :

Enter the Number : 3
Three

 

Comments

Login is mandatory to comment Please login

Recommended Posts

Loops in c | for loop | while loop | do while loop | c programming language

Welcome Guys.. What is loop and how many types of loops in c programming language. How loops work in c programming language.

Array in c programming language

An Array is the list of finite number of elements of same data types. For Example: interger, string, float etc. An Array can be defined as the collection of the

Break and Continue Statement in c

Break statement is usually used to terminate a case in the switch statement. In c programming language the continue statement works...

Function in C | What is Functions?

Function like main(), printf() and scanf(), we have already come through. All c Programs made up of one or more function.