one simple question

A

ash

i have got this question in old question papers and i know the answer
but didn`t understand how it is possible.

main( )
{
int x=0;
while(x<=50)
for( ; ;)
if( ++x % 50 ==0)
break;
printf("x = %d", x);
}

the answer is 100.
if someone will explain, i will be thankful.
 
R

Richard Heathfield

ash said:
i have got this question in old question papers and i know the answer
but didn`t understand how it is possible.

main( )
{
int x=0;
while(x<=50)
for( ; ;)
if( ++x % 50 ==0)
break;
printf("x = %d", x);
}

the answer is 100.
if someone will explain, i will be thankful.

Firstly, the program behaviour is undefined because you are calling a
variadic function without a valid prototype in scope. Fix that with:

#include <stdio.h>

Okay, let's write the code with some labelling:

#include <stdio.h>

int main(void)
{
int x=0;

while(x <= 50) /* A */
{
for( ; ; ) /* B */
{
if( ++x % 50 == 0) /* C */
{
break; /* D */
}
}
}
printf("x = %d", x); /* E */

return 0;
}

x starts off at 0. It passes the first test (A) easily. Then it enters the
for loop (B). On line (C), the value of x is incremented, and then it's
divided by 50 and the remainder compared to 0. Clearly this won't "fire"
until x has the value 50. At this point, we get to (D), which breaks out of
loop (B), and goes around to (A) again. Is x <= 50? Why yes, it is, so we
go back into the loop again, and hit (B), which will take us around for
another 50 iterations. Then x will get the value 100. Since 100 % 50 is 0,
the (B) loop will be broken. That takes us back to (A). Since 100 is not <=
50, the (A) loop terminates, and we proceed to (E).

HTH. HAND.
 
V

Vladimir Oka

Richard said:
ash said:


Firstly, the program behaviour is undefined because you are calling a
variadic function without a valid prototype in scope. Fix that with:

#include <stdio.h>

Okay, let's write the code with some labelling:

#include <stdio.h>

int main(void)
{
int x=0;

while(x <= 50) /* A */
{
for( ; ; ) /* B */
{
if( ++x % 50 == 0) /* C */
{
break; /* D */
}
}
}
printf("x = %d", x); /* E */

return 0;
}

x starts off at 0. It passes the first test (A) easily. Then it enters the
for loop (B). On line (C), the value of x is incremented, and then it's
divided by 50 and the remainder compared to 0. Clearly this won't "fire"
until x has the value 50. At this point, we get to (D), which breaks out of
loop (B), and goes around to (A) again. Is x <= 50? Why yes, it is, so we
go back into the loop again, and hit (B), which will take us around for
another 50 iterations. Then x will get the value 100. Since 100 % 50 is 0,
the (B) loop will be broken. That takes us back to (A). Since 100 is not <=
50, the (A) loop terminates, and we proceed to (E).

HTH. HAND.

You forgot one:

QED

;-)
 
A

ash

thankx richard, a little point left what is meaning of - for ( ; ;)
how long this loop executed.Is this some kind of infinite loop?
 
V

Vladimir Oka

ash said:
thankx richard, a little point left what is meaning of - for ( ; ;)
how long this loop executed.Is this some kind of infinite loop?

Indeed it is. What gets you out of it is the `break;` statement.
 
A

ash

thankx richard, a little point left what is meaning of - for ( ; ;)
how long this loop executed.Is this some kind of infinite loop?
 
E

Eric Sosman

ash said:
i have got this question in old question papers and i know the answer
but didn`t understand how it is possible.

main( )
{
int x=0;
while(x<=50)
for( ; ;)
if( ++x % 50 ==0)
break;
printf("x = %d", x);
}

the answer is 100.
if someone will explain, i will be thankful.

Perhaps the easiest way to see what's going on in a
case like this is to "play computer:" take pencil and
paper, write down the initial values of all the variables
that have them, and then follow the code line by line as
if you were the computer. Evaluate each expression or
test by consulting the variable values written on the paper,
and whenever a value changes scratch out the old and write
the new. Your mimicry would begin like this:

Set x to zero.

Is 0 <= 50? Yes, so the loop executes.

Change x from 0 to 1. Compute 1 % 50. Is that value
equal to zero? No, so the loop continues.

Change x from 1 to 2. Compute 2 % 50. Is that value
equal to zero? No, so the loop continues.

(At about this juncture you'll probably realize that
the inner loop will just keep executing until it has
advanced `x' to a multiple of fifty, so you'll just
write `x = 50' on your paper and forego simulating
all the rest of the loop. This kind of shortcut is
an important step in learning how to understand a
program: You are learning to replace rote simulation
with reasoning.)

Back to the outer loop, is 50 <= 50? Yes, so the
outer loop executes again.

...

After you have done this kind of low-level simulation
enough times with enough pieces of code, you will find that
you develop an ability to do without it. The shortcuts you
discover allow you to take a higher-level view of the code
than the one the computer itself takes; you begin to reason
about the code instead of following it without understanding.

On the other hand, you may also find that the higher-
level viewpoint is more likely to be decieved by constructs
that aren't quite what they seem: a `defaut' or `case1:' label
in a `switch' statement, a misleading indentation

if (a < 0)
printf ("Correcting a from %d to 0\n", a);
a = 0;

.... and so on. An eye for detail remains important in this
business.

double x[10][10];
int i, j;
/* Initialize x as an identity matrix: */
for (i = 0; i < 10; ++i) {
for (j = 0; j < 10; ++i)
x[j] = (i == j);
}
 
R

Richard Heathfield

Vladimir Oka said:
Indeed it is. What gets you out of it is the `break;` statement.

At which point it is no longer infinite. I prefer to think of it as a lie
(unless it really does run "infinitely", which for our purposes might
reasonably be thought of as "right up until the machine's power dies").
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Richard said:
ash said:




Firstly, the program behaviour is undefined because you are calling a
variadic function without a valid prototype in scope. Fix that with:

#include <stdio.h>

Okay, let's write the code with some labelling:

#include <stdio.h>

int main(void)
{
int x=0;

while(x <= 50) /* A */
{
for( ; ; ) /* B */

Good thing you inserted a space between ; and )... My news-reader showed
a smiley instead of ';' + ')'.


Best regards / Med venlig hilsen
Martin Jørgensen
 
J

Jaspreet

ash said:
thankx richard, a little point left what is meaning of - for ( ; ;)
how long this loop executed.Is this some kind of infinite loop?

Please use the Reply button at top of the message (shown by clicking on
Show Options) instead of the broken Reply button at the bottom of the
message.

for (; ;)

Lets us go through the syntax of for :

for (initialisation ; condition ; increment / decrement)

Either of the three are optional. If you leave out the condition part
that means you are not sure for how long would you like to execute the
loop. In most probable case, you would have a break inside the for-loop
to terminate the loop. That is what is happening in your case too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top