Monday, April 29, 2019

C language : conditional statement

                      Conditional statement in C language

Conditional statement: The conditional statement help us to jump from one part of the program to another depending on whether a particular condition is satisfied or not. These decision control statement include:

1. if statement
2. if else statement
3. if else if statement
4. switch case statement

1. if statement-
The if statement is the simplest form of decision control statement that is frequently used in decision making. The syntax of if statement is shown in figure:

if (test expression)
{
Statement 1;
..........
............
Statement n;
}
Statement x;

The if statement may include one statement or more statements enclosed within curly braces. First the test expression is evaluated, if the test expression is it true then statement of if block, statement 1 to n are executed otherwise these statements will be skipped and execution will jump to the statement x . In control statement no semicolon is used after the test expression.

For example,
/* Program to find whether a person is eligible for vote or not. */

#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
int age;
printf ("\n Enter the age ");
scanf ("%d",&age);
if(age>=18)
printf ("\n Person is eligible for vote.");
getch();
}

2 . if else statement
       The decision control statement in which, the test expression is first evaluated. If the expression is true, statement Block 1 is executed and the statement Block 2 is skipped otherwise if the expression is false statement 2 is executed and statement 1 is skipped or ignored. The syntax for simple if else statement is shown in figure:

if (test expression)
{
Statement 1;
}
else
{
Statement 2;
}
Statement x;

Now in any case after the statement Block 1 and 2 gets executed ,the control will pass to statement x. Therefore, statement x is executed in every case.

For example,

/* Program to find whether a given number is even or odd. */

#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
int n;
printf ("\n Enter the value of n  ");
scanf ("%d",&n);
if(n%2==0)
printf ("\n Entered number is even. );
else
printf ("\n Entered number is odd.");
getch();
}

3 . If else if statement
      C language supports if else if statement to test an additional condition apart from the initial test expression. The if else if construct work in the same way as a normal if statement.
if else if statement is also known as nested if construct. It is not necessary that every if statement should have an else block as C language supports simple if statement. After the first expression or the first if branch the programmer can have many else if branches as he wants depending on the test expression that has to be tested . The syntax for if else if statement is shown in figure:

if (test expression)
{
statement 1;
}
else if (test expression 2)
{
statement 2;
}
.....................
......................
else

statement X;
}


For example,

/* Program to find whether a given number is greater or smaller or equal. Only for positive numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("\n Enter the value of a and b ");
scanf("%d %d",&a,&b);
if(a>b)
printf("\n a is greater than b ");
else if(b>a)
printf("\n b is greater than a ");
else
printf("\n a is equal to b ");
getch();
}

4. Switch case statement
     A switch-case statement is a multiway decision statement that is simplified version of an if else block that evaluates only one variable. The syntax of switch case is shown in figure:

switch (variable)
{
case 1:
statement 1;
break;
case 2:
statement 2;
break;
................
...................
case n:
statement n;
break;
default:
statement y;
}

Switch case mostly used in two situation-
1. When there is only one variable to evaluate in the expression.
2 . When many condition are being tested for.

              When there are many condition to test, using if -else- if construct become a bit complicated and confusing. Therefore, switch case statement are often used as an alternative to long if statement that compares a variable to several integral values. The switch case statement compares the value of a variable given in the switch statement with the value of each case statement that follows. If the value of a switch and the case statement matches then the statement block of that particular case is executed.
In the switch case, the break statement must be used at the end of each case because if it were not used then all the cases from one met will be executed.

For example,
/* program for switch case statement */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
    int choice;
   while(1)
    {
  printf("\n 1. DAYS   ");
  printf("\n 2. MONTHS   ");
  printf("\n 3. Exit");
    printf("\n Enter your choice  ");
    scanf("%d",&choice);
    switch(choice)
    {
  case 1:
       printf(" Name of the days are ");
       printf("\n ");
       printf("\n Monday ");
       printf("\n Tuesday ");
       printf("\n Wednesday ");
       printf("\n Thrusday ");
       printf("\n Friday ");
       printf("\n Satday ");
       printf("\n Sunday ");
        break;
  case 2:
       printf(" Name of the months are ");
       printf("\n ");
       printf("\n January ");
       printf("\n Febuary ");
       printf("\n March ");
       printf("\n April ");
       printf("\n May ");
       printf("\n June ");
       printf("\n July ");
       printf("\n August ");
       printf("\n September ");
       printf("\n October ");
       printf("\n November ");
       printf("\n December ");
                break;
 
  default:
        printf("wrong choice");
         break;
case 3:
        exit(0);
     
   
    }
    getch();
}}

No comments:

Post a Comment

Function

FUNCTION C enables programmer to break up a program into segments commonly known as functions, each of which can be written more or...