【问题标题】:How to represent a loop with a "continue" statement from C in a PlantUML activity diagram如何在 PlantUML 活动图中用 C 中的“继续”语句表示循环
【发布时间】:2019-10-31 05:03:57
【问题描述】:

我需要使用 PlantUML 将下面的 C 代码转换为活动图。

从下面的代码中实现“继续”语句有什么好的解决方案?

    void function_1(){
    int a = 0;
    int b =0;
      for (int i; i < 8; i++)
      {
        if (i < 2)
        {
           continue;
        }
        if (i > 4)
        {
            a = 1;
        }           
        else
        {
           b = 2;
        }
      }
    }

【问题讨论】:

  • 欢迎来到 StackOverflow。您的代码将永远不会执行任何操作,除非处于无限循环中,因为您在每次循环时都设置了 i=5
  • 代码仅以 continue 语句为例。无论如何,我更正了代码。谢谢
  • Plantuml 在可表示的语言结构方面有点受限。可能需要一种解决方法来尽可能接近所需的输出。你能澄清一下想要的输出是什么吗?这是(在我看来)图片有用的罕见情况之一。甚至可能是手绘图。或者您可以尝试在网上找到一个看起来与您想要的相似的示例。
  • 带有if (i &lt; 2) {continue;} 的部分实际上意味着在这种情况下您无需执行任何操作。所以,就当它不存在吧。如果我正确理解期望的结果,这应该没问题。
  • @virolino: if (i

标签: c uml activity-diagram plantuml


【解决方案1】:

我不懂 C,但在我看来,在 PlantUML 中表示逻辑的一种方式如下:

start
while (i < 8 ?)
  if (i > 4 ?) then (yes)
      :a = 1;
  else (no)
      if (i >= 2 ?) then (yes)
      :b = 2;
      else (no)
      endif
  endif
 endwhile (no)
:Carry out the next task;
end

“执行下一个任务”任务是一个占位符。它应该替换为您的应用程序接下来应该执行的任何操作。

产生the following diagramme

【讨论】:

  • 在我看来您缺少 for 循环。 continue 语句意味着(Kerningham 和 Ritchie,C 编程语言 1978 年,第 203 和 62 页):使控制权传递到最小的封闭 while、do 或 for 语句的循环继续部分;那就是结束循环。在另一个地方它说:它导致封闭循环(for,while,do)的下一次迭代开始。
  • 你是绝对正确的,@albert。我在 plantUML 图表中添加了while 语句。
  • 作为附注elseif,图表看起来确实更好(在我看来)
【解决方案2】:

无论是否将代码转换为另一种语言,我都会首先通过以下方式对其进行优化:

void function_1(){
int a = 0;
int b =0;
  for (int i=0; i < 8; i++)
  {
    if (i > 4)
    {
        a = 1;
    }           
    else if ( i >= 2 )
    {
       b = 2;
    }
    else
    {
        /* - nothing to do */
        /* - this statement is here as proof that we do not want to handle
        /*   other cases, as opposed to just forgetting about them */
        /* - it can be used in the future for extension, as needed */
    }
  }
}

【讨论】:

    猜你喜欢
    • 2022-12-17
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2018-04-25
    相关资源
    最近更新 更多