Behaviour of if and for loop without {}

W

Waxhead

Hi folks, First of all I'm new to newsgroups so please don't kill me in
case I'm breaking any rules , but if you really have to make it quick
and painfull ;)

What is actually propper hehaviour of Snippet 1 ? as I understand it
the result should be the same as snippet 2. (but it isn't at least with
my compiler).

I expect snippet 1 to work like this: if then for and function() else
just function() or perhaps a bit clearer:

if A is false C get's executed once
if A is true do B and execute C repeatedly while n<step

Snippet 2 is working as it should. but I tried to avoid having two
functioncalls by rewriting as in snippet 1 and now I'm confused... :/

/*** Snippet 1 ***/

for(int x=0; x<xwidth ; x++)
{
for(int y=0; y<ywidth ; y+=step)
{
if(0==(x%step)) /* A */
for(int n=0;n<step;n++) /* B */
SetPixel(hdc,x,y,gridcolor); /* C */
}
}

/*** Snippet 2 ***/

for(int x=0; x<xwidth ; x++)
{
for(int y=0; y<ywidth ; y+=step)
{
SetPixel(hdc,x,y,gridcolor);
if(0==(x%step)) /* D */
{
for(int n=0;n<step;n++) /* E */
SetPixel(hdc,x,y+n,gridcolor); /* F */
}
}
 
E

Eric Sosman

Waxhead said:
Hi folks, First of all I'm new to newsgroups so please don't kill me in
case I'm breaking any rules , but if you really have to make it quick
and painfull ;)

What is actually propper hehaviour of Snippet 1 ? as I understand it
the result should be the same as snippet 2. (but it isn't at least with
my compiler).

They should not be the same. The A,B,C of Snippet 1
is the same as D,E,F in Snippet 2, but 2 has a function call
(before the test) that 1 lacks.
I expect snippet 1 to work like this: if then for and function() else
just function() or perhaps a bit clearer:

if A is false C get's executed once

No, if A is false the "then" part is not executed
at all. B,C constitute one statement (namely, a "for"
statement that itself contains C as a sub-statement),
so the whole thing gets skipped.
if A is true do B and execute C repeatedly while n<step

Yes.
Snippet 2 is working as it should. but I tried to avoid having two
functioncalls by rewriting as in snippet 1 and now I'm confused... :/

/*** Snippet 1 ***/

for(int x=0; x<xwidth ; x++)
{
for(int y=0; y<ywidth ; y+=step)
{
if(0==(x%step)) /* A */
for(int n=0;n<step;n++) /* B */
SetPixel(hdc,x,y,gridcolor); /* C */
}
}

/*** Snippet 2 ***/

for(int x=0; x<xwidth ; x++)
{
for(int y=0; y<ywidth ; y+=step)
{
SetPixel(hdc,x,y,gridcolor);
if(0==(x%step)) /* D */
{
for(int n=0;n<step;n++) /* E */
SetPixel(hdc,x,y+n,gridcolor); /* F */
}
}

Assuming that calling SetPixel() twice with the same
argument values produces the same effect as calling it
just once ("When you press one of these black buttons that
are labeled in black on a black background, a little black
light lights up black to let you know you've done it"),
you could avoid writing two function calls by reorganizing
the loops a bit. Here's one way:

for(int x=0; x<xwidth; x++)
{
int s = (x % step == 0) ? 1 : step;
for (int y=0; y<ywidth; y+=s)
SetPixel(hdc,x,y,gridcolor);
}
 
V

Vladimir S. Oka

Waxhead said:
Hi folks, First of all I'm new to newsgroups so please don't kill me
in case I'm breaking any rules , but if you really have to make it
quick and painfull ;)

Your question, and the post, are quite fine, even for c.l.c -- which is
a welcome change... ;-)
What is actually propper hehaviour of Snippet 1 ? as I understand it
the result should be the same as snippet 2. (but it isn't at least
with my compiler).

Bear with me...
I expect snippet 1 to work like this: if then for and function() else
just function() or perhaps a bit clearer:

if A is false C get's executed once
if A is true do B and execute C repeatedly while n<step

Snippet 2 is working as it should. but I tried to avoid having two
functioncalls by rewriting as in snippet 1 and now I'm confused... :/

/*** Snippet 1 ***/

for(int x=0; x<xwidth ; x++)
{
for(int y=0; y<ywidth ; y+=step)
{
if(0==(x%step)) /* A */
for(int n=0;n<step;n++) /* B */
SetPixel(hdc,x,y,gridcolor); /* C */
}
}

What you're saying here is, essentially: if A is true, do this for loop
(B) for me, please. The C is what gets executed in the for loop.

If you look how `if` statement is defined, it's along the lines of:

if (<condition>)
<statement>
;

`statement` being /everything/ from the closing parenthesis to
the /first/ semicolon. If you choose to surround `statement` by braces,
you can have more semicolons inside them.

Similarly, `for` is defined as:

for (<for stuff>)
<statement>
;

and the same applies.

Indentation does not change this. ;-)

Personally, I'd recommend bracing everything. As you have seen, not
doing so can mislead (I had to take a good look to spot where all the
semicolons were.

<snipped Snippet 2>

Hope this helped.

Cheers

Vladimir

PS
I'm sure there are more PC ways of explaining `if` and `for`, but it's
late, and I'm sure someone will come along and do it better.

PPS
Carefully reading C reference or a textbook is also recommended.
 
I

Ian Collins

Waxhead said:
Hi folks, First of all I'm new to newsgroups so please don't kill me in
case I'm breaking any rules , but if you really have to make it quick
and painfull ;)

What is actually propper hehaviour of Snippet 1 ? as I understand it
the result should be the same as snippet 2. (but it isn't at least with
my compiler).

I expect snippet 1 to work like this: if then for and function() else
just function() or perhaps a bit clearer:

if A is false C get's executed once

No, if A is false nothing happens.

The for loop is a single statement, up to the ; Don't be fooled by the
indentation, you have written:

if(0==(x%step)) /* A */
{
for(int n=0;n<step;n++) /* B */
{
SetPixel(hdc,x,y,gridcolor); /* C */
}
}
 
W

Waxhead

Waxhead said:
Hi folks, First of all I'm new to newsgroups so please don't kill me in
case I'm breaking any rules , but if you really have to make it quick
and painfull ;)

What is actually propper hehaviour of Snippet 1 ? as I understand it
the result should be the same as snippet 2. (but it isn't at least with
my compiler).

I expect snippet 1 to work like this: if then for and function() else
just function() or perhaps a bit clearer:

if A is false C get's executed once
if A is true do B and execute C repeatedly while n<step

Snippet 2 is working as it should. but I tried to avoid having two
functioncalls by rewriting as in snippet 1 and now I'm confused... :/

/*** Snippet 1 ***/

for(int x=0; x<xwidth ; x++)
{
for(int y=0; y<ywidth ; y+=step)
{
if(0==(x%step)) /* A */
for(int n=0;n<step;n++) /* B */
SetPixel(hdc,x,y,gridcolor); /* C */
}
}

/*** Snippet 2 ***/

for(int x=0; x<xwidth ; x++)
{
for(int y=0; y<ywidth ; y+=step)
{
SetPixel(hdc,x,y,gridcolor);
if(0==(x%step)) /* D */
{
for(int n=0;n<step;n++) /* E */
SetPixel(hdc,x,y+n,gridcolor); /* F */
}
}

Thanks everyone helping me understand that C is part of B ;) I was
thinking that since B is part of A the C would not be a part of B
unless A is true. =)
 

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,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top