Control Statements in C – if, else, switch

Control Statements in C are used to execute/transfer the control from one part of the program to another depending on a condition. These statements are also called as conditional statements.
There are two types of Control Statements in C:

  • if-else statement
  • switch statement
    Control Statements in C

    1. if-else statement : if-else statement is used to execute a statement block or a single statement depending on the value of a condition. It has the following syntax :

    if(condition)
    {
      - - - - - - - - -
     <true block>
      - - - - - - - - -
    }
    else
    {
      - - - - - - - - -
      <false block>
      - - - - - - - - -
    }  // where <condition> is a logical expression which will have the value <strong>true</strong> or <strong>false .</strong>
    

  • Nested if-else Statement :
  • An if statement may have another if statement in the < true block > and < false block >. This compound statement is called nested if statement.

    if(condition 1)
    if(condition 2)
    {
      - - - - - - - - -
     <true block 1>
      - - - - - - - - -
    }
    else
    {
      - - - - - - - - -
      <false block 1>
      - - - - - - - - -
    } 
    else
    if(condition 3)
    {
      - - - - - - - - -
     <true block 2>
      - - - - - - - - -
    }
    else
    {
      - - - - - - - - -
      <false block 2> 
      - - - - - - - - -
    } 
    

    2. switch statement : switch statement is used to execute a block of statements depending on the value of a variable or an expression. It has the following syntax :
    switch (<expression>)
    {
     case <label 1>: {
                         - - - - - - - - - -
                          <statement 1>
                         - - - - - - - - - -
                          break;
                      }
    case <label 2>:  {
                         - - - - - - - - - -
                          <statement 2>
                         - - - - - - - - - -
                          break;
                      }
    case <label 3>:  {
                         - - - - - - - - - -
                          <statement 3>
                         - - - - - - - - - -
                          break;
                      }
    case <label n>:  {
                         - - - - - - - - - -
                          <statement n>
                         - - - - - - - - - -
                          break;
                      }
    default:          {
                         - - - - - - - - - -
                          <default statement>
                         - - - - - - - - - -
                          break;
                      }
    }  // where <expression> refers any int or char expression or variable. <label 1>, <label 2>, <label 3>, <label n> are the values which will match with the value of the expression.
    break is a statement which will transfer the control to the end of switch statement.
    

    Recommended Posts:

    C
        1. Fundamental of C Language
        2. Keywords in C
        3. Data Types in C
        4. Operators in C
        5. Type Conversion in C
        6. Input/Output Functions in C
        7. C Program to Swap Two Numbers With or Without Temporary Variables
        8. Control Statements in C – if, else, switch
        9. Loop Control Statements in C | for, while, do-while loop
        10. Comparison Between for loop while loop and do-while loop
        11. Unconditional Control Transfer Statements in C
        12. C Program to Check Armstrong Number
        13. C Program to Generate Fibonacci Series
        14. C Program to Check Whether a Number is Prime or Not
        15. C Program to Convert Binary Number to Decimal
        16. C Program to Convert Decimal Number to Binary
        17. Arrays in C – One-Dimensional, Two-Dimensional Array
        18. C Program to Find the Largest Number in an Array
        19. C Program to Sort an Array in Ascending Order
        20. C Program to Add Two Matrices
        21. C Program to Multiply Two Matrices
        22. C Program to Find Transpose of a Matrix
        23. String Manipulations In C
        24. C program to count number of vowels in a string
        25. C Program to Check whether the Given String is a Palindrome
        26. C Program to Concatenate Two Strings without using strcat()
        27. C Program to Convert Lowercase to Uppercase
        28. Functions in C
        29. C Program to Find Factorial of a Number using Function
        30. C Program to Reverse a Number using Function
        31. C Program to Reverse a String using Function
        32. Recursion in C
        33. C Program to Find Factorial of a Number Using Recursion
        34. C Program to Print Fibonacci Series using Recursion
        35. Local and Global Variables in C
        36. Storage Classes in C
        37. Pointers in C
        38. Call by Value in C using Pointers
        39. Call by Reference in C using Pointers
        40. Difference between Call by Value and Call by Reference
        41. C Program to Add Two Numbers using Pointers
        42. C Program to Find Largest of Two Numbers using Pointers
        43. Dynamic Memory Allocation in C | malloc(), calloc(), free() and realloc()
        44. C Program to count Vowels in a string using Pointer
        45. C program to Check if a string is palindrome using Pointers
        46. C Program to Copy String Using Pointers
        47. Structure and Union in C
        48. Difference between Structure & Union
        49. File handling in C
        50. C Program to Create a File and write in it
        51. C program to copy contents of one file to another