"While" loops and "for" loops in flowcharts

When code is generated for a flowchart, Rhapsody® recognizes flowchart elements that represent while loops and generates the appropriate code. You can have for loops generated in the code instead by providing initialization and step code.

Generation of while loops in flowchart code

If an action or decision node has an exit transition with a guard as well as a second exit transition, and there is also a transition that brings the flow back to the original decision point, Rhapsody recognizes that these elements represent a while loop, and generates the appropriate code.

For example, the following flowchart segment will result in a while loop, as can be seen in the generated code that follows it.

example of "while" loop in flowchart
void Thermostat::ModifyTemperature() {
    //#[ transition ModifyTemperature().2 
    //#]
    while (temperature < temperatureRequested)
    {
        // State action_2
        //#[ state ModifyTemperature().action_2.(Entry) 
        temperature++;
        //#]
    }
    return;
    
}

Generation of for loops in flowchart code

If an action or decision node has an exit transition with a guard as well as a second exit transition, and there is also a transition that brings the flow back to the original decision point, you can have Rhapsody generate a for loop rather than a while loop by carrying out the following steps.

  1. Open the Features window for the action or decision that has the guard for exiting the loop, and apply the stereotype FlowChartForLoop to the element.
  2. After setting the stereotype for the element, enter the relevant loop initialization code in the Loop initialization field on the General tab of the Features window.
  3. Enter the relevant loop increment code in the Loop step field on the General tab of the Features window.

For example, the following flowchart segment will result in a for loop, as can be seen in the generated code that follows it. In this example, the code used for Loop initialization was monthCounter = 1, and the code used for Loop step was monthCounter++

example of "for" loop in flowchart
void Calendar::printMontlySchedules() {
    //#[ transition printMontlySchedules().2 
    //#]
    for (monthCounter = 1; month < 13; monthCounter++)
    {
        // State action_0
        //#[ state printMontlySchedules().action_0.(Entry) 
        printEventsForMonth(monthCounter);
        //#]
    }
    return;
    
}