For statement - practicing

In this section we will only practice the use of for statement.

Tasks for exercise

Three times up and down

Karel is on a rectangular board of 5 rows and 7 columns and needs to reach the right bottom square.

Karel should repeat one complex action three times, that is, to go to the next column to the right, go to the top of the column, go to another column to the right, go down to the first row, and finally turn towards the last column to prepare for the next iteration.

Complete the program, taking into consideration that the counter in for statements you write should not be named i (this name is already used in the outer loop).

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_for_up_col_down_col_constant)

Bring all the balls from all the squares

Karel should bring all 12 balls to the starting square.

Karеl needs to repeat “step into the next column and empty it” four times and eventually come back to the starting square and drop all the balls. Karel can empty each column by repeating “move one step forward and take the ball” three times, and then returning to the bottom of the column, facing the next column.

Complete the program.

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_for_fetch_from_matrix)

Triple loop

Now, there are 4 balls on each of the 6 squares, similar to the the previous task. Karel should bring all 24 balls to the starting square.

The only difference (compared to the previous task) is that now pick_ball() statement should be in an additional loop, the third in depth. Also, the number of balls that Karel drops on the starting square (at the end of the program) is different. Therefore, a bit easier way to solve the task is to copy the previous program and modify it.

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_for_fetch_24_from_matrix)

Ascend and descend

Karel should climb up the first set of stairs, then go down the other ones, and end up in the lower right corner.

Now we need two loops one after the other (not nested). In the first loop, Karel should climb to the first staircase, and come down the second staircase in the second loop. In each loop, Karel has to perform 4 actions that represent one step up or down the stairs.

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_for_stairs_constant)

Collect the balls on the stairs

Karel should finish again in the lower right corner, and along the way he should take all the balls.

A good way to solve this task is to start from the solution of the previous task. Hint: copy the solution of the previous task here, and then insert the loops for taking the balls.

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_for_stairs_and_balls_constant)