DECISION MAKING STATEMENTS IN C

 Decision Making Statements : if, if else,nested if, switch statement

if STATEMENT

if is most basic statement of Decision making statement. It tells to program to execute a certain part of code only if particular condition or test is true.

Syntax

if(<condition>)

    [ Statement or block of statements];

Example

#include<stdio.h>

#include<conio.h>

void main()

{

int a=9;

if(a<10)

{

printf(“a is less than 10”);

}

getch();

}

Output

a is less than 10

Constructing the body of "if" statement is always optional, Create the body when we are having multiple statements. For a single statement, it is not required to specify the body. If the body is not specified, then automatically condition part will be terminated with next semicolon ( ; ).

if -else STATEMENT

It is a keyword, by using this keyword we can create a alternative block for if part. Using else is always optional i.e, it is recommended to use when we are having alternate block of condition. In any program among if and else only one block will be executed. When if condition is false then else part will be executed, if part is executed then automatically else part will be ignored.

Syntax

if(condition)

   [ Statement1 or Block of statements1];

else

   [Statement2 or Block of statements2];

Example

#include<stdio.h>

#include<conio.h>

void main()

{

int age=40;

if(a<18)

{

printf(“You are not eligible for voting”);

}

else

{

printf(“You are eligible for voting”);

}

getch();

}

In above example the If and else part both contains only one statement and these statements are enclosed in { and } but if there is only one statement which is depending on if or else then it is not necessary to enclose the statements within block so you can also write the above statements without using { and }.

Nested if Statement

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. 

The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.

Syntax 

if (testExpression1)

{

// statements to be executed if testExpression1 is true

}

else if(testExpression2)

{

// statements to be executed if testExpression1 is false and testExpression2 is true

}

else if (testExpression 3)

{

// statements to be executed if testExpression1 and testExpression2 is false and testExpression3 is true

}

.

.

.

else

{

// statements to be executed if all test expressions are false

}

Example

#include <stdio.h>

int main()

{

int number;

printf(“Enter an integer :”);

scanf(“%d”,&number);

if ( number > 0)

{

printf(“this is positive integer”);

}

else if (number < 0)

{

printf("This is negative integer";

}

else

{

printf("Entered value is 0.");

}

getch();

}

In above example one if block contains another if so these if statements are called as nested if.

Switch STATEMENT

The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles values of a single variable called switch variable.

Syntax

switch(<expression>)

{

case <value1>:  

           //code to be executed;

           break; //optional

case <value2>:

            //code to be executed;

             break; //optional

...... 

    default:

                 code to be executed if all cases are not matched;

}

Example

switch(n)

{

case 1: printf(“First choice selected”);

            break;

case 2: printf(“Second choice selected”);

             break;

Default:  printf(“Please choose choice 1 or 2 only”);

}

In above example the statements which are following case are executed depending on choice given i.e. value of n. if value of n is 1 then result will be First choice selected and if value of n is 2 then result will be Second choice is selected. If choice is not 1 or 2 then result will be Please choose choice 1 or 2 only. The break statement is necessary otherwise it will continue. Suppose my choice is 1 and accidentally there is no break statement at the end of case 1 then it will continue to next statement irrespective of case and display First choice selected Second choice selected.

Example program

Program.

//Program for addition , subtraction and multiplication of two numbers using switch

#include<stdio.h<

#include<conio.h>

void main()

{

  int a,b,c,choice;

  clrscr();

  printf(“Enter two numbers :”);

  scanf(“%d%d”,&a,&b);

  printf(“Enter your choice :”);

  scanf(“%d”,&choice);

  switch(choice)

    {

      case 1: c=a+b;

                   printf(“\n Addition is %d”,c);

                   break;

       case 2: c=a-b;

                    printf(“\n Subtraction is %d”,c);

                    break;

       case 3: c=a*b;

                   printf(“\n Multiplication is %d”,c);

                   break;

        default: printf(“Enter choice between 1 to 3 only , Invalid choice”);

}

getch();

}


Related Videos on Youtube













Comments

Popular posts from this blog

INTRODUCTION TO LANGUAGES