Re: for loops without braces?

  • From: "Littlefield, Tyler" <tyler@xxxxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Thu, 18 Nov 2010 19:13:51 -0700

The for loop will execute the next line below it, if there are no braces.
so:
for (i=0;i<100;i++)
if (i==5)
printf("yay!");
works just as well without the braces. If I were to add another if statement or something I'd need braces in the for loop.
On 11/18/2010 7:09 PM, Alex Hall wrote:
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...



--

Thanks,
Ty

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

Other related posts: