Вы находитесь на странице: 1из 16

CONTROL STATEMENTS

A program is a sequence of instructions. Each instruction is executed only once in the


order it appears. But in practice, we have a number of situations where we may have to change
the order of execution of statements based on certain conditions, or repeat a group of statements
until certain specified conditions are met. This can be achieved with the help of control
statements.

TYPES OF CONTROL STATEMENTS:

 Branching statement

 Looping statement

 Jumping statement

BRANCHING

One of the several possible branches will then be carried out, depending on the outcome
of the logical test. This is known as branching. EX: if, if-else, nested if-else, else-if ladder.

SELECTION:

This is a special kind of branching. In which one group of statements is selected from
several available groups. EX: switch

LOOPING:

Sometimes the execution of certain statements needs to be repeated until a given


condition is satisfied or it may have to be repeated for a known number of times. Such
repetitions’ are carried out by using a looping statements. EX: for, while, do-while

JUMPING STATEMENT:

The execution control will be transferred from one place to another place with the help of
jumping statement. EX: goto, break, continue.

SIMPLE IF STATEMENT:

This is one of the decision making statement. Wherever we want to take a decision, we can use
‘if’ statement. The If structure has the following syntax
if (condition)

Statement-block;

Statement-x;
1
 The statement block may be a single statement or a group of statements.
 If the condition 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 statement-x are executed in
sequence.
 The condition part should not end with a semicolon, since the condition and statement
