Users browsing this thread: 1 Guest(s)
Some programming help?
#4
(06-21-2011, 04:39 PM)Koopaul Wrote: Thanks for posting it this way! It's better if I figure it out myself.

But I'm going to use your advice,

So let's see:

int i is to the length of the num array which would be
0
1
2
3
4
5
6
7
8 right?
Theoretically correct, however these are not the values that you will see when you would print the value of i inside the for-loop. The contents of the loop are only executed while the given condition holds, which is i < num.length. The length of the array is 8.
(see below for an example of how a for-loop works)

Quote:int j is equal to int i plus 1 which would be
1
2
3
4
5
6
7
8
9
Since j is again defined and used in a for-loop, it will have a range of values for every i. j does indeed start at i+1, but it will again end when the condition evaluates to false.

Quote:num[i] is less than num[j] which means t equals num[i] which is equal to num[j]?
Keep in mind that the lines are executed sequentially. First the value of t is set to the current value of num[i]. Only after that operation has been completed will the other lines be executed.

Quote:21
52
13
74
45
86
37
98
69
The only additions in the program are on variables i and j. These two variables are only used as indices to the array, and will thus never be added to the values in the array. With the example code, the values of the array will never contain any number that is not one of the set {20,50,10,70,40,80,30,90,60}.


Just in case, here's how a for-loop structure is evaluated:

(underlined words were supposed to be italics, but using num[i] at the same time doesn't work)
Thanked by: Koopaul


Messages In This Thread
Some programming help? - by Koopaul - 06-20-2011, 10:46 PM
RE: Some programming help? - by Barubary - 06-21-2011, 03:56 AM
RE: Some programming help? - by Koopaul - 06-21-2011, 04:39 PM
RE: Some programming help? - by Barubary - 06-22-2011, 04:25 AM
RE: Some programming help? - by Work Polo Shirts - 07-15-2011, 10:25 PM

Forum Jump: