There's not much difference, all three can be written to imitiate the other.
The for loop is often used in cases where you want to repeat a section of a program for a fixed amount of times. You have an initialisation value, and end value and an increment.
for( int i=0; i <1000; i++)
{
//do stuff
}
The while loops does not have many different sections to set up as the for loop. The only thing it needs is a conditional test, which accompanies the while statement.
while (condition)
{
//do stuff
}
The do-while loop is similar in function to the while loop, but the conditional test goes in a different place.
do
{
//do stuff
} while (condition);
A way to think about the difference between the while and do-while loop is like this:-
Pretend you're a teenager who wants to borrow your daddy's car. There's two options to take:
1. Borrow the car first and tell daddy later.
2. Ask daddy before you borrow the car.
do-while is like option 1.
while is like option 2.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>Size of int is implementation dependent.
But for portability reasons, you can't assume more than -32,767 to +32,767. The standard requires an int to be at least 16 bits. It can be more, but never less, so it's safe to assume 16 bits and non-portable to assume more.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
But for portability reasons, you can't assume more than -32,767 to +32,767.
Use the limits in limits.h if you want portability.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>Use the limits in limits.h if you want portability.
Smartass. :rolleyes:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>Use the limits in limits.h if you want portability.
Smartass. :rolleyes:
Ha! One for me, one million for you.:)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>>Also why integer is limited to -32767 TO +32767?
The standards do not specifiy the size of an integer, and it depends on the compiler you are using. You should look at limits.h to find out what you compiler supports.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343