should be put together as a single statement.
Example program to check whether the number is a negative number
1. # include <stdio.h> //Include the stdio.h file
2. void main () // start of the program
3. {
4. int numbers; // declare the variables
5. printf ("Type a number:"); // message to the user
6. scanf ("%d", &number); // read the number from standard input
7. if (number < 0) // check whether the number is a negative number
8. printf ("The number is negative”);
9. }

The above program checks the value of the input number to see if it is less than zero. If it is then
the following program statement “The number is negative” will be executed. If the
value of the number is not less than zero, then the following statement is automatically
skipped.

THE IF ELSE CONSTRUCT:


If we want to perform one action when the specified condition will be met and also we want to
perform another one action when the condition will not be met, then in this type of situation we
can use if-else statement. The syntax of the If else construct is as follows:-
If(condition)
{
Statement-1;
}
Else
{
Statement-2;
}
2
 The if else is actually just on extension of the simple if statement.
 If the result of the condition is true, then statement 1 is executed. If the result of the
condition is false, then statement 2 will be executed.
Sample Code
1. #include <stdio.h> //include the stdio.h header file in your program
2. void main () // start of the main
3. {
4. int num ; // declare variable num as integer
5. printf ("Enter the number"); // message to the user
6. scanf ("%d", &num) ; // read the input number from keyboard
7. if (num < 0) // check whether number is less than zero
8. printf ("The number is negative") ; // if it is less than zero then it is negative
9. else // else statement
10. printf ("The number is positive") ; // if it is more than zero then the given number is
positive
11. }

 In the above program the If statement checks whether the given number is less than 0.
 If it is less than zero then it is negative therefore the condition becomes true then the
statement “The number is negative” is executed.
If(condition1)
 If the number is not less than zero the If else construct skips the first statement and prints
the second statement{ declaring that “the number is positive”.
NESTED IF-ELSE If(condition2)
 If we want to take a decision when two or more conditions will be satisfied,then we can
use nested if-else. Statement-1;
 The if statement may
elseitself contain another if statement is known as nested if statement.
Syntax: It can be in many forms. One of the form is
Statement-2;
}
else
{ If(condition2)
Statement-1;
else
Statement-2; 3

}
The if statement may be nested as deeply as you need to nest it. Condition 1 is tested first and
then condition 2 is tested. The second if condition is nested in if part. The third if condition is
nested in else part. The second if condition is tested only when the first condition is true else the
program flow will skip to check the third if condition.
 Statement-1 will only be executed if condition1 and condition2 are true.
 Statement-2 will only be executed if condition1 is true and condition2 is false.
 Statement-3 will only be executed if condition1 is false and condition3 is true.
 Statement-4 will only be executed if condition1 and condition3 are false.

Sample Code
1. #include <stdio.h>
2. main ()
3. {
4. int a,b,c,big; //declaration of variables
5. printf ("Enter three numbers"); //message to the user
6. scanf ("%d %d %d", &a, &b, &c); //Read variables a,b,c,
7. if (a > b) // check whether a is greater than b if true then
8. {
9. if (a > c) // check whether a is greater than c
10. big = a // assign a to big
11. else big = c // assign c to big
12. }
13. else
14. {

4
15. if (b > c) // if the condition (a > b) fails check whether b is greater than c
16. big = b // assign b to big
17. else big = c // assign c to big
18. }
19. printf ("Largest of %d, %d & %d = %d", a,b,c,big) //print the given numbers along
with the largest number
20. }

In the above program the statement if (a>c) is nested within the if (a>b). If the first If condition
if (a>b) is true only then the second if statement if (a>b) is executed. If the first if condition is
executed to be false then the program control shifts to else part.

The ELSE If Ladder


When a series of many conditions have to be checked for a single decision, we may use the else
if ladder, which takes the following general form.

If(condition1)

Statement1;

else if(condition2)

Statement2;

else if(condition3)

Statement3;

else if(condition-n)

Statement-n;

else

default statement;

statement-x. 5
The conditions are evaluated from the top of the ladder to downwards. As soon on the true
condition is found, the statement associated with it is executed and the control is transferred to
the statement – x (skipping the rest of the ladder). When all the condition becomes false, the final
else containing the default statement will be executed.
Example program using If else ladder to grade the student according to the following rules.

Marks Grade

70 to 100 DISTINCTION
60 to 69 IST CLASS
50 to 59 IIND CLASS
40 to 49 PASS CLASS
0 to 39 FAIL

Sample Code
1. #include <stdio.h> //include the standard stdio.h header file
2. void main () //start the function main
3. {
4. int marks; //variable declaration
5.
6. printf ("Enter marks\n"); //message to the user
7. scanf ("%d", &marks); //read and store the input marks.
8.
9. if (marks <= 100 && marks >= 70) //check whether marks is less than 100 or
greater than 70
10. printf ("\n Distinction"); //print Distinction if condition is True
11. else if (marks >= 60) //else if the previous condition fails Check
12. printf("\n First class"); //whether marks is > 60 if true print Statement
13. else if (marks >= 50) //else if marks is greater than 50 print
14. printf ("\n second class"); //Second class

6
15. else if (marks >= 35) //else if marks is greater than 35 print
16. printf ("\n pass class"); //pass class
17. else
18. printf ("Fail"); //If all condition fail apply default condition print Fail
19. }

Program description:
The above program checks a series of conditions. The program begins from the first if statement
and then checks the series of conditions it stops the execution of remaining if statements
whenever a condition becomes true.

THE SWITCH STATEMENT:


Disadvantages of if-else statement:
 The usage of multiple If else statement increases the complexity of the program since
when the number of If else statements increase it affects the readability of the program
and makes it difficult to follow the program.
The switch statement removes these disadvantages by using a simple and straight forward
approach.

The general format of the switch Statement is :


switch(expression)
{
case value-1:
Block-1;
break;
case value-2:
Block-2;
break;
.
.
.
case value-n:
Block-n;

7
break;
default:
default Block;

break;

 The expression is an integer expression or characters.


 The case values are an integer or character. Each of these case values should be unique
with in a switch statement.
 When the switch statement is executed the expression is evaluated first and the result of
expression is compared with the case values in the given order.
 If the case value matches with the value of the expression then the control is transferred
directly to the corresponding block of statement.
 If none of the case value matches then the default block is executed.
 The default statement is optional in switch statement.
 In case if any default statement is not given and if none of the condition matches then no
action takes place in this case the control transfers to the next statement of the switch
statement.
Sample Code
1. #include <stdio.h>
2. void main ()
3. {
4. int num1, num2, result;
5. char operator ;
6.
7. printf ("Enter two numbers");
8. scanf ("%d %d", &num1, &num2);
9. printf ("Enter an operator") ;
10. scanf ("%c", &operator);
11.
12. switch (operator)
13. {

8
14. case '+':
15. result = num1 + num2 ;
16. break ;
17. case '-':
18. result = num1 - num2 ;
19. break ;
20. case '*':
21. result = num1 * num2;
22. break ;
23. case '/':
24. if (num2 != 0)
25. result = num1 / num2 ;
26. else
27. {
28. printf ("warning : division by zero \n");
29. result = 0 ;
30. }
31. Break;
32. default:
33. printf ("\n unknown operator") ;
34. result = 0 ;
35. break ;
36. }
37. printf ("%d", result);
38. }

In the above program the break statement is need after the case statement to break out of the
loop and prevent the program from executing other cases.
LOOPING STATEMENTS:

9
 During looping, a set of statements are executed until some conditions for termination of
the loop is encountered.
 A program loop therefore consists of two segments one known as body of the loop and
other is the control statement.
 The control statement tests certain conditions and then directs the repeated execution of
the statements contained in the body of the loop.

In looping process in general would include the following four steps


 Setting and initialization of a counter.
 Execution of the statements in the loop.
 Test for a specified conditions for the execution of the loop.
 Incrementing the counter.
The test may be either to determine whether the loop has repeated the specified number of times
or to determine whether the particular condition has been met.

The While Statement:


The simplest of all looping structure in C is the while statement. The general format of the while
statement is:
while ( condition)

Body of the loop

 Here the given test condition is evaluated and if the condition is true then the body of the
loop is executed.
 After the execution of the body, the test condition is once again evaluated and if it is true,
the body is executed once again.
 This process of repeated execution of the body continues until the test condition finally
becomes false and the control is transferred out of the loop.
 On exit, the program continues with the statements immediately after the body of the
loop.
 The body of the loop may have one or more statements.
 The braces are needed only if the body contained two are more statements
Example program for generating ‘N’ Natural numbers using while loop:
#include<stdio.h>

void main()

10
{

int n,i=0; //Declare and initialize the variables

printf(“Enter the upper limit number”); // message to the user

scanf(“%d”,&n); //read and store the number

while(i<=n) //while statement with condition

{ // body of the loop

printf(“\t%d”,i); //print the value of i.

i++; //increment I to the next natural number

}
}
 In the above program the looping concept is used to generate n natural numbers.
 Here n and I are declared as integer variables and I is initialized to value zero.
 A message is given to the user to enter the natural number till where he wants to generate
the numbers.
 The entered number is read and stored by the scanf statement.
 The while loop then checks whether the value of I is less than n i.e., the user entered
number if it is true then the control enters the loop body and prints the value of I using
the printf statement and increments the value of I to the next natural number this process
repeats till the value of I becomes equal to or greater than the number given by the user.

THE DO WHILE STATEMENT:


The do while loop is also a kind of loop, which is similar to the while loop in contrast to while
loop. The do while loop tests at the bottom of the loop after executing the body of the loop.
Since the body of the loop is executed first and then the loop condition is checked. Here the body
of the loop is executed at least once.
The syntax of the do while loop is:
Do
{
statement;
}
while(expression);
Here the statement is executed, then expression is evaluated. If the condition expression is true
then the body is executed again and this process continues till the conditional expression
becomes false. When the expression becomes false the loop terminates.
Example program for generating ‘N’ Natural numbers using do-while loop:

11
#include<stdio.h>

void main()

int n,i=0; //Declare and initialize the variables

printf(“Enter the upper limit number”); // message to the user

scanf(“%d”,&n); //read and store the number

do

{ // body of the loop

printf(“\t%d”,i); //print the value of i.

i++; //increment I to the next natural number

} while(i<=n); // while statement with condition

}
Here without checking any condition, the loop statements are executed at first time. Then the
loop will be executed based on the result of the condition that is loop will be executed until I will
be equal to n.

NOTE:

Entry-controlled loop.

In the entry controlled loop, the conditions are tested before the start of the loop execution. If the
conditions are not satisfied, then the body of the loop will not be executed

Ex: While loop and for loop

Exit-controlled loop.

In the exit-controlled loop, the conditions are tested at the end of the body of the loop and
therefore the body is executed unconditionally for the first time.

EX: do-while loop.

For Loop:
The for loop is another entry-controlled loop. The general form of the for loop is:

for(initialization; condition; increment/decrement)

Body of the loop 12

}
 When the control enters for loop the variables used in for loop is initialized with the
