954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Difference among loops

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

lchamarthi
Newbie Poster
4 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

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
 
Also why integer is limited to -32767 TO +32767?

Size of int is implementation dependent.

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

>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
Administrator
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
Team Colleague
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
Administrator
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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

ivanCeras
Newbie Poster
20 posts since May 2006
Reputation Points: 10
Solved Threads: 1
 
bit scope: signed(-32767) = 11111111 11111111 unsigned(655535) = 11111111 11111111

The C languages support one's complement, two's complement, and sign-and-magnitude representations. http://en.wikipedia.org/wiki/Signed_number_representations

Dave Sinkula
long time no c
Team Colleague
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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You