Skip to main content
Logo image

Section 10.2 for and if together

You can nest an if statement inside a loop or a loop inside an if statement.
Take a look at the following example which prints out those numbers between 0 and 29 that are divisible by 3. Make sure to try to understand how the code accomplishes this task.

admin.....open in new window

Investigate 10.2.

Is there a difference between the two following scenarios, if the same statements are used within each element?
  • Nesting an if statement within a loop
  • Nesting a loop within an if statement