starting value such as I=0,count=0.
 The value which was initialized is then checked with the given test condition. The test
condition is a relational expression, such as I < 5 that checks whether the given condition
is satisfied or not if the given condition is satisfied the control enters the body of the loop
or else it will exit the loop.
 The body of the loop is entered only if the test condition is satisfied and after the
completion of the execution of the loop the control is transferred back to the increment
part of the loop.
 The control variable is incremented using an assignment statement such as I=I+1 or
simply I++ and the new value of the control variable is again tested to check whether it
satisfies the loop condition. If the value of the control variable satisfies then the body of
the loop is again executed. The process goes on till the control variable fails to satisfy the
condition.
For loop example program:
/* The following is an example that finds the sum of the first fifteen positive natural numbers*/
#include<stdio.h>
void main()
{
int I,sum=0,sum_of_squares=0; //declare and initialize variable
for(i=0;i<=30;i+=2) //for loop
{
sum+=I; //add the value of I and store it to sum
sum_of_squares+=i*I; //find the square value and add it to sum_of_squares
} //end of for loop
printf(“Sum of first 15 positive even numbers=%d\n”,sum); //print sum
printf(“sum of squares=%d\n”,sum_of_squares); //print sum of square
}

13
JUMPING STATEMENT:

The execution control will be transferred from one place to another place with the help of
jumping statement. EX: goto, break, continue.

