Loops based on a flowchart

Asked

Viewed 1,282 times

2

How do I write the code for a loop based on this flowchart? It has a do...while. Peeciso of a simple and direct response with nested loops.

inserir a descrição da imagem aqui

  • Java or Processing ? Your question is in relation to the flowchart or the do...while ? Regardless I advise to review the whole subject given by the teacher

  • Processing or java. The important thing is that I understand how to interpret the flowchart. In the matter it was not clear. I wanted to reach the 5 loops, but I’m not getting out even the first.

  • That I would do with an infinite loop and a break in B

  • @Jeffersonquesado can give me an example?

  • I’m providing in response ;-)

  • There are 5 possible ways to write this loop. That would be one? int i=0;&#xA;do {&#xA; println("A");&#xA;} while (i<5);&#xA;&#xA;while(i<5){&#xA; println ("C"); &#xA;}

  • @Jsi83 The answer given answers, yes, your question. If you don’t understand something, feel free to question the author in the comments. The recent issue you asked in the question changes, even slightly, the meaning of the question. You initially asked for examples of loops that follow the flow chart - five, to be exact. Jefferson showed a lot more than that. If really the answer is not the desired one, you need to review your text, because what you asked he answered.

  • I changed the question, because the answer does not use...while, and this causes "A" not to be executed once if the condition is false. Thus, it does not correspond to the flowchart.

Show 3 more comments

1 answer

7


That flow, tell me this:

  1. Perform task A
  2. Compare B; If it comes true, get out of the loop
  3. Perform the task C
  4. Go back to step 1

Note that the flow in step 4 is an unconditional deviation returning to step 1. This deviation ends up creating an infinite loop (while B not satisfied). In general, I would do so:

while (true) {
  // passo 1
  // passo 2
  // passo 3
}

Step 4 is represented by the flow control provided by while. Filling in a few more gaps, we get:

while (true) {
  A();
  // passo 2
  C();
}

Assuming A() and C() are the methods called in steps 1 and 3. It may be anything, but I represented by methods.

On step 2, it involves a conditional on B. There is no case senão. So, by and large:

while (true) {
  A();
  if (B()) {
    // ação caso B seja verdadeiro
  }
  C();
}

The action to get out of the loop is with the break. So:

while (true) {
  A();
  if (B()) {
    break;
  }
  C();
}

Using the language dot to describe your diagram, I obtained the following:

diagrama original, mas em dot

I am only republican-here because I will use this language to do some manipulations in the structure of the tie and generate other equivalents, so the idea is to lessen the strangeness.

The loop structure is equivalent to the expression:

AB(CAB)*

Why do I say this? Simple, because initially A and B are executed and, depending on that result, executed C, afterward A (back in the loop) and then B again, to make the decision as to where to go.

The following description is the translation of the expression. Note that this step-by-step is equivalent to the first.

  1. Perform task A
  2. Until B, do:
    1. Execute C
    2. Execute A

Compared to the previous scheme, I no longer needed a step of unconditional deviation, I used a "while". I copied the command that was pre-conditional to the end of the loop, so that it runs at the right time the same amount of times.

The flowchart is now drawn like this:

A1-> B -> C -> A2 -> B...

In code, the while becomes a while. The rest stays the same:

A();
while (!B()) { // perceba que eu só repito enquanto `B()` for falso, já que `B()` é a condição de saída
  C();
  A();
}

Still using flowchart 2, we can do using the for. I don’t like this alternative, but it works.

A();
for (; !B(); A()) {
  C();
}

Changing the for, we get the following:

A();
for (; !B(); C(), A());

We can do the for as infinite loop also:

for (;;C()) {
  A();
  if (B()) {
    break;
  }
}

If you insist on doing the verification after the operations, this operation must necessarily be after all the operations. For this, we can define that the rodable initial be only A, assign to the rodable the execution of C and then A and then make the conditional check:

Runnable rodavel = () -> { A(); };
do {
  rodavel.run();
  rodavel = () -> { C(); A(); };
} while (!B());

We can also control differently the way the operation is run C. We can control through a flag mnemonically named deveExecutarC, which starts as false but, at the end of the loop, is assigned the true value in it:

boolean deveExecutarC = false;
do {
  if (deveExecutarC) {
    C();
  }
  A();
  deveExecutarC = true;
} while (!B());

We can try to assign the flag value several times by placing this assignment within the block else:

