C is Easy

C Programming made easy with just a few little steps

If Else and Switch Case statements

We have seen that a C Program is a set of statements, which are normally executed sequentially in the order in which they appear. This happens when no repetitions are necessary. However, we may have some situations in which we would have to change the sequence of execution of statements based on certain conditions. C Programming Language offers such decision making capabilities in the form of decision making statements like

  1. If Else Statement
  2. Switch Statement  
If Statement

It is an important decision making statement and is also a two-way decision making statement. It takes the following form

if(test expression)

It allows the computer to evaluate the expression first and then, depending on whether the value of the expression is 'true' or 'false', it transfers the control to a particular statement. From here there are two paths, one for the true condition and the other for the false condition.

It can be used in different ways:-

1. The Simple If Statement

The syntax of this statement is:

if(test expression)
{
statement block-1;
}
statement-x;

Explanation:-

If the test expression is true then the statement block will be executed otherwise it will be skipped and execution will jump to statement x. Note:- When the test expression is true both the statement block and the statement-x will be executed.

Q. WAP to accept three integers and print the largest of the three using Simple If statement?

Ans.

#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c,max;
clrscr();
printf("Enter three integers\n");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{ max=a;
if(max>c)
{ printf("\nThe largest integer is: %d",a);}
if(c>max)
{ printf("\nThe largest integer is: %d",c);}
}
if(b>c)
{ printf("\nThe largest integer is: %d",b);
if(c>b)
{ printf("\nThe largest integer is: %d",c);}
}
getch();
}




Output


2. The If....else Statement

It is an extension of the simple if statement.
Syntax:-

if(test expression)
{           True block statement(s);
}
else
{           False block statement(s);
}
statement-x;

Explanation:-
If the test expression is true, then the true block statement(s), immediately following the If statement are executed; otherwise the false block statement(s) are executed. In either case, either true block or false block will be executed, not both.

Q. WAP to find whether a integer is a multiple of 2 and 5?

Ans.

#include<stdio.h>
#include<conio.h>
void main()
{ int a;
printf("Input an integer\n");
scanf("%d",&a);
if(a%2==0 && a%5==0)
{ printf("%d is a multiple of 2 and 5\n",a);}
else
{       printf("%d is not a multiple of 2 and 5\n",a);}
}


Output



3. The nested ifs

A nested if is an if that has another if in its if's body or in its else's body.The nested if can have one of the following three forms:

1. if(expression1)
   {             :
                  if(expression2)
                  statement-1;
                  else
                  statement-2;
   }
   else
   body-of-else;

2.if(expression1)
body-of-if;
else
{ :
if(expression2)
statement-1;
else
statement-2;
        :
}

3.if(expression1)
{ :
if(expression2)
statement-1;
else
statement-2;
:
}
else
{ :
if(expression2)
statement-3;
else
statement-4;
        :
}

Q.Program for a four function Calculator

Ans.

void main()
{ int ch;
float a,b,result=0;
clrscr();
printf("Enter two numbers\n");
scanf("%f %f",&a,&b);
printf("\nEnter the required arithmetic operatorion\n1.Addition+\n2.Subtraction-\n3.Multiplication*\n4.Division/:");
scanf("%d",&ch);
if(ch==1)
result=a+b;
else
if(ch==2)
result=a-b;
else
if(ch==3)
result=a*b;
else
if(ch==4)
result=a/b;
else
{printf("\nWrong Operator....Aborting!!!");}
printf("\nThe calculated result is:%f",result);
getch();
}



Output





The Conditional Operator

This operator can be used as an alternative to the if statement. This operator can be used to replace if-else statements of the form:
  if(expression1)
expression2
else
expression3;

this can be written as:

expression1?expression2:expression3;

For example,

int c;
if(a<b)
c=a;
else
c=b;

int c=a<b?a:b;

THE SWITCH STATEMENT


It is a selection statement that succesively tests the value of an expression against a list of integer or character constants. When a match is found, the statement associated with that constant are executed.The syntax is:

switch(expression1)
{           case 1:
                 constant1:   statement sequence 1;
                 break;
              case 2:
                 constant2:   statement sequence 2;
                 break;
               case 3:
                 constant3:   statement sequence 3;
                 break;
               :
               case n-1:
                 constant1:   statement sequence n-1;
                 break;
              default: statement sequence-n;
}

Q. Four function calculator using switch case

Ans.

#include<stdio.h>
#include<conio.h>
void main()
{ int ch;
float a,b,result=0;
clrscr();
printf("Enter two numbers\n");
scanf("%f %f",&a,&b);
printf("\nEnter the required arithmetic operatorion\n1.Addition+\n2.Subtraction-\n3.Multiplication*\n4.Division/:");
scanf("%d",&ch);
switch(ch)
{       case 1:
result=a+b;
break;
case 2:
result=a-b;
break;
case 3:
result=a*b;
break;
case 4:
result=a/b;
break;
default:
printf("\nWrong Operator....Aborting!!!");
}
printf("\nThe calculated result is:%f",result);
getch();
}


Output


Programs for Practice:-

1.Temperature Conversion Program
2.Program for calculation of area of circle, rectangle, square and triangle

1 comment:

  1. Very informative blog!

    Please take some time to visit my blog @
    loop in C notes

    Thanks!

    ReplyDelete