You are Here: Home >< Maths

# D1 simple algorithm - can someone explain it to me please? watch

1. I've got a couple of questions like this. I think that it's upposed to be easy but there's a part to it that i just don't get. Here it is:
X=0, K=0
FOR I = 1 TO 6
FOR J = 1 TO 5
X = I x J
K = K + 1
PRINT K, I, "times", J, "equals", X
NEXT J
NEXT I
END

What is FOR I = 1 TO 6 and FOR J = 1 TO 5 all about? How do i know what I and J are? Will the rest make sense when i figure this out?
Thanks for any help.
2. The "FOR I = 1 TO 6" bit means you only apply the algorithm if I takes a value between 1 and 6 inclusive.

The algorithm's not well-written, but I think you should just start with I and J equal to 1, since this is logical. The algorithm should then output the squares of 1, 2, 3, 4 and 5. I don't think it'll output the square of 6 because the algorithm doesn't run when J > 5.
3. thanks, i thought it was going to be something really complicated.
4. (Original post by coral_fangs)
I've got a couple of questions like this. I think that it's upposed to be easy but there's a part to it that i just don't get. Here it is:
X=0, K=0
FOR I = 1 TO 6
FOR J = 1 TO 5
X = I x J
K = K + 1
PRINT K, I, "times", J, "equals", X
NEXT J
NEXT I
END

What is FOR I = 1 TO 6 and FOR J = 1 TO 5 all about? How do i know what I and J are? Will the rest make sense when i figure this out?
Thanks for any help.
This code fraction simply outputs some partial multiplication tables.
You have an outer loop and an inner loop.
The inner loop variable varies from 1 to 5 for each value taken by the outer loop variable.
When I =1, say, J takes the values from 1 to 5, and X takes the values,
X = I*J = 1*1
X = I*J = 1*2
X = I*J = 1*3
.....

And within the inner loop, you have a print statement that writes,

K, I, "times", J, "equals", X

puting in values for the outer loop variable I = 1, and K constantly incrementing,

1, 1 times 1 equals 1
2, 1 times 2 equals 2
3, 1 times 3 equals 3
4, 1 times 4 equals 4
5, 1 times 5 equals 5

There ends the first run of the innner loop. The outer loop now increments it's loop variable, I, to equal 2, and the inner loop now outputs a 2nd times table.

6, 2 times 1 equals 2
7, 2 times 2 equals 4
8, 2 times 3 equals 6
9, 2 times 4 equals 8
10, 2 times 5 equals 10

etc.

### Related university courses

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: August 11, 2004
Today on TSR

### A level Maths discussions

Find out how you've done here

### 1,954

students online now

Exam discussions

Poll
Useful resources

### Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

### How to use LaTex

Writing equations the easy way

### Study habits of A* students

Top tips from students who have already aced their exams