1 / 10

Loops in Java

Loops in Java. What are loops?. Loops are used for repeated execution of a block. There are following types of loops in Java: while Loop do…while loop for loop. While Loop. Syntax: while (Boolean Expression) { //your code } e.g. Example_while.java. Do ... while Loop. Syntax: do

evania
Download Presentation

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. Loops in Java

  2. What are loops? Loops are used for repeated execution of a block. There are following types of loops in Java: • while Loop • do…while loop • for loop

  3. While Loop Syntax: while (Boolean Expression) { //your code } e.g. Example_while.java

  4. Do...while Loop Syntax: do { //your code }while(Boolean_expression); e.g. Example_dowhile.java

  5. For Loop Syntax: for(initialization; Boolean_expression; update) { //Statements } Initialization: The first step in execution for initializing your loop variable. Boolean_expression: Condition for evaluation. If evaluated to true, loop executes, else goes to the next statement. Update: After the body of the for loop executes, the flow of control jumps back up to the update statement. This statement allows you to update any loop control variables. The code block is re-executed till the time the Boolean expression evaluates to true. Example: Example_for.java

  6. Enhanced for loop in Java: Typically used for arrays. Syntax: for(declaration : expression) { //your code } Example: Example_efor.java

  7. Break & Continue Break: for breaking out of the loop immediately and execute what's after the block. Continue: it causes the flow of control to jump to the next iteration. Syntax: break; // for break continue; //for continue Example: Example_break_continue.java

  8. Methods

  9. What is it all about? A Java method is a collection of statements that are grouped together to perform an operation. Syntax: modifier returnValueTypemethodName(list of parameters) { // Method body; } Example: Example_method.java

  10. Constructors • A constructor is used for initializing an object. • It has the same name as the class. • It looks similar to a method. • It does not have a return type. Example: Example_constructor.java Example_cons_invkd.java

More Related