LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with 'while' loop operation ..... (https://www.linuxquestions.org/questions/programming-9/help-with-while-loop-operation-29580/)

purpleburple 09-04-2002 10:39 AM

Help with 'while' loop operation .....
 
Hi. I am wondering the steps a while loop takes. I know it basically but in the following >

#include <stdio.h>

int main(void)
{
int array[] = { 1, 2, 3, 4, 5 };
int i = 0;


while(array[i++] != '\0')
printf("array[%d] contains %d\n", i, array[i]);

return 0;

}

my output was ....

array[1] contains 2
array[2] contains 3
array[3] contains 4
array[4] contains 5
array[5] contains -1073743052
array[6] contains -1073743144
array[7] contains 134513729
array[8] contains 1073834212
array[9] contains -1073743052
array[10] contains -1073743096
array[11] contains 1074038023
array[12] contains 1
array[13] contains -1073743052
array[14] contains -1073743044
array[15] contains 134513402
array[16] contains 134513952
array[17] contains 0

So in the 'while' loop I am guessing the order of operation is
first 'array[0] is 'evaluated' and then if it checks out TRUE it is 'augmented'
by one and THEN the 'printf' is executed. Am I right?

Which would also be the case with the 'i'.
So I came to conclusion or 'remembered' that the augmentation should take place
in the printf statement or somewhere within the 'while' loop braces. Right?

And bringing me to my second question ..
What does an int array terminate with? It obviously wasn't '\0'. Or did I do something wrong?

thanks alot :)

acid_kewpie 09-04-2002 10:51 AM

1... no, that's total gibberish. array's start at 0 and not 1, everything else should fall into place then.

2... it doesn't terminate. once you get into pointers and all that junk, you'll see that technically an array is open ended, and you just get given the contents of the next memory space if the array has not been defined there. you should use a sizeof() operation really to ensure you know how long the array really is instead. see it's manpage for details

purpleburple 09-04-2002 10:57 AM

thanks acid I know arrays start at array[0] but looking at my output (totally skipping the printing of array[0]'s
contents to standard out) I would assume that while loop order is

It checks to see if array[0] does not equal '\0' (which it would never in this example) and THEN it augmets the 'i' in the array[i++] and after all that it prints array[1] = instead of array[0] = beacuse it would have augmented the i++ in the check part before moving on to the printf statement.

Am I correct?

thanks :)

acid_kewpie 09-04-2002 11:02 AM

erm yeah i guess. quite you want to delibertaly ignore the first value is pretty much beyond me, but nevermind...

no2nt 09-04-2002 11:02 AM

for integer arrays, you'll need to manually terminate them.

The first time through the loop array[0] is evaluated but
after that evaluation i is incremented to 1. So in order to
get the correct array values with the printf stmt, use i-1.

Code:

#include <stdio.h>

int main(void)
{
int array[] = { 1, 2, 3, 4, 5, -1 };
int i = 0;


while(array[i++] != -1)
printf("array[%d] contains %d\n", i-1, array[i-1]);

return 0;

}

Side note (using the example purple posted):
Be very careful using sizeof(array) in this situation. sizeof(array) will return
20 because there are 5 integers that are 4 bytes each. 5 (ints) * 4 (bytes) = 20. So (sizeof(array) / sizeof(int)) would work.

acid_kewpie 09-04-2002 11:04 AM

ahh don't give the sizeof problem away...! makes it more fun to find out!

no2nt 09-04-2002 11:07 AM

rofl. you don't know how much time i spent in highschool programming class tryin to figgur that out!

purpleburple 09-04-2002 11:12 AM

thanks alot guys .... but I think the simpler way to work thru this loop would have been to do this >

int array[] = { 1, 2, 3, 4, 5, -1 };
int i = 0;

while( array[i] != -1)
printf(array[%d] contains %d\n", i, array[i++]);

return 0;

}

or just put the augment of 'i' somewhere inside the 'while' loop instead of in the
printf statement.

So I guess another question is ..... if I have an array that holds say 100 'ints' and I ask
the user to input as many 'int's' as he or she wants and then I want to print 'only' those
array contents to the screen and NOT the entire 100 array elements I use sizeof() or something
similiar?

thanks alot guys :)

no2nt 09-04-2002 11:18 AM

100 'ints' question:
using sizeof will return the length of the whole array. it will not
give you the length of the values the user inputted. You'll
want to terminate your array with a special value in order to
only print out the ints the user inputted.

side note:
once again :-) be careful about putting using the pre/post
increment operators or nested assignment stmts inside functions.
Code:

printf("%d %d\n", ++n, power(2, n));
produces different results on different machines.
Best to do
Code:

++n;
printf("%d %d\n", n, power(2, n));

Moral of the story: don't write code which depends on the order of evaluation.

acid_kewpie 09-04-2002 11:21 AM

well at that point you should get into the concepts of arrays and pointers. as arrays are of a fixed length (goign by the book at least) you should always really require them to be filled or whatever. if in your example the user can giev as many or as little values as they want then an array is a poor choice of data structure to use. You can resize array's pretty easily, but it's done by treating them as pointers, which is all they really are. BUT if that's what you're doing then you should just use it as a pointer from step 1...

e.g.
Code:

array = (int *) realloc (array, array_length * sizeof (int));

acid_kewpie 09-04-2002 11:23 AM

oh and your idea of using a -1 at the end of an array is generally pretty poor, there are proper ways to do these things, cludgey solutions aren't needed

purpleburple 09-04-2002 11:36 AM

thanks alot guys .... you've been a tremendous help :)

As far as augmenting 'i' and WHEN it's done I did the following to find out >

#include <stdio.h>

int main(void)
{

int array[3] = { 5, 2, 3 };
int i = 0;


printf("array[%d] contains %d and i eqauls %d\n", i++, array[i], i);
printf("After leaving printf i eqauls %d\n", i);

return 0;
}

and the ouput was >

array[0] contains 5 and i eqauls 0
After leaving printf i eqauls 1


Now .... the ouput was not what I expected. I expected

array[0] contains 5 and i equals 1
After leaving printf i eqauls 1

The reason I expected the above was I thought that 'i' would be augmented and THEN the last 'i'
in the printf statement would equal 1 but it looks as though 'i' is not 'augmented' until 'after' it leaves
printf. Am I correct?

thanks and Im done for today ... :)

acid_kewpie 09-04-2002 11:44 AM

no because i++ is POST increment not pre,so one is added to it AFTER it's value has been given to the code. to get what you expected you would use ++i instead

no2nt 09-04-2002 12:11 PM

As far as using -1 as a terminator in an array of ints I don't see how that
is cludgey at all (unless -1 must also be used).
Look at "strings" in C. It's an array of characters terminated by '\0' and
there exist standard functions for dealing with "strings".
If I haven't completely lost it, if you use
Code:

array = (int *) realloc (array, array_length * sizeof (int));
in order to get the (alloc'ed) size of the array, you'll need to store
array_length * sizeof(int) somewhere.

Granted, having the length of an array (char or int) already calculated is
very nice and great for efficiency, C just doesn't do it that way.

One could do the following (ah, the Pascal way!):
Code:

struct
{
    int length;
    int *array;
};

This is not intended to be a flame!


All times are GMT -5. The time now is 04:38 AM.