-2

I need help with a while(1) loop that contains a continue and break statement. It must count from numbers 1 to 20 and for every even number, it must output the values. I have to use a continue after my writeToPage statement and use a break statement when it reaches 20. This is what I tested out but the file will not even load:

writeToPage("Program 4: Continue and Break");
writeToPage("");

while(1) {
    if (i % 2 == 0){
        writeToPage(+ i);
        continue;
    }
    if (i >= 20){
        break;
    }
}

I'm not sure if I'm putting them in the wrong place.

nkrivenko
  • 1,231
  • 3
  • 14
  • 23
coding cat
  • 17
  • 5
  • 2
    Try `while (true) ` maybe? – daniu Nov 18 '20 at 18:04
  • `the file will not even load` what do you mean ? Also the continue is useless here – azro Nov 18 '20 at 18:04
  • the instructions say to use while(1) but I'm not sure how to make it work – coding cat Nov 18 '20 at 18:05
  • Change this line `writeToPage(+ i)` to `writeToPage(++i)` and come back again – Tugay Nov 18 '20 at 18:05
  • 2
    You canno't use `while(1)` in Java, that is not valid – azro Nov 18 '20 at 18:05
  • When I try to load my code into the browser it will not load, it just keeps saying it's loading. It works fine when I take out these lines of code. – coding cat Nov 18 '20 at 18:05
  • U need boolean value maybe 1=1? – Kick Buttowski Nov 18 '20 at 18:06
  • I tried i ++ and it did not work. – coding cat Nov 18 '20 at 18:06
  • You should consistently line up your brackets and indentation. Doing so will make it far easier to see where a block of code starts and stops. – Big Guy Nov 18 '20 at 18:06
  • Why don't you link to the assignment, so we can deduce for ourselves what is required. It sounds like your professor just wanted you to specify an _infinite_ loop, e.g. `while(true)`, `for(;;)`, or `while(1)` in languages where `0`==`false` – Rogue Nov 18 '20 at 18:07
  • I'm not sure how to link here as I'm still new but these were the instructions: – coding cat Nov 18 '20 at 18:08
  • // Program 4: // Write a while (1) loop (infinite loop) that counts // numbers from 1 to 20, for every even number // print out the values and use 'continue' after // the write statement // // When you reach the number past 20, break from the // loop using the break statement // – coding cat Nov 18 '20 at 18:08
  • If the instructions say to use `while (1)`, then the instructions are not for Java, but maybe C++ or JavaScript. Please pay attention to what language you're learning and/or writing in (or find better learning material that actually fits the language you're using). – Andreas Nov 18 '20 at 18:18
  • In c, ``while(1)`` is the idiom for an infinite loop. In java, the idiom is ``while(true)1`` because java doesn't allow you to use an int instead of a boolean. – NomadMaker Nov 18 '20 at 18:57

1 Answers1

0
  1. For infinite loop, you need to write while(true) instead of while(1).
  2. If i % 2 != 0 is true, simply increment i by 1 and continue; otherwise, print the value of i and increment i by 1.

Demo:

public class Main {
    public static void main(String[] args) {
        int i = 1;
        while (true) {
            if (i % 2 != 0) {
                i++;
                continue;
            } else {
                writeToPage(i);
                i++;
            }

            if (i >= 20) {
                break;
            }
        }
    }

    static void writeToPage(int i) {
        System.out.println(i);
    }
}

Output:

2
4
6
8
10
12
14
16
18
20
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110