Conditional Statement

  

Conditional Statement,if, else,loop,while,do,for

Conditional Statement

C Language must be able to perform different sets of actions depending upon the circumstances. Conditional Statement helps you to make some decisions based upon the different conditions. These conditions are specified by some set of boolean values expressions which tends to the boolean true or false.

Types of Conditional Statement:

1. if statement.
2. if-else statement.
3. nested if-else statement.
4. switch statements.

If Statement:

C uses the if keyword to implement decision control instruction.

General Syntax of if Statement:

if(This condition is true)
    { Execute this statement; }


Flow diagram for if statement:


if, else,conditional,decision,C
 

Example:

#include <stdio.h>

int main()

{

    int age;        /* Defining age in integer data type */

    age=15;         /* declaring age with value 12 */

    if(age<18)      /* if age is smaller than 18 print defined statement */

    printf("User is Prohibited to enter");

    return 0;

}


 

If else statement:

If statement by itself will execute a single statement, or a group of statements When the expression following the if statement leads to true.So what can we do if the statement is false?This is what the purpose of else came into existence.

General Syntax:

if(This condition is true)
    { Execute this statement; }
else
{
    execute this statement if the upper condition is false
}


Flow diagram for if-else statement: 


if, else,conditional,decision,C

Example:

#include <stdio.h>

int main()
{
    int age;        /* Defining age in integer data type */
    age = 19;         /* declaring age with value 12 */
    if(age<18)      /* if age is smaller than 18 print defined statement */
    {
        printf("User is Prohibited to enter");
    }
    else
    {
        printf("User Can Enter");
    }
    
    return 0;
}



Nested if statement:

If we write entire if-else statement within either the body of the if statement or the body of an else statement. This is called the nesting of if's statement

Example:

#include <stdio.h>
int main()
{
    int age;        /* Defining age in integer data type */
    age = 9;         /* declaring age with value 12 */
    if(age<18)      /* if age is smaller than 18 print defined statement */
    {
        if(age<10)
        {
            printf("User is strictly Prohibited");      /* Nested if statement under if */
        }
        else{
            printf("User is Prohibited to enter");    
        }
    }
    else
    {
        printf("User Can Enter");
    }
    return 0;
  }




Switch Statements:

The control statement that allows us to make a decision from the number of choices is called a switch statement, or more correctly a switch case default.
Since these three keyword go together to make up the control switch statement.

General Syntax:

switch (integer expression)
{
    case constant 1:
        execute this;
    case constant 2:
        execute this;
    case constant 3:
        execute this;
    case constant 4:
        execute this;
    default:
        execute this;
}


Flowchart for switch statement: 


switch,if,else,nested if,case,default

Example:

#include <stdio.h>
int main()
{
    int i;
    i=7;
    switch (i)
{
    case 1:
        printf("I am in case 1\n");
        break;
    case 2:
        printf("I am in case 2\n");
        break;
    case 3:
        printf("I am in case 3\n");
        break;
    case 4:
        printf("I am in case 4\n");
        break;
    default:
        printf("I am in default case");
        break;
}
}


 

Post a Comment

0 Comments