140 likes | 243 Vues
Dive into the basics of control structures in Java with a focus on while and do-while loops. This guide covers how these loops function, including examples illustrating their syntax and behavior. Learn to handle common scenarios like breaking out of loops, using the continue statement, and managing boolean flags for better control flow. We will also touch on enums and their appropriate usage in Java, along with error handling strategies. Perfect for beginners wanting to build a solid foundation in Java programming!
E N D
While ciklas while (expression) { // do stuff } or int x = 2; while(x == 2) { System.out.println(x); ++x; }
while (int x = 2) { } Ar teisinga?
int x = 1; 1 while (x) { } 2 while (x = 5) 3 while (x == 5) { } 4 while (true) { }
Do ciklas do { System.out.println("Inside loop"); } while(false);
For each String [] sNums = {"one", "two", "three"}; for(String s : sNums) { System.out.println(s); }
Break ir continue boolean problem = true; while (true) { if (problem) { System.out.println("There was a problem"); break; } } while (!EOF) { //read a field from a file if (wrongField) { continue; // move to the next field in the file } // otherwise do other stuff with the field }
Žymės boolean isTrue = true; outer: for(int i=0; i<5; i++) { while (isTrue) { System.out.println(“1"); break outer; } System.out.println(“2"); } System.out.println(“3");
outer: for (int i=0; i<5; i++) { for (int j=0; j<5; j++) { System.out.println(“1"); continue outer; } System.out.println(“2"); } System.out.println(“3");
Išvardijimai enum CoffeeSize { BIG, HUGE, OVERWHELMING };
public class CoffeeTest1 { public static void main(String[] args) { enum CoffeeSize { BIG, HUGE, OVERWHELMING } // WRONG! Cannot // declare enums // in methods Coffee drink = new Coffee(); drink.size = CoffeeSize.BIG; } }
Klaidų šaltiniai • Kompiuteris • JRE • Vartotojo sukurta programa