C Programming - Control Statements

C Programming - Control Statements


SEQUENCE STATEMENT:
A sequential statement control structure is a linear structure. It executes statements one after another in a sequence. There is no mechanism for choosing alternate paths in statement flow. It executes one statement then automatically moves to next statement and so on.

Example:       // Program to show sequence flow
#include<stdio.h>
#include<conio.h>
void main()
{
printf(" I am in grade XII.\n");
printf(" I am a student of computer science.");
getch();
}                    

Flow chart of Sequence:
         



Decision making or Control  or Selection statement:
The statement which display one statement when the condition is true, otherwise display another statement is known as decision-making statement. Since these statement “control” the flow of execution, they are also known as control statement.
C language has following types of decision making statements are available as follows:
a) Simple if statement
b) if –else statement
c) Nested if-else statement
d) else---if ladder
e) Switch –case statement

a) Simple if Statement
The general form of a simple if statement is:
      if (test condition)
      {
                  statement-block;
      }
      statement-x;

The ‘statement-block’ may be a single statement or a group of statements. If the test expression is true, the statement block will be executed; otherwise the statement-block will be skipped and the execution will jump to the statement-x. Remember, when the condition is true both the statement block and the statement-x are executed in sequence.

Consider the following segment of a program that is written for processing of marks obtained in an entrance examination.
………………….
………………….
if (category= = sports)
{
Marks= marks + bonus_marks;
}
printf(“ %f ”,marks);
………………….
………………….
The program tests the type of category of the student. If the student belongs to the SPORTS category, then additional bonus_marks are added to his marks before they are printed. For others, bonus_marks are not added.

b) if---else statement
The if --- else statement is an extension of the simple if statement.
If the test expression is true, then the true-block statements, immediately following the if statement are executed; otherwise, the false-block statements are executed. In either case, either true-block or false block will be executed, not both. This is shown in flowchart below. In both the cases, the control is transferred subsequently to the statement-x.
 
Syntax:

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

 Let us consider an example of counting the number of boys and girls in a class. We use code 1 for a boy and 2 for a girl. The program statement to do this may be written as follows.
………………….     
………………….
if(code= = 1)                                  
 boy= boy+1;
if(code= = 2)
girl=girl+1;
………………….
………………….
The first test determines whether or not the student is a boy. If yes, the number of boys is increased by 1 and the program continues to the second test. The second test again determines whether the student is a girl. This is unnecessary. Once a student is identified as a boy, there is no need to test again for a girl, not both. The above program segment can be modified using the else clause as follows.
………………….
………………….
if(code = = 1)
boy=boy+1;
else
girl=girl+1;
………………….

c) Nested if ----- else statement
When a series of decisions are involved, we may have to use more than one if ----else statement in nested form as shown below:
if (test condition-1)
{
if (test condition-2)
{
Statement-1;
}
else
{
Statement-2;
}
}
else
{
Statement-3;
}
Statement-x;

If the condition-1 is false, the statement-3 will be executed; otherwise it continues to perform the second test. If the condition-2 is true, the statement -1 will be executed; otherwise the statement-2 will be executed and then the control is transformed to the statement –x.
A commercial bank has introduced an incentive policy of giving bonus to all its deposit holders. The policy is as follows: A bonus of 2% of the balance held on 31st December is given to everyone, irrespective of their balance, and 5% is given to female account holders if their balance is more than Rs. 5000. This logic can be coded as follows:


…….  
if(sex is female)
{
if(balance >5000)
bonus=0.05*balance;
else
bonus=0.02*balance;
}
else
{
bonus=0.02*balance;
}
balance=balance+bonus;                
……………

The logic of execution is illustrated in figure below.


d) The else---if ladder
This is another way of putting ifs together when multipath decisions are involved. A multipath decision is a chain of ifs in which the statement associated with each else is an if.
It takes the following general form:
if (condition 1)
          statement-1;
else if (condition 2)
          statement-2;
else if (condition 3)
          statement-3;
else if (condition n)
          statement-n;
else
          default - statement;
statement –x; 
This construct is known as the else - - if ladder. The conditions are evaluated from the top (of the ladder) to downwards. As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the statement – x (skipping the rest of ladder). When all the “n” conditions become false, then the final else containing the default – statement will be executed.
Let us consider an example of grading the students in an academic instituting.

The grading is done according to the following rules:
Average Marks
Grade
80 to 100
Distinction
60 to 79
First Division
50 to 59
Second Division
40 to 49
Third Division
0 to 39
Fail
This grading can be done using the else if ladder as follows:
if(marks>79)
          grade=”Distinction”;  
else if(marks>59)                                                                                                                                        
          grade=”First Division”;                                                                                                                                            
else if(marks>49)
grade=”Second Division”;                                                                                                                                           
else if(marks>39)
grade=”Third Division”;                                                                                                                                           
else
grade=”Fail”;                                                                                                                                            
printf(“%s\n”,grade);


e) Switch- Case Statement
The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statements associated with that case is executed.
The general form of the switch statement is as shown below:

switch(expression)
{
case value-1:
block-1;break;
case value-2:
block-2;
break;
…………………….
…………………….
default:
default – block;
break;
}
statement – x;
When the switch is executed, the value of the expression is successfully compared against the values value-1,value-2,..
If a case is found whose value matches with the value of the expression, then the block of statements that follow the case are executed.

The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement, transferring the control to the statement-x following with switch.

The default is an optional case. When present, it will be executed if the value of the expression does not match with any of the case values. If not present, no action takes place if all matches fail and the control goes to the statement-x.
Let us consider an example, which displays the name of 7 days according to input numbers.
 This can be shown as follows:
Program to display 1 for Sunday, 2 for Monday and so on.
#include<stdio.h>
void main ( )
{
int choice;
printf("Enter the number of the day=");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Wrong Choice");
}