Re: for loops without braces?

  • From: Kerneels Roos <kerneels@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Fri, 19 Nov 2010 18:09:40 +0200

Hi,
On this topic, it's a good thing to always remember that for loops have 4 as in four parts like this:

for (init, check, update)
{
    body
}

Then the execution cycle is always:

init
check
body
update
check
body
update
check
body
update
....
check -- check fails and loop exits.

As was pointed out, the braces are not mandatory and the body part could be one statement.

for (int i=0;  i < 5; i++) speak(i);

A general rule of thum, and correct me guys if I'm wrong here, for C style languages is that a statement is terminated by a ; and could be a very long and involved piece of code. Alternatively use braces to mark out a code block of multiple statements in which case the final closing brace of course doesn't need any ;.

Kerneels

On 11/19/2010 4:00 PM, Alex Midence wrote:
get MinGw and you'll have a nice c++ compiler for these sorts of things:
http://www.mingw.org/
Once you install it, add it to the path so gcc and g++ will work from
anywhere.  Not having a compiler while studying c or c++ won't get you
very far.

Alex M

On 11/18/10, Alex Hall<mehgcap@xxxxxxxxx>  wrote:
Okay, thanks. Hopefully this will clear up Prim's Algorithm some. I
would run it, but I do not have a C compiler; I use Python, and I
think I have Java hanging around somewhere, but Python is my primary
weapon of choice.

On 11/18/10, Littlefield, Tyler<tyler@xxxxxxxxxxxxx>  wrote:
the print right under the for will be executed. As I said, it executes
the line right under it and no more. Whether that's an if statement
opening a new block or whatever. After the loop exits (after it prints
0-9, the rest of your code will be executed. Try running it to see.
On 11/18/2010 8:19 PM, Dave wrote:
or would only the first statement after the for, the print(i-1), be
executed and then the loop exited?

This is what would happen.  Remember that only the statement/block
after the loop would be parsed as the body of the loop.  Without the
braces, you'd only get the first print statement.

On 11/18/10, Alex Hall<mehgcap@xxxxxxxxx>   wrote:
Thanks for the quick responses. So, would the following be correct?

for(i=0;i<10;i++)
print(i-1);
print(i+1);
if(i%2==0){
print(i);
}

or would only the first statement after the for, the print(i-1), be
executed and then the loop exited?

On 11/18/10, qubit<lauraeaves@xxxxxxxxx>   wrote:
easily -- when the compiler reaches the end of the statement following
the
)
of the for loop header.
Here the statement is the if statement, which could be huge and even
contain
an else clause, but according to C++ syntax, the compiler has to parse
the
complete statement before closing the for loop.
--le
----- Original Message -----
From: "Alex Hall"<mehgcap@xxxxxxxxx>
To:<programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, November 18, 2010 8:09 PM
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


--
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



--

Thanks,
Ty

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



--
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


--
Kerneels Roos
Cell: +27 (0)82 309 1998
Skype: cornelis.roos

"If one has the talent it pushes for utterance and torments one; it will out; and 
then one is out with it without questioning. And, look you, there is nothing in this 
thing of learning out of books. Here, here and here (pointing to his ear, his head and 
his heart) is your school. If everything is right there, then take your pen and down with 
it; afterward ask the opinion of a man who knows his business."

(To a musically talented boy who asked Mozart how one might learn to compose.)

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

Other related posts: