Advanced Control Flow

This simulation allows you to explore two main experiments:

  1. Factorial Calculation using Loops
  2. Pyramid Pattern using Nested Loops

You can switch between these two modes at any time using the Switch to Nested Loop or Switch to Simple Loop button. Each experiment can be performed in two ways:

  • Start with the default mode (Simple Loop for factorial, Nested Loop for pyramid)
  • Or switch to the other mode using the switch button

Experiment 1: Factorial Calculation (Simple Loop)

  1. By default, the simulation opens in the Simple Loop mode.
  2. Select the loop type (for, while, or do-while) from the dropdown menu.
  3. Enter the number for which you want to calculate the factorial.
  4. Click Start to begin the simulation.
  5. The code for the selected loop will be displayed, and the Next button will be enabled.
  6. Click Next repeatedly to execute the code step by step. The current line and local variables will be highlighted and updated after each step.
  7. To try the pyramid pattern experiment, click Switch to Nested Loop.

Experiment 2: Pyramid Pattern (Nested Loop)

  1. Click Switch to Nested Loop to open the nested loop simulation (or start here if already in this mode).
  2. Enter the desired height of the pyramid.
  3. Click Start to begin the simulation.
  4. The code for the nested loop will be displayed, and the Next button will be enabled.
  5. Click Next repeatedly to execute the code step by step. The current line and local variables will be highlighted and updated, and the pyramid pattern will be displayed.
  6. To return to factorial calculation, click Switch to Simple Loop.

Manual

This simulation demonstrates:

  • How different looping constructs (for, while, do-while) can be used to calculate the factorial of a positive integer.
  • How nested loops can be used to generate a pyramid pattern of any given height.