Control Statements

 

In C programs, statements are executed sequentially in order of which they appear in the program. But we may want to use a condition for doing only a part of program. Also many situations arise where we may want to execute some statements several times.

 

Central statements enable us to specify the order in which the various instructions in the program are to be executed. This determines the flow of control. Control statement defines how the control is transferred to other parts of the program. Control statement defines the flow.

 

C language support four types of Control Statements which are as :

1) if

2) if-else

3) goto

4) switch

5) loops

  1. i) while
  2. ii) do-while

iii) for

 

 

1) if:

This statement is used to test the condition and take a possible action. If the condition is true then a single statement or a block of statements is executed.

 

Syntax-1

if (condition)

{

statement

————-

————-

}

 

 

 

 

 

 

 

true

 

Block of statements

 

 

 

 

 

 

If Statement

Fig (1)

 

 

 

2) if-else

This is a bi-directional conditional control statement. This statement is used to test the condition and take one of the two possible actions. If the condition is true then a single statement or a block of statements is executed, otherwise another single statement or a block of statements is executed.

 

Syntax:

if (condotion)

{

statement

————

————

}

else

{

statement

————

————

}

 

 

 

 

 

 

 

 

Block of statements

false

 

 

 

true

 

Block of statements

 

 

 

 

 

 

If   Else Statement

Fig (2)

 

 

 

3) Nested if-else

In if statements there amy be one or more if statements. In such a case, they are said to be nested. An example of a nested if statement is shown below:

if condition-1

{

if condition-2

{

Statement-1;

}

else

{

statement-2;

}

}

else

{

statement-3;

}

 

 

 

 

 

 

 

 

 

 

Block of statements

false

 

 

 

true

 

 

 

 

 

false

 

true

Block of statements

 

 

 

 

 

 

Nested If Else Statement

Fig (3)

 

 

/*To find the largest number*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b;

clrscr();

printf(“enter the value of a n”);

scanf(“%d”,&a);

printf(“enter the value of b n”);

scanf(“%d”,&b);

if(a>b)

printf(“a is greatest”);

else

printf(“b is greatest”);

getch();

}

————————————————————————————————————

 

 

 

 

/*largest no. b/w three no.*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

clrscr();

printf(“enter the value of a,b&c”);

scanf(“%d%d%d”,&a,&b,&c);

if (a>b&&b>c)

printf(“a is greatest”);

else

if(b>c&&c>a)

printf(“b is greatest”);

else

printf(“c is greatest”);

getch();

}

 

 

 

 

Goto:

This is an unconditional control statement. When we use this statement the flow of control is transferred to another part of the program without testing any other condition.

the goto statement can be used as:

goto labelname;

——————-

——————-

——————–

labelname;

statement

————-

————-

labelname is the label where the flow of control goes.

 

program:

 

/*wap for goto statement*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b;

clrscr();

printf(“enter the value of a&b”);

scanf(“%d%d”,&a,&b);

if (a>b)

goto large;

else

goto small;

large:

printf(“a is large”);

 

small:

printf(“b is large”);

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CONTROL TRANSFER AND DECSION MAKING

/EXERCISE/

1.) What is normal order of execution of statement? How can be changed?

2.) State three types of control statements in c.

3.) State various conditional control statements supported by c.

4.) State various unconditional control statement supported by c.

5.) State various loop statement supported by c.

6.) What do you mean by if statement in c? State its general form.

7.) State purpose and syntax of if……….. e l se statement.

8.) What do you mean by nested if statement? Illustrate using some example.

9.) Write a program to determine smallest number between two numbers.

10.) Write a program to determine smallest among three numbers.

11.) Write a program to check whether given number is odd or even.

12.) Write a program to check whether given number is divisible by 5 or not.

13.) Write a program to determine net salary for a given basic pay using following information.

If basic pay<_5000, DA=60% of basic pay

HRA=20% of basic pay

Otherwise DA=55% basic pay

HRA =22% of basic pay

14.) Write a program to find roots of a quadratic equation.

15.) Write a program to check whether entered number is positive , negative or zero.

16.) Write a program to display whether a student is in merit or not, given his marks in 3 subjects.

(A student is in merit if percentage>_75%)

I f (b p<4000) hr a=250 else} h r a=500

}

Leave a Reply

Leave a Reply

Your email address will not be published.