How to add a repeat loop (for) when needed

Asked

Viewed 131 times

0

I have a program where there are three parameters. So:

These three parameters are positions within the m_aluno[w_i][w_j][w_k]);, where w_i can receive a position informed by the user or not and this serves to w_j and w_k also; My problem lies where, if a value for w_i and soon w_j and w_k are empty, only the w_j and the w_k that will begin in 0 soon the w_i shall be in an informed position!

My case is, "I want to add one loop only when necessary. How to do?"

I played Jsfiddle with all as condições for better visualization (it was supposed to work, but I don’t know how to edit to run the function first).

Code I wish to decrease ²

Thus, there are três for's:

for (w_i=0; w_i<w_ii; w_i++) //primeiro
     {
     for (w_j=0; w_j<w_jj; w_j++) //segundo 
         {
         for (w_k=0; w_k<w_kk; w_k++) //terceiro
             {
  • If the w_i not start in 0 the first for has to go away.
  • If the w_j not start in 0 the according to for has to go away.
  • If the w_k not start in 0 the third for has to go away.

The w_i, w_j e w_k just won’t start in 0 if the user gives some value to them.

  • Which language is using?

  • I’m doing in JavaScript! It actually needs to be in JS :x

  • I don’t quite understand what you’re trying to do. What does "add no to the value" mean? Perhaps it would be clearer if you gave a simplified example including your input and desired output

  • @hugomg I will edit for better understanding!

  • @Alexandre JS = JavaScript

  • Yes I know @Thomas! In the above comment I am stating that it should be on JS ^^

  • From what I understand, the noose will be done when w_i is informed, iterating w_k and w_j. But until what value do you want to iterate the variables? The problem is not very clear and the nomenclature of the variables makes it even more confusing.

  • @Thomas put in Jsfiddle to show how I had done before, it’s not running because I guess I’m not getting the function! (I don’t know how to edit there)

  • I was doubtful about that part: if a value for w_i is given and then w_j and w_k are empty, only w_j should be iterated and w_k should start at 0 so w_i should be in the informed position!. Basically, if the College is informed, only the Room and Matter should be iterated in the for?

  • Yes, and it would be the same if it were: It was informed only the room, then only the college and the matter should iterate in for. In the Jsfiddle example there are cases that may occur!

  • Those three loops, then, won’t cuddle as the code comes up, right? They will iterate independently as those NOT informed... that’s it?

  • Yes that’s right @Thomas

  • 1

    @Alexandre I realize you need help, this code is not possible to keep this way... Can you also explain what you want to do? What functionality you need, so we can help you remake that code that’s too complex.

  • Sure @Sergio, my purpose is: I’m inserting inside the m_aluno[w_i][w_j][w_k] values in certain positions ( 0 , 0 , 0 ). And after inserting I would like to search what is contained in these certain positions! So it is my case to play the informed value (that would be the position) within the w_i or w_j or w_k or even three or two values!

  • @Alexandre Mas a Sala, to exist, must be connected to a Colégio, right? For example, to list the Salas available, it would not be easier to force the user to choose a Colégio rather than listing all the Salas of all the Colégios?

  • In that case, to choose one w_j, need to choose a w_i. In fact, why not call it indice_colegio? The purpose of the variable would be clearer in the code.

  • Yes it is correct what you said, but if case is informed 2 colégios 2 salas and 2 matérias. Do you agree that in colégio 1 would have 2 salas and 2 matérias and in the colégio 2 the same right thing?

  • So if he wants to list the values that are only in sala 2, he will show of colégio 1 and of colégio 2!

Show 13 more comments
No answers

Browser other questions tagged

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