English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can someone look at this code and tell me what it is supposed to do?
for(row=row_index-1; row<=row_index+1; row++)
for(col=col_index-1; col<=col_index+1; col++)

This was the code that was provided to find the number of neighbors of each cell.

2007-11-29 04:13:26 · 2 answers · asked by Smokey. 6 in Computers & Internet Programming & Design

2 answers

A For statement is one that executes as long as the conditional statement is true.

The first part (row=row_index-1) is the starting condition. This is where your program is at when the for loop begins.

The second part (row <= row_index+1) is the conditional statement. As long as this statement remains true, the for loop will continue to execute.

The third part (row++) is the "updater" to the equation. Basically, it increases the row_index by 1, so the For statement reruns, this time testing the updated value against the conditional statement.

2007-11-29 04:25:17 · answer #1 · answered by JessiC 3 · 1 0

This code walks through each neighboring element of the array surrounding the element at (row_index, col_index).
If there is a third line here it would would probably be a comparison against the contents of the array position and a decision about the contents.
By the way some where you have to test for a boundary because the row or column in these loops could be outside the the array and you would have a bug either a crash or an unpredictable condition.

2007-11-29 12:51:09 · answer #2 · answered by tfloto 6 · 1 0

fedest.com, questions and answers