Control Statements_Unit2
Control Statements_Unit2
Topics Covered: Go To, While, Do-While, For, Break and Continuous Statements
The GO TO Keyword
Avoid goto keyword! They make a C programmer’s life miserable. There is seldom a
legitimate reason for using goto, and its use is one of the reasons that programs become
unreliable, unreadable, and hard to debug. And yet many programmers find goto seductive.
In a difficult programming situation it seems so easy to use a goto to take the control where
you want. However, almost always, there is a more elegant way of writing the same program
using if, for, while and switch. These constructs are far more logical and easy to understand.
The big problem with gotos is that when we do use them we can never be sure how we got to
a certain point in our code. They obscure the flow of control. So as far as possible skip them.
The following program shows how to use goto.
}
And here are two sample runs of the program...
Enter the number of goals scored against India 3
To err is human!
Enter the number of goals scored against India 7
About time soccer players learnt C
and said goodbye! adieu! to soccer
A few remarks about the program would make the things clearer.
If the condition is satisfied the goto statement transfers control to the label ‘sos’,
causing printf( ) following sos to be executed.
The label can be on a separate line or on the same line as the statement following it, as
in, sos : printf ( "To err is human!" ) ;
Any number of gotos can take the control to the same label.
The exit( ) function is a standard library function which terminates the execution of
the program. It is necessary to use this function since we don't want the statement
printf ( "To err is human!" ) to get executed after execution of the else block.
The only programming situation in favour of using goto is when we want to take the
control out of the loop that is contained in several other loops.
While Statement
The While statement is used in such cases where we want to program something a fixed
number of times. Like to calculate gross salaries of ten different persons, or you want to
convert temperatures from centigrade to fahrenheit for 15 different cities .
The flowchart shown below would help you to understand the operation of the while loop.
Do-While Statement
Here, since the condition fails the first time itself, the printf( ) will not get executed at all.
Let's now write the same program using a do-while loop.
main( )
{
do
{
printf ( "Hello there \n") ;
} while ( 4 < 1 ) ;
}
In this program the printf( ) would be executed once, since first the body of the loop is
executed and then the condition is tested.
For Statement
/* Calculation of simple interest for 3 sets of p, n and r */
main ( )
{
int p, n, count ;
float r, si ;
for ( count = 1 ; count <= 3 ; count = count + 1 )
{
printf ( "Enter values of p, n, and r " ) ;
scanf ( "%d %d %f", &p, &n, &r ) ;
si = p * n * r / 100 ;
printf ( "Simple Interest = Rs.%f\n", si ) ;
}
}
The following figure would help in further clarifying the concept of execution of the for loop.
It is important to note that the initialization, testing and incrementation part of a for loop can
be replaced by any valid expression.
The break Statement
We often come across situations where we want to jump out of a loop instantly, without
waiting to get back to the conditional test. The keyword break allows us to do this. When
break is encountered inside any loop, control automatically passes to the first statement after
the loop. A break is usually associated with an if. As an example, let’s consider the
following example. Example: Write a program to determine whether a number is prime or
not. A prime number is one, which is divisible only by 1 or itself. All we have to do to test
whether a number is prime or not, is to divide it successively by all numbers from 2 to one
less than itself. If remainder of any of these divisions is zero, the number is not a prime. If no
division yields a zero then the number is a prime number. Following program implements
this logic.
main( )
{
int num, i ;
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
i=2;
while ( i <= num - 1 )
{
if ( num % i == 0 )
{
printf ( "Not a prime number" ) ;
break ;
}
i++ ;
}
if ( i == num )
printf ( "Prime number" ) ;
}
In this program the moment num % i turns out to be zero, (i.e. num is exactly divisible by i)
the message “Not a prime number” is printed and the control breaks out of the while loop.
The keyword break, breaks the control only from the while in which it is placed.