Enhance Your Coding Skills: FOR Loop and Array Problem Solving Resources

  • Thread starter RadiationX
  • Start date
  • Tags
    Array Loop
In summary: This will run the loop 20 times and everytime it will print the value of J which will be 2, 4, 6, 8, 10, 12, 14, 16, and 18.
  • #1
RadiationX
256
0
Does anyone know of a site that has some problem solving using FOR loops and or Arrays?
 
Computer science news on Phys.org
  • #3
I'm Looking For Something That Is Not Langauge Specific, But If I Had To Choose A Langauge Visual Basic Would Be The One. Thx
 
  • #4
A for statement does three things:

Assign a starting value to a counter variable
Increments the counter variable by a specified amount
Checks to see if the expression which makes the loop continue holds true

For example in C++:

for( int x = 0; x < 5 ; x++) { some code}

When the computer first enters the for loop the first thing it does is set x to the value 0. Then it checks the condition to see if it should enter the loop. Since x which is equal to 0 is in fact less than 5 it executes some code. When it finishes it increments x by 1 and then checks to see if the condition still holds. If it does it executes some code, increments x by 1 and checks to see if the condition still holds. This basic sequence happens over and over again until the condition is false, at which point it leaves the for loop.
 
  • #5
In Visual Basic, the syntax is a bit different.

DIM A(10)

gives you eleven variables to play with, called A(0), A(1), ... A(10)

the FOR statement works like this:


FOR J = 1 TO 10

... do some stuff ...


NEXT


this makes J take the value 1 for the first pass through the loop, 2 for the second and so on up to 10 the last time through.

To make the values of A() be the seven times table, you could do something like:

DIM A(12)

FOR J = 1 to 12

A(J) = 7 * J

NEXT


If you want the loop variable to go in steps other than 1 you add a step clause:

FOR J = 0 to 20 STEP 2

PRINT J
' this loop will print 0, 2, 4, 6, ... 18, 20

NEXT
 

FAQ: Enhance Your Coding Skills: FOR Loop and Array Problem Solving Resources

1. What is a FOR loop?

A FOR loop is a type of programming loop that allows for the repetition of a set of code for a specified number of times. It is commonly used to iterate through arrays or to execute a task multiple times with different variables.

2. How do I use a FOR loop?

To use a FOR loop, you will need to specify the starting and ending values for the loop, as well as the increment value. Inside the loop, you can add the code that you want to repeat. The loop will run until the ending value is reached, with the specified increment value being added each time.

3. What is an array?

An array is a data structure that can hold multiple values of the same data type. It allows for the storage and manipulation of a large set of data in a single variable.

4. How do I declare an array?

To declare an array, you will need to specify the data type of the values it will hold, as well as the size of the array. For example, to declare an array of integers with a size of 10, you would write: int[] myArray = new int[10];

5. How do I access values in an array using a FOR loop?

To access values in an array using a FOR loop, you can use the loop's counter variable as the index for the array. For example, if you have an array named myArray with 5 elements, you can access the values using the loop variable i like this: myArray[i]. This will iterate through the array and access each element one by one.

Similar threads

Replies
2
Views
897
Replies
5
Views
1K
Replies
7
Views
1K
Replies
9
Views
2K
Replies
21
Views
2K
Back
Top