1 / 9

Understanding Java Loops: A Practical Guide for Computer Programming

This guide provides an overview of various loop constructs in Java, including for, while, and do-while loops. Each loop type is explored with a focus on its unique advantages and typical use cases. You will learn how to implement and nest loops effectively to meet your programming requirements. Additionally, the guide includes practical examples and a comparative analysis of loop implementations, emphasizing the importance of condition checks and execution flow. By the end of this guide, you'll be equipped to apply loops in diverse programming scenarios confidently.

mandek
Télécharger la présentation

Understanding Java Loops: A Practical Guide for Computer Programming

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. CS 240 – Computer Programming ILab KalpaGunaratna – kalpa@knoesis.org http://knoesis.wright.edu/researchers/kalpa/

  2. Contact • Contact by e-mail • kalpa@knoesis.org • Office hours • 376 Joshi • Mondays & Wednesday 3:00 pm – 4:00 pm

  3. Intention • Familiar yourself with various loops available in Java language. • Some loops have advantages above others but all loops can be used most of the time for many tasks. • Loops, • for , while, do while • Any loop can be nested in whatever you want and for your requirement.

  4. while loop • while (<boolean condition>) { // loop body } Until the boolean condition is false Loop will iterate. First checks the condition and execute The body. <boolean condition> false true <loop body>

  5. do – while loop • do { // loop body } while (<boolean condition>) • Loops body is executed at least before checking the condition. <loop body> <boolean condition> true false

  6. for loop • Steps, initial action (only once) -> check condition -> body -> final action -> loop <initial action> <boolean condition> false true <loop body> <action>

  7. Following two for loops and while loop are equivalent. for( ; ; ){ // do something } for( ; true; ) { // do something } while( true ){ // do something }

  8. In lab • Remember the last week’s lab about printing a triangle. • Numbers you have to print is all about powers of 2. • Math.pow(2, <power>); • Numbers should be exactly as they are shown in the assignment and also they should align in the exact way !

  9. Post lab • Repeat the same process as you did in in lab and use whole and do – while loops. No for loops ! • You should use at least one of while and do – while loops.

More Related