Difference between for n while

R

rohitjogya

Can anyone tell me the difference bet for loop and while loop
execution?
____________________
for (i=0 ; i<10 ; i++)
; /* Do nothing*/

print i;

___________________
i=0;
while (i<10) {
i++;
}
print i;
____________________

wat will be the output?
please expain ur ans
 
A

Anthony Irwin

Can anyone tell me the difference bet for loop and while loop
execution?
____________________
for (i=0 ; i<10 ; i++)
; /* Do nothing*/

print i;

___________________
i=0;
while (i<10) {
i++;
}
print i;
____________________

wat will be the output?
please expain ur ans

Hi,

You code won't work in c.

Try the following instead.

#include <stdio.h>

int main(void) {
int i=0, ii = 0;
while (i<10) {
i++;
}
printf("%d\n", i);

for (ii=0 ; ii <10 ; ii++)
; /* Do nothing*/

printf("%d\n", ii);
return 0;
}

The answer to the above is 10 for both loops.

The reason is that it reaches 9 and then increments in the loop and
when it comes back around it is 10 and then the loop stops because it
is not less then 10.

Kind Regards,
Anthony Irwin
 
C

CBFalconer

.... snip ...

wat will be the output?
please expain ur ans

Ankor Wat is overgrown and abandoned in the jungles of Cambodia. I
believe the actual location of Ur is not accurately known, and that
it is also buried in the sands of Mesopotamia. The pain comes from
not taking the exlax. It is not connected with Ur.

Your shift key is apparently broken.
 
A

Anthony Irwin

Actually this program will probably show you more.

#include <stdio.h>

main() {
int i=0, ii = 0;
while (i<10) {
printf("Inside Loop: %d\n", i);
i++;
}

printf("Outside Loop: %d\n", i);

for (ii=0 ; ii <10 ; ii++)
printf("Inside Loop: %d\n", ii);

printf("Outside Loop: %d\n", ii);
return 0;
}

Output below:

Inside Loop: 0
Inside Loop: 1
Inside Loop: 2
Inside Loop: 3
Inside Loop: 4
Inside Loop: 5
Inside Loop: 6
Inside Loop: 7
Inside Loop: 8
Inside Loop: 9
Outside Loop: 10
Inside Loop: 0
Inside Loop: 1
Inside Loop: 2
Inside Loop: 3
Inside Loop: 4
Inside Loop: 5
Inside Loop: 6
Inside Loop: 7
Inside Loop: 8
Inside Loop: 9
Outside Loop: 10

See how the loop never reaches 10 on the inside only outside the loop.
The loop never executes when it is 10.

the ii++ increments after the loop has complete in the for loop.

Just play with some code and compile it any you will learn a fair bit.

Kind Regards,
Anthony Irwin
 
R

rohitjogya

If there is no any diff bet for and while loop, den why some people
say that
use while loop while handling pointers like in LinkedList programs?
 
R

Richard Heathfield

(e-mail address removed) said:
If there is no any diff bet for and while loop, den why some people
say that
use while loop while handling pointers like in LinkedList programs?

I y c b b t w E p a f a y c, w s w b b t r w y w ?
 
J

J. J. Farrell

If there is no any diff bet for and while loop, den why some people
say that
use while loop while handling pointers like in LinkedList programs?

Please try to write in English. You'll need to ask the people who say
that.
 
A

Army1987

Can anyone tell me the difference bet for loop and while loop
execution?
____________________
for (i=0 ; i<10 ; i++)
; /* Do nothing*/

print i;

___________________
i=0;
while (i<10) {
i++;
}
print i;

In C89, the only relevant differences between

expression_0;
while (condition) {
expression_1;
expression_2;
expression_3;
expression_n;
}

and:

for (expression_0; condition; expression_n) {
expression_1;
expression_2;
expression_3;
}

are:

1. In the first case, condition cannot be empty, in the second case it can
and if it is it is considered to be 1 (always true).
2. If a continue statement is used in the for loop, expression_n is evalued
before checking condition and resuming the loop from expression_1 if it is
true.

In C99 expression_0 can be a variable declaration, and the variable so
declared is only visible within the loop body. There are also other
differences, but unless you want to do pointless things such as re-defining
an enum in the loop body, they won't show up.

(Personally, I consider while (condition) a shorthand for for (;condition;)
rather than vice versa...)
 
R

rohitjogya

Exactly....
I was look for this information only...
For and while loop will produce diff o/ps in case of continue
statement !!
 
G

Gunvant Patil

If there is no any diff bet for and while loop, den why some people
say that
use while loop while handling pointers like in LinkedList programs?

I would say For is better choice than While if you are dealing with
LinkedList.

for (temp = head; temp->nxt != NULL; temp = temp->nxt) {
/* Do something */
}

temp = head;
while (temp->nxt) {
/* Do something */
temp = temp->nxt;
}

decide your self which is more readable and less prone to errors.

-Cheers,
Gunvant
~~~~~~~~
No trees were killed in the sending of this message. However a large
number of electrons were terribly inconvenienced.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top