boolean deveExecutarC = false;
do {
  if (deveExecutarC) {
    C();
  } else {
    deveExecutarC = true;
  }
  A();
} while (!B());

This problem could be solved recursively as well:

private void executaRecursivo() {
  C();
  A();
  if (!B()) {
    executaRecursivo();
  }
}

private void fluxograma() {
  A();
  if (!B()) {
    executaResursivo();
  }
}

Just call the method fluxograma and you will get the same execution.


We can set the recursion differently, passing a parameter to it. The idea is the same as the do-while with the flag deveExecutarC:

private void fluxograma(boolean deveExecutarC) {
  if (deveExecutarC) {
    C();
  }
  A();
  if (!B()) {
    fluxograma(true);
  }
}

private void fluxograma() {
  fluxograma(false);
}

To get the desired result, just call fluxograma(), unencumbered.


More variation with recursion:

private void recursao() {
  if (!B()) {
    C();
    A();
    recursao();
  }
}

private void fluxograma() {
  A();
  recursao();
}

As usual, just call fluxograma().


A friendlier variation of the previous recursion:

private void recursao() {
  if (B()) {
    return;
  }
  C();
  A();
  recursao();
}

private void fluxograma() {
  A();
  recursao();
}

As usual, just call fluxograma().


Another variation:

private void fluxograma() {
  A();
  if (B()) {
    return;
  }
  C();
  fluxograma();
}

As usual, just call fluxograma().


One more recursion, inspired in this answer, unfolding Indirect function recursion, function pointer:

private void fluxograma() {
  A();
  Runnable funcaoSaida = B()? () -> {}: () -> { C(); fluxograma(); };
  funcaoSaida.run();
}

As usual, just call fluxograma().


If you do not want the variable:

private void fluxograma() {
  A();
  (B()? (Runnable) () -> {}: (Runnable) () -> { C(); fluxograma(); }).run();
}

As usual, just call fluxograma().

  • I understood what you did and I think it’s the melehor solution. But, what about the other 4 loops? I have to use one loop inside another?

  • I am working to show how to manipulate the order of operations so as to obtain equivalent flows and therefore another way of ordering. Just out of curiosity, where did you get that number 5?

  • The number 5 is any number. I’m trying to generate the same result for these loops. It could be any number. I just created a condition.

  • So it’s not "5 possible ways to write". You’re describing a loop that repeats 5 times

  • No, 5 was a test and it could be anything. I made a nonsense code. That’s five different responses to the flowchart. One loop is with break (answer 1), the other one I believe is aligned loops (answer 2), the other one a loop out of the other (answer 3), but the other 2?

  • Seeing the answers of that question I managed to count at least 7 ways to your case

  • Could you help me write the loop without the break? in the flowchart there is one of the...while for sure, because A comes before the condition. So far so good, but how do I continue the loop with C? is half and half: A needs to be executed by Ando Condicão and C, no.

  • 15 alternatives: 2 with while, 3 with for, 3 with do-while, and the rest with esoteric recursions. I got tired of varying, I could go far. Good studies

  • The answer given seems very complex. I just need a do. while that represents the flowchart.

  • I gave you 3 options with do-while so that you can use them at will. I even showed equivalence flows and also the expected regular behavior of this algorithmic scheme.

  • I’m starting to understand loops. It got even more complicated. "A' should run at least once. If I use while without the do, this does not occur.

  • I gave two options with while that trivially you verify that A() is always executed

  • What was not clear: the rhombus is a condition represented in the flowchart, it is an IF or a WHILE in the code?

  • @Jsi83 while, if and for has conditions inside them. This rhombus and these squares have a view more similar to assembly than to structured or object oriented. Understand that this is a state machine, in which, given one state, it is necessary to go to another. Do not try to take a dictionary and translate literally what is written. To translate literally, you would need to goto, but Java doesn’t have it

  • do{ A if (true) break; C } while(true); That code makes sense to you?

  • Has not B, therefore does not answer the question. Not to mention that the way it is written, the correct version is the same thing as the first option I made, with while

  • do {&#xA; A();&#xA; if (B()) {&#xA; break;&#xA; }&#xA; C();&#xA;}while (true) I can reverse the while and use the?

  • Note that this is identical to the first form I wrote.

  • So now I think I understand. This is a way to write the loop. Now I need to understand the other 4 ways.

  • I provide you 15

Show 15 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.