Decision control statements

if  statement

  • if is a conditional statement, i.e., if the condition is satisfied, then statements present inside the block is executed.

Syntax:

                if (expression)

                statement;

          A statement can be a single statement, a block of statements, or nothing (in the case of empty statements).

  • If an expression evaluates to true, then statement or block that forms the target of if is executed

Example:

#include<stdio.h>
int main()
{
   int Marks=10;
   if(Marks==10)
   {
        printf("Excellent");
   }
}
Output:
Excellent
if-else  Statement
  • if the condition is satisfied, then statements present inside the block are executed. Otherwise, statements present inside the else block are executed.
  • The general form is:

                                              if (expression)

                                              statement;

                                              else

                                              statement;

          A statement can be a single statement, a block of statements, or nothing (in the case of empty statements).

  • If an expression evaluates to false, then statements of if block will not execute and else statement will execute.

Example:

#include<stdio.h>
int main()
{
   int Marks=2;
   if(Marks==10)
   {
        printf("Excellent");
   }
    else
   {
        printf("Try to work hard");
   }

}
Output:
Try to work hard
else-if statement
  • An else-if statement is similar to an if statement.
  • It is used when we have multiple if conditions.
  • The general form is:

                                               if(expression)

                                               {

                                                            statement;

                                                }

                                                else if(expression)

                                                {

                                                              statement;

                                                 }

                                                 else

                                                 {

                                                              statement;

                                                  }

  • If all the if block statements are false, then else if condition is tested and if it is true then statements in the else if blocks are executed.

Example:

#include<stdio.h>
int main()
{
   int Marks=9;
   if(Marks==10)
   {
        printf("Excellent");
   }
    else if(Marks==9)
   {
        printf("Good");
   }
    else
   {
        printf("Try to improve your performance");
   }

}
Output:
Good

Nested if

  • The “if” statement, which is present in another “if” statement, is known as Nested if.
  • It is used when we have multiple if conditions.
  • Nested ifs are very common in programming.
  • In a nested if, an else statement always refers to the most next if statement under the same block as the other and is not already linked.
  • Given below, If the if(condition1) is true, then if (condition2) is tested, i.e., one if statement targets another if statement.

Syntax:

                               if (condition1)

                                      {

                                              if (condition2)

                                                         {

                                                                statement;

                                                          }

                                       }

Example:

#include<stdio.h>
int main()
{
   char Grade='A';
   int Marks=10;
   if(Grade=='A')
   {
        if(Marks==10)
        {
            printf("Excellent");
        }
        if(Marks==9)
        {
            printf("Good");
        }
   }
   else
   {
        printf("Try to improve your performance");
   }
   return 0;
}
Output:
Excellent

for loop

  • Loops (also called iteration statements) allow a collection of instructions to execute repeatedly until a specific condition is reached.

Syntax:

                          for (initialization; condition; increment) statement; 

         There are many variations of for loop, but its most common form acts like this:

  • The initialization is used to set the loop control variable, assigning an initial value to it.
  • The condition is a relational expression that defines the exit of the loop.
  • The increment defines the change in the loop control variable at each time when the loop is repeated. 
  • These three sections are separated with semicolons.
  • The for loop executes continuously until the condition remains true. Once the condition becomes false, control is transferred to the statement following the for.

Example:

#include<stdio.h>
int main()
{
   for(int i=0;i<10;i++)
   {
       printf("i=%d\n",i);
   }
   return 0;
}
Output:
i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9

while loop

  • A while loop executes a target statement repeatedly until a given condition is true.

Syntax:

                while(condition)

                statement;

  • Where the statement is either an empty statement, a single statement, or multiple statements. 
  • The condition is a relational expression that defines the exit of the loop.
  • The while loop executes continuously until the condition remains true. Once the condition becomes false, control is transferred to the statement following it.

Example:

#include<stdio.h>
int main()
{
    int i = 9;
    while(i<20)
    {
        printf("value of i: %d\n", i);
        i++;
    }
    return 0;
}
Output:
value of i: 9
value of i: 10
value of i: 11
value of i: 12
value of i: 13
value of i: 14
value of i: 15
value of i: 16
value of i: 17
value of i: 18
value of i: 19

Do-while loop

  • do-while loop won’t test the loop condition at the top of the loop; it checks its condition at the bottom of the loop to execute the statements inside the block at least once.
  • This indicates that the do-while loop always executes at least once.

Syntax:

               do {

                      statement;

                     }

              while(condition);

  • Although the curly braces are not necessary when only one statement is present, they are usually used to avoid confusion (to you, not the compiler) with the while.
  • do-while loop iterates until the condition becomes false.

Example:

