The Odd Loop in C

Uses of Odd Loop

In real life programming one comes across a situation when it is not known beforehand how many times the statements in the loop are to be executed. This situation can be programmed as shown below:

/* Execution of a loop an unknown number of times */
main( )
{
char another ;
int num ;
do
{
printf ( “Enter a number ” ) ;
scanf ( “%d”, &num ) ;
printf ( “square of %d is %d”, num, num * num ) ;
printf ( “\nWant to enter another number y/n ” ) ;
scanf ( ” %c”, &another ) ;
} while ( another == ‘y’ ) ;
}
And here is the sample output…
Enter a number 5
square of 5 is 25
Want to enter another number y/n y
Enter a number 7
square of 7 is 49
Want to enter another number y/n n

This program the do-while loop would keep getting executed till the user continues to answer y. The moment he answers n, the loop terminates, since the condition ( another == ‘y’ ) fails. Note that this loop ensures that statements within it are executed at least once even if n is supplied first time itself.

The same functionality if required, can also be accomplished using for and while loops as shown below:

/* odd loop using a for loop */
main( )
{
char another = ‘y’ ;
int num ;
for ( ; another == ‘y’ ; )
{
printf ( “Enter a number ” ) ;
scanf ( “%d”, &num ) ;
printf ( “square of %d is %d”, num, num * num ) ;
printf ( “\nWant to enter another number y/n ” ) ;
scanf ( ” %c”, &another ) ;
}
}

/* odd loop using a while loop */
main( )
{
char another = ‘y’ ;
int num ;
while ( another == ‘y’ )
{
printf ( “Enter a number ” ) ;
scanf ( “%d”, &num ) ;
printf ( “square of %d is %d”, num, num * num ) ;
printf ( “\nWant to enter another number y/n ” ) ;
scanf ( ” %c”, &another ) ;
}
}

 

Related Posts

  • 70
    For loop is probably the most popular looping instruction. The for allows us to specify three things about a loop in a single line: Setting a loop counter to an initial value. Testing the loop counter to determine whether its value has reached the number of repetitions desired. Increasing the value of…
    Tags: loop, learn, programming
  • 57
    The way if statements can be nested, similarly whiles and for can also be nested. To understand how nested loops work, look at the program given below: /* Example of nested loops */ main( ) { int r, c, sum ; for ( r = 1 ; r <= 3 ;…
    Tags: loop, learn, programming, tutorial
  • 53
    The programs that we have learned/developed in this tutorial so far used either a sequential or a decision control instruction. In the first one, the calculations were carried out in a fixed order, while in the second, an appropriate set of instructions were executed depending upon the outcome of the condition…
    Tags: loop, number, tutorial, programming, learn
  • 51
    The if statement can take any of the following forms: if ( condition ) do this ; if ( condition ) { do this ; and this ; } if ( condition ) do this ; else do this ; if ( condition ) { do this ; and this…
    Tags: learn, programming, tutorial
  • 50
    Let's have an example to understand else if clause : /* else if ladder demo */ main( ) { int m1, m2, m3, m4, m5, per ; per = ( m1+ m2 + m3 + m4+ m5 ) / per ; if ( per >= 60 ) printf ( "First…
    Tags: printf, learn, programming, tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: