Chapter 11. Control Statements in C

conditional

Contributed By: Shehrevar Davierwala

1. What are control statements in C ?

In C, programs are executed sequentially in the order of which they appear. This condition does not hold true always. Sometimes a situation may arise where we need to execute a certain part of the program. Also it may happen that we may want to execute the same part more than once. Control statements enable us to specify the order in which the various instructions in the program are to be executed. They define how the control is transferred to other parts of the program. Control statements are classified in the following ways:

classification
classification

2. What are selection statements/decision statements/branching statements in C ?

Decision-making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

E.g. If the weather is sunny, I will go out & play, else I will be at home. Here my course of action is governed by the kind of weather. If it’s sunny, I can go out & play, else I have to stay indoors. I choose an option out of 2 alternate options. Likewise, we can find ourselves in situations where we have to select among several alternatives. We have decision control statements to implement this logic in computer programming.

3. Write a note on if statement ?

if Statement:
The keyword if tells the compiler that what follows is a decision control instruction. The if statement allows us to put some decision -making into our programs. The general form of the if statement is shown below

ifstatement

Syntax of if statement:
if (condition )
{
Statement 1;
…………..
Statement n;
}
//Rest of the code
If the condition is true(nonzero), the statement will be executed. If the condition is false(0), the statement will not be executed. For example, suppose we are writing a billing program.

if (total_purchase >=1000)
printf(“You are gifted a pen drive.\n”);
Multiple statements may be grouped by putting them inside curly braces {}. For example:
if (total_purchase>=1000)
{
gift_count++;
printf(“You are gifted a pen drive.\n”);
}
For readability, the statements enclosed in { } are usually indented. This allows the programmer to quickly tell which statements are to be conditionally executed. As we will see later, mistakes in indentation can result in programs that are misleading and hard to read.

4. Few programs based on if statement.

1. Write a program to print a message if negative no is entered.
#include<stdio.h> int main()
{
int no;
printf(“Enter a no : “);

scanf(“%d”, &no); if(no<0)
{
printf(“no entered is negative”); no = -no;
}
printf(“value of no is %d \n”,no);
return 0; }
Output 1:
Enter a no: 6

value of no is 6
Output 2:
Enter a no: -2

value of no is 2

2. Write a program to perform division of 2 nos
#include<stdio.h> int main()
{
int a,b; float c;
printf(“Enter 2 nos : “);
scanf(“%d %d”, &a, &b);
if(b == 0)
{
printf(“Division is not possible”);
}
c = a/b;
printf(“quotient is %f \n”,c);
return 0; }

Output 1:
Enter 2 nos: 6 2

quotient is 3
Output 2:
Enter 2 nos: 6 0
Division is not possible

5. What is if-else statement ?

The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. By using else we execute another group of statements if the expression evaluates to false.
if (a > b)
{ z = a;
printf(“value of z is :%d”,z);
}
else
{ z = b;
printf(“value of z is :%d”,z);
}
The group of statements after the if is called an ‘if block’. Similarly, the statements after the else form the ‘else block’.

ifelse

6. Programs on if-else statements

1.Write a program to check whether the given no is even or odd
#include<stdio.h>
int main()
{
int n;
printf(“Enter an integer\n”); scanf(“%d”,&n);
if ( n%2 == 0 )
printf(“Even\n”); else

printf(“Odd\n”);
return 0;
}
Output 1:
Enter an integer 3
Odd

Output 2:
Enter an integer 4
Even

