Lession- 6 Decision Making

6.1 CONTROL STATEMENTS

The C language programs presented until now follows a sequential form of execution. Many times it is required to alter the flow of the sequence of instructions. C language provides statements that can alter the flow of a sequence of instructions. These statements are called control statements. These statements help to jump from one part of the program to another. The control transfer may be conditional or unconditional.

C language supports the following statements known as control or decision making statements.

1. if statement

2. switch statement

3. Conditional operator statement

4. goto statement

6.2 CONDITIONAL STATEMENTS

IF STATEMENT

The if statement is used to control the flow of execution of statements and isof the 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.

FIG-6.1

Eg: if(bank balance is zero)

Borrow money

The if statement may be implemented in different forms depending on the complexity of conditions to be tested.

1. Simple if statement

2. if…..else statement

3. Nested if…..else statement

4. elseif ladder

6.2.1 SIMPLE IF STATEMENT

The general form of a simple if statement is The ‘statement-block’ may be a single statement or a group of statement. 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.

If(test exprn)

{

statement-block;

}

statement-x;

Example:

if(category = = SPORTS)

{

marks = marks + bonus_marks;

}

printf(“%f “,marks);

……….

……….

6.2.2 THE IF…ELSE STATEMENT

The if….else statement is an extension of simple if statement.The general form is

                        If(test expression)

{

True-block statement(s);

}

else

{

False-block statement(s);

}

statement-x;

If the test expression is true, then the true block statements are executed; otherwise the false block statement will be executed.

FIG-6.2

For Example:

………

………

if(code ==1)

boy = boy + 1;

if(code == 2)

girl =girl + 1;

………

………

6.2.3 NESTING OF IF…..ELSE STATEMENTS

When a series of decisions are involved, we may have to use more than one if….else

statements, in nested form as follows.

If(test condition 1)

{

if(test condition 2)

{

statement-1;

}

else

{

statement-2;

}

}

else

{

statement-3;

}

statement-x;

FIG-6.3

Program

/*Selecting the largest of three values*/

void main()

{

float A, B, C;

printf(“Enter three values \n”);

scanf(“%f %f %f ”,&A, &B, &C);

printf(“\nLargest value is:”);

if(A > B)

{

if(A > C)

printf(“%f \n”,A);

else

printf(“%f \n”,C);

}

else

{

if(C > B)

printf(“%f \n”,C);

else

printf(“%f \n”,B);

}

}

OUTPUT

Enter three values:

5 8 24

Largest value is 24

6.2.4 THE ELSEIF LADDER

The general form is

If(condn 1)

Statement-1;

else if (condn 2)

statement-2;

else if (condn 3)

statement-3;

……….

……….

else if (condn n)

statement-n;

else

default statement;

statement-x;

Program

/*Use of else if ladder*/

void main()

{

int units, cno;

float charges;

printf(“Enter customer no. and units consumed \n”);

scanf(“%d %d”,&cno, &units );

if(units <= 200)

charges = 0.5 * units;

else if(units <= 400)

charges = 100+ 0.65 * (units – 200)

else if (units <= 600)

charges = 230 + 0.8 * (units – 400)

else

charges = 390 + (units – 600);

printf(“\n \ncustomer no =%d,charges =%.2f \n”,cno,charges);

}

OUTPUT

Enter customer no. and units consumed 101 150

Customer no=101 charges = 75.00

6.3 THE SWITCH STATEMENT

Switch statement is used for complex programs when the number of alternatives increases. The switch statement tests the value of the given variable against the list of case values and when a match is found, a block of statements associated with that case is executed.The general form of switch statement is:

switch ( integer expression )

{

case constant 1 :

do this ;

case constant 2 :

do this ;

case constant 3 :

do this ;

default :

do this ;

}

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. The keyword case is followed by an integer or a character constant. Each constant in each case must be different from all the others. The “do this” lines in the above form of switch represent any valid C statement.

What happens when we run a program containing a switch? First, the integer expression following the keyword switch is evaluated. The value it gives is then matched, one by one, against the constant values that follow the case statements. When a match is found, the program executes the statements following that case, and all subsequent case and default statements as well. If no match is found with any of the case statements, only the statements following the default are executed. Following examples will show how this control structure works.

………

………

index = marks / 10;

switch(index)

{

case 10:

case 9:

case 8:

grade = “Honours”;

break;

case 7:

case 6:

grade = “first division”;

break;

case 5:

grade = “second division”;

break;

case 4:

grade = “third division”;

break;

default:

grade = “first division”;

break;

}

printf(“%s \n”,grade);

If you want that only case 5 should get executed, it is upto you to get out of the switch then and there by using a break statement.

6.4 THE ?: OPERATOR

The C language has an unusual operator, useful for making two-way decisions. This operator is a combination of ? and : and takes three operands. It is of the form:

exp1?exp2:exp 3

Here exp1 is evaluated first. If it is true then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false then exp3 is evaluated and its value becomes the value of the expression. For example:

if(x < 0)

flag = 0;

else

flag = 1;

can be written as:

flag = (x < 0)? 0 : 1;

6.5 THE GOTO STATEMENT

C supports the goto statement to branch unconditionally from one point of the program to nother. The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name and must be followed by a colon.The general from is:

goto label                                                        label:

----------                                                            statement;

----------                                                            ----------

label:                                                               ----------

statement;                                                        goto label

Note: A label can be anywhere in the program, either before or after the goto label;statement.

Example of goto label statement:

void main()

{

double x, y;

read:

scanf(“%f”,&x);

if(x < 0)

goto read;

y = sqrt(x);

printf(“%f %f \n”,x, y);

goto read;

}

Last modified: Monday, 28 October 2013, 4:32 AM