#include<stdio.h>
int main()
{ 
    int a = 0;
    do
    {
        printf("value of a: %d\n",a);
        a++;
    }while(a<=5);
     return 0;
}
Output:
value of a: 0
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5

break statement

Syntax:

                break;

  • The break statement has two uses.
  1. We can use it to terminate a case in the switch statement.

Example 1:

#include<stdio.h>
int main()
{
        int num;
        printf("Enter the number:");
        scanf("%d",&num);
        switch(num)
        {
        case 1:
            printf("You have entered 1\n");
            break;
        case 2:
            printf("You have entered 2\n");
            break;
        default:
            printf("Input value is other than 1 and 2");
        }
        return 0;
}
Output:
Enter the number:1
You have entered 1
  1. We can also use it to force immediate termination of a loop bypassing the standard loop conditional test. The loop is immediately terminated when the break statement is present inside the loop, and program control is transferred to the following statement.

Example 2:

#include<stdio.h>
int main()
{
        int a;
        for(a=50; a>=12; a--)
        {
            printf("a= %d\n",a);
            if(a==41)
            {
                break;
            }
        }
        printf("Out of loop");
        return 0;
}
Output:
a= 50
a= 49
a= 48
a= 47
a= 46
a= 45
a= 44
a= 43
a= 42
a= 41
Out of loop
  • If we use nested loops, the break statement stops the innermost loop execution and starts executing the following available lines of code after the block.

switch statement

  • switch is a selection statement that tests the value of an expression successively from a character constant or a list of integers.
  • When a match is detected, the statements associated with that constant are executed.

Syntax:

                switch (expression)

               {

                case constant1:

                statement

                break;

                case constant2:

                statement

                break;

                case constant3:

                statement

                break;

                .

                .

                .

                default

                statement

                }

  • The expression can be a character or an integer value, but floating-point expressions are not allowed.
  • The value of an expression is tested with the values of the constants, one after the other, specified within the case statements.
  • When a match is detected, the statement sequence associated with that case is executed until a break statement or end of the switch statement is reached.
  • The default statement is executed if no matches are detected.

Example 1:

#include<stdio.h>
int main()
{
        char grade = 'B';
        switch(grade)
        {
        case 'A':
            printf("Excellent!\n");
            break;
        case 'B':
        case 'C':
            printf("Well done\n");
            break;
        case 'D':
            printf("Just passed\n");
            break;
        case 'F':
            printf("Failed\n");
            break;
        default:
            printf("Invalid grade");
        }
        printf("Your grade is %c\n", grade);
        return 0;
}
Output:
Well done
Your grade is B

Example 2: Switch statement without using break:

#include<stdio.h>
int main()
{
        int num=0;
        printf("Enter the number:");
        scanf("%d",&num);
        switch(num)
        {
        case 10:
            printf("Number is equal to 10\n");
        case 20:
            printf("Number is equal to 20\n");
        case 100:
            printf("Number is equal to 100\n");
        default:
            printf("Number is not equal to 10, 20 or 100");
        }
        return 0;
}
Output:
Enter the number:20
Number is equal to 20
Number is equal to 100
Number is not equal to 10, 20 or 100

continue statement

  • The continue statement works somewhat similar to the break statement but continues statement forces the next iteration of the loop instead of forcing termination.
  • It is mainly used when we want to skip a particular part of code for some condition. 

Syntax:

                //statements of the loop  

                    continue;  

                //some lines of the code which we want to skip  

Example:

#include<stdio.h>
int main()
{
    for(int a=1;a<=10;a++)
    {
     if(a==5)
        continue;
     else
        printf("a=%d\n",a);
    }
    return 0;
}
Output:
a=1
a=2
a=3
a=4
a=6
a=7
a=8
a=9
a=10
goto statement
  • The goto statement in C is known as the unconditional jump statement used to transfer a program’s control.
  • It allows the flow of execution of the program to jump to a particular location within the function.
  • It can also be used to break the multiple loops, which cannot be done using a single break statement. 
  • However, the goto statement is avoided these days as it makes the program less readable and complicated.

Syntax:

                      goto label;

                      .

                      .

                      .

                       label: statement;

Example:

#include<stdio.h>
int main()
{
    int marks;
    abc: //name of the label
        printf("you are passed\n");
    xyz: //name of the label
        printf("you are failed\n");
    printf("Enter your marks:");
    scanf("%d",&marks);
    if(marks>=40)
        goto abc; //goto label abc
    else
        goto xyz; //goto label xyz
    getch();
}
Output:
you are passed
you are failed
Enter your marks:30
you are failed
Enter your marks:

Who is the course for?

Prerequisites before starting this course:

Questions 🙵 Answers.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

More Courses

You might also be interested in these courses