Re: for loops without braces?

  • From: "qubit" <lauraeaves@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 19 Nov 2010 11:16:39 -0600

actual syntax:

"for ( init_statement condition_expression; increment_expression ) statement

Now it isn't a typo that i neglected the semicolon after init_statement, 
since this statement ends in a semicolon by definition.
Same for the statement that is the body of the for loop.

Now also note that statement can be "{ statement_list }".
The point is that there is nothing that says there has to be braces around 
the body. This is likewise true of while, do, sif and even switch 
statements, although it would be a very simplistic switch statement without 
a block of statements rather than just one statement.

HTH
--le


----- Original Message ----- 
From: "Sina Bahram" <sbahram@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, November 18, 2010 11:45 PM
Subject: RE: for loops without braces?


This is very simple.


For(blah)
If(blah)
{
Do blah
}

Simplifies to

For(blah)
If statement doing blah

You see?

The for loop only repeats one item, ever. It's just that, most of the time, 
that one item is a block noted by an open and closed
brace, but otherwise it can be any other one item, like another for loop, an 
if statement, a single statement, or so on.

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex Hall
Sent: Thursday, November 18, 2010 9:09 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: for loops without braces?

Hi all,
I am desperately trying to understand Prim's algorithm. I have found it in 
pseudocode and C code, but in both examples they use for
loops without braces, so it might be:
for(i=0; i<4; i++)
if(i%2==0){
print(i);
}

Obviously this is understandable, but when you mix it into other code it 
becomes much harder to tell what is going on. First, how
can one do this syntactically and have it be correct? Second, what is the 
rule to figure out where the loop ends if it is not in
braces? The C compiler must have a way...

--
Have a great day,
Alex (msg sent from GMail website)
mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap __________ View the 
list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: