I know a FOR loop is for definite loops when you know the starting value and ending value. It use step value (number use to alter a loop control variable on each past of the loop) in which the control value changes after the body executes. A for statement is never required for any loop; it is more efficient and less room for errors.

I can see the difference in writing out a for loop in pseudocode, but what is the difference when writing a for loop in flowchart form?

Dani

How about...

for (int n=1; n<5; n++) {
    do something
  }

   .-----.
  | Start |
   '-----'
      |
      V
  +-------+
  | n = 1 |
  +-------+
      |<---------------------------------------------+
      V                                              |
     ' '                                             |
   /     \           +--------------+   +---------+  |
  '  n<5? ' --yes--> | do something |-->| n = n+1 |--+
   \     /           +--------------+   +---------+
     ' '
      | no
      V
   .------.
  |  Done  |
   '------'


  n=1
  while (n<5) {
    do something
    n += 1
  }

   .-----.
  | Start |
   '-----'
      |
      V
  +-------+
  | n = 1 |
  +-------+
      |<---------------------------------------------+
      V                                              |
     ' '                                             |
   /     \           +--------------+   +---------+  |
  '  n<5? ' --yes--> | do something |-->| n = n+1 |--+
   \     /           +--------------+   +---------+
     ' '
      | no
      V
   .------.
  |  Done  |
   '------'

In flow chart, they are very similar (or the same if you want to). In coding, they are different because you need to control your loop. But remember that while can also be do-while too.

Edited 5 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.