Day 16: Jump Statements

Jump Statements

These statements alter the normal flow of control by transferring execution.

1.break Statement

We saw this break already in switch statements. Actually it terminates the loop or switch statement immediately.

To exit out of any control statements we do it let's see 2 examples of sing it in switch and for loop.

Example of break in Switch:


Break exits in Case3 after encountering the break statement.


break in Loops:

Used to exit a loop before its condition fails.



Output:

i = 1

i = 2

It gets terminated from the Loop when i=3

2. continue Statement

The continue statement is used to skip the current iteration of a loop and move to the next iteration. Just the current code with continue statement be skipped.


As discussed encountering continue the iteration will be skipped when i=3

Output:

i = 1

i = 2

i = 4

i = 5

3. return Statement

The return statement is used to exit from a method and optionally return a value to the caller of the method.

  • It Ends the execution of the current method.
  • Control returns to the method's caller.
  • If the method is void, return does not return a value but just break the execution and gets out of the method.

Example 1 (Void Method):


after return statement the code in the method becomes unreachable will not be executed.

This makes just Hello be printed in the output.

Did you notice object is not created to call the method. what makes it happen, It's all the Magic of the word Static. Let's explore it detailly tomorrow.



Output: Square of 5: 25

Control statement are a basic and simple topic. Understanding and using these statements effectively can improve the flow control and readability of Java programs. Let's Explore Static tomorrow.

要查看或添加评论,请登录

Steffi Graph Preethi S的更多文章

  • Arrays

    Arrays

    Been so long since I posted, Happy that i'm able to be back with basic concepts again. Let's see what is Arrays: As we…

  • Day 19: StringBuffer and StringBuilder in Java:

    Day 19: StringBuffer and StringBuilder in Java:

    Mutable String: A String that can be modified or changed is known as mutable String. and are classes in Java used for…

  • Day 18: Strings:

    Day 18: Strings:

    String is a class in the java.lang package and is one of the most commonly used classes.

  • Day 17:

    Day 17:

    Static Keyword: Static is a keyword in Java. The keyword is used to indicate that a particular member (method…

  • Day 16:

    Day 16:

    Looping Statements Looping statement are the execution of the block of code repeatedly until we break it. for Loop…

  • Day 15:

    Day 15:

    Java Control Statements: Java compiler executes the code from Top to Bottom. However Java provides the statement to…

  • Day 14: Encapsulation

    Day 14: Encapsulation

    Day 14: Encapsulation: Encapsulation in Java is integrating the data (fields) and the methods that operate on the data…

  • DAY 13 Polymorphism:

    DAY 13 Polymorphism:

    What is Polymorphism? In simple terms, polymorphism means "many forms," enabling a single method to work in different…

  • Abstract methods and abstract classes

    Abstract methods and abstract classes

    Day 12: What is Abstraction? Hiding the Implementation details. Interface allows 100% Abstraction, whereas Abstraction…

  • DAY11 Multiple Inheritance and Interfaces:

    DAY11 Multiple Inheritance and Interfaces:

    4. Multiple Inheritance Java does not support multiple inheritance with classes to avoid ambiguity caused by the…

社区洞察

其他会员也浏览了