BREAK STATEMENT:
 The break statement allows the programmers to terminate the loop.

 The break skips from the loop or block in which it is defined.

 The control then automatically goes to the first statement after the loop or block.

 We can also use break statement in the nested loops. If we use break statement in the
innermost loop then the control of the program is terminated only from the innermost
loop.

Example program to illustrate the use of break statement.


/*Find the average of marks*/
#include<stdio.h>
void main()
{
int I,num=0; //declare and initialize the variables
float sm=0;,average;
printf(“Enter the marks, -1 to end\n”); //message to the user.
while(1) //while loop starts
{
scanf(“%d”,&i); //read and store the input number
if(i==-1) //check whether the input number is -1
break; //if number is -1 , skip the loop
sum+=I; else add the value of I to the sum
num++; //increment the num value by 1
} //end of while loop
} //end of the program

CONTINUE STATEMENT:
The continue statement is exactly opposite to the break statement. The continue statement is
used for continuing next iteration of the loop statements. When it occurs in the loop it does not
terminate, but it skips the statements after this statement. It is useful when we want to continue
the program without executing any part of the program.
The format of the continue statement is simply:
Continue;

14
Consider the following program that finds the sum of five positive integers. If a negative number
is entered, the sum is not performed since the remaining part of the loop is skipped using
continue statement.
Example program to illustrate the use of break statement.
/*Find the average of marks*/
#include<stdio.h>
void main()
{
int I,num=0; //declare and initialize the variables
float sm=0;,average;
printf(“Enter the marks, -1 to end\n”); //message to the user.
while(1) //while loop starts
{
scanf(“%d”,&i); //read and store the input number
if(i==-1) //check whether the input number is -1
continue; //if number is -1 , skip the iteration
else if(i==0)
break;
else
sum+=I; else add the value of I to the sum
num++; //increment the num value by 1
} //end of while loop
} //end of the program

THE GOTO STATEMENT:


This statement does not require any condition. This statement passes control anywhere in the
program i.e. control is transferred to another part to the program without testing any condition.
Syntax:
goto label;
Where label name must start with any character.
Here, the label is the position where the control is to be transferred.

Example:
/*checking the given number is odd or even*/
#include<stdio.h>
void main()

15
{
int x;
printf(“Eneter a number”);
scanf(“%d”,&x);
if(x%2==0)
goto even;
else
goto odd;

even:
printf(“\n Even number”);
exit(0);
odd:
printf(“\n odd number”);
}

16

Вам также может понравиться