0% found this document useful (0 votes)
9 views

Control Statements_Unit2

The document discusses various control statements in programming, including the use of 'goto', 'while', 'do-while', 'for', 'break', and 'continue'. It emphasizes avoiding 'goto' due to its potential to make code difficult to read and debug, while providing examples of how to use the other control structures effectively. Additionally, it explains the differences between 'while' and 'do-while' loops, as well as the functionality of 'break' and 'continue' statements within loops.

Uploaded by

Dr. Salma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Control Statements_Unit2

The document discusses various control statements in programming, including the use of 'goto', 'while', 'do-while', 'for', 'break', and 'continue'. It emphasizes avoiding 'goto' due to its potential to make code difficult to read and debug, while providing examples of how to use the other control structures effectively. Additionally, it explains the differences between 'while' and 'do-while' loops, as well as the functionality of 'break' and 'continue' statements within loops.

Uploaded by

Dr. Salma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Control Statements

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

The do-while loop looks like this:


do
{
this ;
and this ;
and this ;
and this ;
} while ( this condition is true ) ;
There is a minor difference between the working of while and dowhile loops. This
difference is the place where the condition is tested. The while tests the condition before
executing any of the statements within the while loop. As against this, the do-while tests the
condition after having executed the statements within the loop. Figure 3.5 would clarify the
execution of do-while loop still further.
This means that do-while would execute its statements at least once, even if the condition
fails for the first time. The while, on the other hand will not execute its statements if the
condition fails for the first time. This difference is brought about more clearly by the
following program.
main( )
{
while ( 4 < 1 )
printf ( "Hello there \n") ;
}

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 ) ;
}
}

Let us now examine how the for statement gets executed:


− When the for statement is executed for the first time, the value of count is set to an initial
value 1.
− Now the condition count <= 3 is tested. Since count is 1 the condition is satisfied and the
body of the loop is executed for the first time.
− Upon reaching the closing brace of for, control is sent back to the for statement, where the
value of count gets incremented by 1.
− Again the test is performed to check whether the new value of count exceeds 3.
− If the value of count is still within the range 1 to 3, the statements within the braces of for
are executed again.
− The body of the for loop continues to get executed till count doesn’t exceed the final value
3.
− When count reaches the value 4 the control exits from the loop and is transferred to the
statement (if any) immediately after the body of for.

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.

The continue Statement


In some programming situations we want to take the control to the beginning of the loop,
bypassing the statements inside the loop, which have not yet been executed. The keyword
continue allows us to do this. When continue is encountered inside any loop, control
automatically passes to the beginning of the loop. A continue is usually associated with an if.
As an example, let's consider the following program.
main( )
{
int i, j ;
for ( i = 1 ; i <= 2 ; i++ )
{
for ( j = 1 ; j <= 2 ; j++ )
{
if ( i == j )
continue ;
printf ( "\n%d %d\n", i, j ) ;
}
}
}
The output of the above program would be...
12
21

You might also like