I would like to know the diffrence among loops (for,do while & while) used for flow control & sample usage & selection of loop based on the application.....as a newbie

Also why integer is limited to -32767 TO +32767?

Thanks & regards
Lakshmi

Recommended Answers

All 9 Replies

Member Avatar for iamthwee

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.

Also why integer is limited to -32767 TO +32767?

Size of int is implementation dependent.

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

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.

>Use the limits in limits.h if you want portability.
Smartass. :rolleyes:

>Use the limits in limits.h if you want portability.
Smartass. :rolleyes:

Ha! One for me, one million for you.:)

Also why integer is limited to -32767 TO +32767?
>> integer value is 16 bit in size which yields a maximum of 65,536 ( from 2^16) combinations.... and it has different attributes (such as signed(default) , unsigned, short long)..
these attribute may vary the range of the integer limitation...
as default signed/short(-32767 TO +32767)
unsigned/long (0 to 65,535)

in memory scope bit structure signed(-32767) is just equal to unsigned(65535) where in the most significant bit of that int data type is used as a flag.. which determines if the integer value would be positive(0) or negative (1)

bit scope:
signed(-32767) = 11111111 11111111
unsigned(655535) = 11111111 11111111

signed(-1) = 10000000 00000001
unsigned(32769) = 10000000 00000001

they are they same... in bit structure...
so they will be equal when casted into same attribute of same data type....
^__^ ....

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.