Java Java Basics Perfecting the Prototype Conditional ints

Here, I have been asked to exit the program. I have mentioned break but its gives me error. Please help

Please let me know what is wrong I am doing

Conditional.java
int numberOfPeople = 3;
if(numberOfPeople < 4){
  console.printf("Your table is ready");
  break;
}

1 Answer

andren
andren
28,468 Points

The break keyword is used to force a loop to exit, it is not used to make the program itself exit. To force the program to exit you have to call System.exit and pass it an exit code. Like this:

int numberOfPeople = 3;
if(numberOfPeople < 4){
  console.printf("Your table is ready");
  System.exit(0); // Exit with code 0 (Which indicates that nothing went wrong)
}

Thanks. It helps.