15. Fundamental Control Structure Statements in C [Part-2]

Table of Contents
< All Topics
Print

15. Fundamental Control Structure Statements in C [Part-2]

What we discussed in previous post?

We actually try to understand Control Structures in C Programming: Selection, Iteration, and Jump Statements and then we have explored the fundamental control structures as if() statement and if()…else statement. In support we have also shared some simple examples to have a proper feel of the concept.

Now in this post we will go deeper into these and will discuss about nested if()… else, if…else if ladder and switch statements

3) Nested if-else statement as a control structure

A user can use if…else statements in-side any specific if or else condition in the program.

Syntax: The syntax of a nested if( )…else statement in C programming language is:
if( condition_1) 
// statements for true part of condition
if(condition_2) 
{ /* Statement(s) when cond_1 and cond_2 both are true. */
} 
else 
{   /* statements for condition 1 as true and false for condition2 */
}
} 
else
{
 if(condition_3) 
{ /* Statement(s) when cond_1 and cond_2 both are true. */
} 
else 
{   /* statements for condition 1 as true and false for condition2 */
} 

Example: Greatest Among Three Given Numbers

"); scanf("%d%d%d", &a, &b, &c); if (a>=b) { if(b>=c) printf("\n %d is greatest in all",a); else if(a>=c) printf("\n %d is greatest in all",a); else printf("\n %d is greatest in all",c); } else if(b>=c) printf("\n %d is greatest in all",b); else printf("\n %d is greatest in all",c); }// end main" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button">
//WAP in C to find the greatest number among three numbers using nested if else.
#include<stdio.h>
void main()
{
int a,b,c;
printf("\n Enter the numbers :----->  ");
scanf("%d%d%d", &a, &b, &c);

if (a>=b)
{
if(b>=c)
printf("\n %d is greatest in all",a);
else
if(a>=c)
printf("\n %d is greatest in all",a);
else
printf("\n %d is greatest in all",c);
}
else
if(b>=c)
printf("\n %d is greatest in all",b);
else
printf("\n %d is greatest in all",c);

}// end main
Output
output window

4) if…else if…else statement as a control structure

Think a situation when we have to work with multiple if…else conditions and we apply multiple if…else blocks then each if…else block is treated as separate condition and each of these if…else block will be being checked by the compiler, also this raises a problem that we get both desired and undesired outputs.

To overcome this problem, C has introduced a construct called if…else if…else block of statement where we have one If () block, one or more else if () blocks and at last an else block of code.

i.e. multiple else if () conditions are stuffed between the if() and else block of statement.

Flowchart
Nested If MB 1024x629

Working: From the above flowchart, when condition1 if true, it will just move to the very next statement after the if…else if…else construct. When condition1 is false then it moves to check condition2 and so on. At last, when all the conditions are false control moves to else block of the construct.

Rules for multiple if … else condition sets.
  • While using if…else if… else statements, there are few points to keep in mind −
  • An if can have zero to many else if’s and they must come before the else.
  • Once an else if succeeds, none of the remaining else if’s or else’s will be tested.
  • An if can have zero or one else’s and it must come after any else if’s.
Syntax:
if(Condition_ 1) 
{
// Set of statements for condition 1 as true
} 
else if(Condition_ 2) 
{
   // Set of statements for condition 2 as true
}
 else if( Condition_3) 
{
   // Set of statements for condition  as true
} 
else
 {
   // Set of statements when all above conditions  are false
 }

Example: Check the grade of the student

100) printf("\n Marks are out of approved limit"); else if(marks>=85) printf("\n your marks= %d and grade is A++", marks); else if(marks>=75) printf("\n your marks= %d and grade is A+", marks); else if(marks>=65) printf("\n your marks= %d and grade is B+", marks); else if(marks>=55) printf("\n your marks= %d and grade is B", marks); else if(marks>=45) printf("\n your marks= %d and grade is C", marks); else printf("\n Better Luck Next Time! marks=%d, grade FAIL", marks); }" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button">
// ckeck the grade of the student
// marks <=100 -----> approved limit
// marks >=85     A++
// marks >=75     A+
// marks >=65     B+
// marks >=55     B
// marks >=45     C
// marks <45      Fail.

#include<stdio.h>
void main()
{ 
int marks;
printf("\n Enter your marks out of 100 : ");
scanf("%d", &marks);

if (marks>100)
printf("\n Marks are out of approved limit");
else if(marks>=85)
printf("\n your marks= %d and grade is A++", marks);
else if(marks>=75)
printf("\n your marks= %d and grade is A+", marks);
else if(marks>=65)
printf("\n your marks= %d and grade is B+", marks);
else if(marks>=55)
printf("\n your marks= %d and grade is B", marks);
else if(marks>=45)
printf("\n your marks= %d and grade is C", marks);
else
printf("\n Better Luck Next Time! marks=%d, grade FAIL", marks);
}
Output
Screenshot 2023 11 18 135541 1024x123

5) switch statement as a control structure

The switch statement in C is an alternate to if-else-if ladder statement. we can define various statements in the multiple cases for the different values of a single variable.

Flowchart
Switch MB 1024x756

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;    
}    // end switch statement.
Rules for using switch statement:
  1. Theswitch expressionmust be of an integer or character type.
  2. Thecase valuemust be an integer or character constant.
  3. Thecase valuecan be used only inside the switch statement.
  4. Thebreak statementin switch case is not must. It is optional.

Let’s try to understand the valid and invalid cases by the examples. We are assuming that there are following variables.

int x,y,z;  
char a,b;  
float f;
Valid SwitchInvalid SwitchValid CaseInvalid Case
switch(x)switch(f)case 3;case 2.5;
switch(x>y)switch(x+2.5)case ‘a’;case x;
switch(a+b-2)case 1+2;case x+2;
switch(func(x,y))case ‘x’>’y’;case 1,2,3;

Example: Create calculator using SWITCH statement

// program to create calculator using SWITCH statement
#include<stdio.h>

void main()
{int a,b;
char ch;
printf("\n Enter two numbers \n");
scanf("%d",&a);
scanf("%d",&b);
printf("\n Press:\n + for addition\n - for substraction\n * for multiplication\n / for divide operation");
scanf("%c",&ch);
switch(ch)
{ case '+': 
printf("\n addition of %d and %d  = %d",a,b, a+b); 
break;
  case '-':
  printf("\n substraction of %d and %d  = %d",a,b,a-b);
  break;
  case '*':
  printf("\n multiplication of %d and %d  = %d",a,b, a*b);
  break;
  case '/':
  printf("\n divide of %d and %d  = %d",a,b, a/b);
  break;
 default:
 printf("\n wrong operator");
}// end switch

}// end main

Example: Write a switch-based menu driven program in C with the following options-


A. Find greater between two numbers.
B. Check, number is even or odd.
C. Calculate area of Circle.
D. Exit

"); scanf("%c",&ch); switch(ch) { case 'A': case 'a': // Find greater between two numbers. printf("\n Enter Two Numbers : "); scanf("%d%d", &n1,&n2); max = n1>=n2? n1 : n2; printf("\n greater number is %d", max); break; case 'B': case 'b': // Check, number is even or odd. printf("\n Enter The Number : "); scanf("%d", &num); res = ((num%2) == 0 )? printf("\n num =%d is EVEN", num): printf("\n num =%d is ODD", num); break; case 'C': case 'c': //Calulate area of Circle. printf("\n Enter The radius : "); scanf("%f", &r); area= 3.14*r*r; printf("\n area covered by radius %f is %f", r, area); break; case 'D': case 'd': // take Exit printf("\n Bye !!"); break; default: printf("\n INVALID INPUT"); }// end switch }// end main" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button">
/*
Write a menu driven program in C with the following options- 
A. Find greater between two numbers. 
B. Check, number is even or odd. 
C. Calulate area of Circle. 
D. Exit
*/

#include<stdio.h>
void main()
{
char ch;
int n1, n2, max;
int num, res;
float r, area;
printf("\n Mention your choice\n Press A for greater between two numbers");
printf("\n Press B to Check, number is even or odd");
printf("\n Press C to Calulate area of Circle.");
printf("\n Press D to take Exit : \t -> ");
scanf("%c",&ch);
 
 switch(ch)
 {
 case 'A':
case 'a':
// Find greater between two numbers.
printf("\n Enter Two Numbers : "); scanf("%d%d", &n1,&n2);
max = n1>=n2? n1 : n2;
printf("\n greater number is %d", max);
break;


case 'B':
case 'b':
// Check, number is even or odd.
printf("\n Enter The Number : "); scanf("%d", &num);
 res = ((num%2) == 0 )? printf("\n num =%d is EVEN", num): printf("\n num =%d is ODD", num);
break;

case 'C':
case 'c':
//Calulate area of Circle. 
printf("\n Enter The radius : "); scanf("%f", &r);
area= 3.14*r*r;
printf("\n area covered by radius %f is %f", r, area);
break;
case 'D':
case 'd':
// take Exit
printf("\n Bye !!");
break;
default:
printf("\n INVALID INPUT");


 }// end switch
}// end main

1 thought on “15. Fundamental Control Structure Statements in C [Part-2]”

Leave a comment