continue statement

Quick question: assume that I have the following structure, will the "continue" command successfully lead me to the next iteration in the outermost for loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
  for(){
     if(){
        if(){
           if(){
              continue;
           }
        }
     }
     if(){
         for(){
         }
     }
  }
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  for()
  {
     if(){
        if(){
           if(){
              continue;
              stuff(?);
           }
        }
     }
     if(){
         for(){
         }
     }
  }  //I didnt count these, I assumed this is the top for() loop closer. 


The bold code is skipped and the next for-loop iteration proceeds, if any.
Last edited on
@jonnin: thanks for the note. This programming is more and more interesting to me each day :)
continue is just goto, renamed because "goto is bad". Its not used a lot, but there are a few places where its the only solution.
@jonnin: hmm... yeah I heard about "goto is bad" thing although in Fortran it is always used. Maybe later I will know more about why it is bad. At the moment I am trying to avoid it as much as possible due to this. Thanks for this note as well!
Another goto in disguise is break.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{
  std::string input;
  for(;;)
  {
      std::cout << "Enter command or quit to finish ";
      std::cin >> input;
      if (input == "quit")
        break;
  }
}


Same as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
  std::string input;
  for(;;)
  {
      std::cout << "Enter command or quit to finish ";
      std::cin >> input;
      if (input == "quit")
        goto exit;
  }
  exit:
  {}
}
If you're curious about why "goto" is bad (in excess), read Edsger Dijkstra's famous essay:
http://bioinfo.uib.es/~joemiro/teach/material/escritura/gotoharmfulCol.pdf
Last edited on
@mbozzi: thank you for referring me to this article! As I am not a native English speaker and I am not quite familiar with programming yet, it seems to me that it is bad practice in that it interrupts the flow of the language but I might be interpreting it entirely wrong lol
Topic archived. No new replies allowed.