2. Write a program to check whether a given year is leap year or not
#include <stdio.h>
int main()
{
int year;
printf(“Enter a year to check if it is a leap year\n”);
scanf(“%d”, &year);
if ( (year%4 == 0) && (( year%100 != 0) || ( year%400 == 0 ))
printf(“%d is a leap year.\n”, year);
else
printf(“%d is not a leap year.\n”, year);
return 0;
}
Output 1:
Enter a year to check if it is a leap year 1996
1996 is a leap year
Output 2:
Enter a year to check if it is a leap year 2015
2015 is not a leap year

7. What is nested if-else statement ?

An entire if-else construct can be written within either the body of the if statement or the body of an else statement. This is called ‘nesting’ of ifs. This is shown in the following structure.
if (n > 0)
{
if (a > b)
z = a;
}
else
z = b;
The second if construct is nested in the first if statement. If the condition in the first if statement is true, then the condition in the second if statement is checked. If it is false, then the else statement is executed.

8. Programs on nested if-else

1. Write a program to check for the relation between 2 nos
#include <stdio.h>
int main()
{
int m=40,n=20;
if ((m >0 ) && (n>0))
{
printf(“nos are positive”);
if (m>n)
{
printf(“m is greater than n”);
}
else
{
printf(“m is less than n”);
}
}
else

{
printf(“nos are negative”);
}
return 0;
}
Output
40 is greater than 20

9. Write note on else-if statement ?

This sequence of if statements is the most general way of writing a multi−way decision. The expressions are evaluated in order; if an expression is true, the statement associated with it is executed, and this terminates the whole chain. As always, the code for each statement is either a single statement, or a group of them in braces.
if (expression)
statement
else if (expression)
statement
else if (expression)
statement
else if (expression)
statement
else
statement
The last else part handles the “none of the above” or default case where none of the other conditions is satisfied. Sometimes there is no explicit action for the default; in that case the trailing can be omitted, or it may be used for error checking to catch an “impossible” condition.

10. Program on else-if

The above program can be used as an eg here.
#include <stdio.h>
int main()
{

int m=40,n=20; if (m>n)
{
printf(“m is greater than n”);
}
else if(m<n)
{
printf(“m is less than n”);
}
else
{
printf(“m is equal to n”);
}
}
Output 1:
m is greater than n

11. Explain switch statement

This structure helps to make a decision from the number of choices. The switch statement is a multi−way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly [3].
switch( integer expression)
{
case constant 1 :
do this;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}

switch

The integer expression following the keyword switch is any C expression that will yield an integer value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer. If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labelled default is executed if none of the other cases are satisfied. A default is optional; if it isn’t there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order.

Example:

main( )
{ int i = 2; switch ( i )
{
case 1:
printf ( “I am in case 1 \n” ) ; case 2:
printf ( “I am in case 2 \n” ) ; case 3:
printf ( “I am in case 3 \n” ) ; default :
printf ( “I am in default \n” ) ; }
}
The output of this program would be:
I am in case 2
I am in case 3
I am in default

Explanation:

Here the program prints case 2 and 3 and the default case. If you want that only case 2 should get executed, it is up to you to get out of the switch then and there by using a break statement.

main( )
{
int i = 2 ;
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 ;
default:
printf ( “I am in default \n” ) ;
}
}
The output of this program would be:
I am in case 2

12. Program on switch statement

to enter a grade & check its corresponding remarks.
#include <stdio.h> int main ()
{
char grade;
printf(“Enter the grade”);
scanf(“%c”, &grade);
switch(grade)
{
case ‘A’ :printf(“Outstanding!\n” );
break;
case ‘B’ : printf(“Excellent!\n” );
break;
case ‘C’ :printf(“Well done\n” );
break;
case ‘D’ : printf(“You passed\n” );
break;
case ‘F’ : printf(“Better try again\n” );
break;
default : printf(“Invalid grade\n” );
}

printf(“Your grade is %c\n”, grade ); return 0;
}
Output
Enter the grade
B
Excellent
Your grade is B

13. Important rules for switch statement

The following rules apply to a switch statement:
 The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
 You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
 The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
 When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
 When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

 Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
 A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

14. Explain nested switch statement

It is possible to have a switch as a part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.

Syntax
The syntax for a nested switch statement is as follows:
switch(ch1) {
case ‘A’:
printf(“This A is part of outer switch” );

switch(ch2) {
case ‘A’:
printf(“This A is part of inner switch” );
break;
case ‘B’: /* case code */
}
break;
case ‘B’: /* case code */
}

Example:

#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
switch(a) {
case 100:
printf(“This is part of outer switch\n”, a );
switch(b) {
case 200:
printf(“This is part of inner switch\n”, a );
}
}
printf(“Exact value of a is : %d\n”, a );
printf(“Exact value of b is : %d\n”, b );
return 0;
}

When the above code is compiled and executed, it produces the following result:

This is part of outer switch
This is part of inner switch
Exact value of a is : 100
Exact value of b is : 200

 

 

Leave a Reply

Your email address will not be published.