Important Announcement
Forum has been made read-only. Please click here for more information or here to return to The Spriters Resource.

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:
[spoiler]
Given the following pseudo-code;
Code:
for(DEF; COND; INCR) {
CODE
}
This could be rewritten as: (there are probably some obscure cases where this will not work, but it should suffice for now)
Code:
{
  DEF;
  while (COND) {
    CODE;
    INCR;
  }
}
In other words;
- Before anything else, the DEF-code is executed. This will usually be definition or assignment of some counter.
- The COND (condition) is checked.
- If COND evaluates to true, the CODE is executed, after which INCR is executed. INCR usually increments the counter defined in DEF. After this, the COND is checked again.
- Once the COND evaluates to false, the loop stops.
(- if a variable was defined in DEF, it will not be visible anywhere else. hence the surrounding brackets)

With example code:
Code:
for(int i = 0; i < 3; i++) {
  System.out.print(i + " ");
}
System.out.println(".");
[spoiler=step-by-step evaluation scheme]
DEF Wrote:- define variable i
- initialize i to 0

COND Wrote:- evaluate i < 3.
- 0 is less than 3 -> 'true'
CODE Wrote:- print i (which is 0) followed by a whitespace
INCR Wrote:- increment i by 1.

COND Wrote:- evaluate i < 3.
- 1 is less than 3 -> 'true'
CODE Wrote:- print i (which is 1) followed by a whitespace
INCR Wrote:- increment i by 1.

COND Wrote:- evaluate i < 3.
- 2 is less than 3 -> 'true'
CODE Wrote:- print i (which is 2) followed by a whitespace
INCR Wrote:- increment i by 1.

COND Wrote:- evaluate i < 3
- 3 is not less than 3 -> 'false'
The loop ends, and a '.' and newline-character is printed.
[/spoiler]
[/spoiler]


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


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:


Users browsing this thread: 1 Guest(s)