Cherry
Cherry

Reputation: 81

Java unbounded for loop

Unbounded loop: where number of times its body repeats is unknown in advance. • e.g. repeat until the user types "q" to quit.

This is the explaination of an unbounded for loop. I don't understand it, could someone maybe give me an example? Thanks in advance

Upvotes: 2

Views: 1968

Answers (5)

Andreas
Andreas

Reputation: 5103

Here is an example:

import java.io.IOException;

public class Class {

  public static void main(String... args) throws IOException {
    int count = 0;
    for (char c = ' '; c != 'q'; c = (char) System.in.read()) {
      System.out.println("current value of c: " + c);
      System.out.println("Number of keystrokes so far: " + count++);
    }
  }
}

Upvotes: 0

boxed__l
boxed__l

Reputation: 1336

Unbound loops can also be due to logical errors:

Example:

int i;
for(i=0;i!=i+1;++i){
    //System.out.println(i);
}
System.out.println(i); // never prints

Upvotes: 0

Arnaud
Arnaud

Reputation: 17534

A bounded loop is a loop with a known iteration count, like :

for(int i=0;i<10;i++){

}

As you see, you're guaranteed (unless something like some Exception or a break statement occurs), that there will be 10 iterations.

An unbounded loop has no bounds, you can't tell in advance how many iterations there will be , like :

boolean exit = false;

while(!exit){

 // set exit to true to stop looping
}

Or like the example from @Fildor, a for loop with no ending condition. Note that this loop is infinite until like said above, a break statement or some exception occur (this loop is similar to while(true) ) :

for(;;)

Upvotes: 0

yasyapro
yasyapro

Reputation: 21

look, in for loop, you can set the counter and when it reaches the loop end conditions, such as for (int i = 0; i < 10; i++). If you need unbounded loop (when you do not know how many iterations you need, but you have some conditions for its completion), you can set the following condition:

for ( ; ; ) {
   if (condition == true) exit from loop;
} 

or using while loop, I think that's better for unbounded.

Upvotes: 1

J Blaz
J Blaz

Reputation: 783

The following is code that does exactly what the definition you posted describes. It just means that when the programmer is typing the code there is no knowledge of the amount of times the body of the while loop will run.

BufferedReader br = null;

try {

    br = new BufferedReader(new InputStreamReader(System.in));

    while (true) {

        System.out.print("Enter something : ");
        String input = br.readLine();

        if ("q".equals(input)) {
            System.out.println("Exit!");
            System.exit(0);
        }

        System.out.println("input : " + input);
        System.out.println("-----------\n");
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (br != null) {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Code from: here

Upvotes: 0

Related Questions