Professional Documents
Culture Documents
2.control Statements
2.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:
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.
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.
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.
7
break;
default:
default Block;
break;
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.
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
{
}
}
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.
11
#include<stdio.h>
void main()
do
}
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
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.
For Loop:
The for loop is another entry-controlled loop. The general form of the for loop is:
}
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 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.
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
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