1 / 14

Understanding Control Structures: While and Do-While Loops in Java

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!

kitra-wells
Télécharger la présentation

Understanding Control Structures: While and Do-While Loops in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Valdymo struktūros 2

  2. While ciklas while (expression) { // do stuff } or int x = 2; while(x == 2) { System.out.println(x); ++x; }

  3. while (int x = 2) { } Ar teisinga?

  4. int x = 1; 1 while (x) { } 2 while (x = 5) 3 while (x == 5) { } 4 while (true) { }

  5. Do ciklas do { System.out.println("Inside loop"); } while(false);

  6. For each String [] sNums = {"one", "two", "three"}; for(String s : sNums) { System.out.println(s); }

  7. 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 }

  8. Ž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");

  9. 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");

  10. Išvardijimai enum CoffeeSize { BIG, HUGE, OVERWHELMING };

  11. 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; } }

  12. Išimtys

  13. Klaidų šaltiniai • Kompiuteris • JRE • Vartotojo sukurta programa

More Related