Cover

Table of Content

  1. Java Control Flow | Control Statements in Java
  2. Loop Statements in Java
  3. Java Switch Statement
  4. Loops in Java
  5. Java While Loop
  6. do-while Loop in Java
  7. Break Statement in Java
  8. Continue Statement in Java
  9. Comments in Java 

 

This eBook is based on Java programming that has been collected from different sources and people. For more information about this ebook. Kindly write to ummedsingh7427@gmail.com. I will happy to help you.

                                                                   Copyright 2023 by Ummed Singh
This eBook is a guide and serves as a next part of first guide Be Expert in Java part-1 which has already been published. This book has been written on the advice of many experts and sources who have good command over Networking and routing. They are listed at the end of this book. All images used in this book are taken from the LAB which is created by experts. All rights reserved, including the right to reproduce this book or portions thereof in any form whatsoever. For any query reach out to the author through email.

Java Control Flow | Control Statements in Java

In Java, the code is executed from top to bottom, and statements are executed in the order they appear. However, Java provides control flow statements to manage the flow of code execution. These control flow statements are essential features of Java, facilitating a smooth program flow.

Java offers three types of control flow statements:

Decision Making statements:

Decision-making statements determine which part of the code to execute based on certain conditions. They evaluate Boolean expressions and control the program flow accordingly. There are two types of decision-making statements in Java: the "if" statement and the "switch" statement.

1.1) If Statement:

The "if" statement in Java is used to evaluate a condition, altering the program's flow based on the condition's result, which can be either true or false. In Java, you can find four types of "if" statements as listed below.

Let's explore the different types of if-statements in Java:

Simple if statement:

The simple if statement is the most basic control flow statement in Java. It evaluates a Boolean expression and executes a block of code only if the expression evaluates to true.

Syntax of the simple if statement:

if (condition) {

// Executes when the condition is true

}

Example:

public class Student {

public static void main(String[] args) {

int x = 10;

int y = 12;

if (x + y > 20) {

System.out.println("x + y is greater than 20");

}

}

}

Output:

x + y is greater than 20

if-else statement:

The if-else statement is an extension of the simple if statement. It allows for an alternative block of code to be executed when the condition of the if-block is false.

Syntax of the if-else statement:

if (condition) {

// Executes when the condition is true

} else {

// Executes when the condition is false

}

Example:

public class Student {

public static void main(String[] args) {

int x = 10;

int y = 12;

if (x + y < 10) {

System.out.println("x + y is less than 10");

} else {

System.out.println("x + y is greater than 20");

}

}

}

Output:

x + y is greater than 20

if-else-if ladder:

The if-else-if ladder consists of multiple else-if statements following the initial if-statement. It forms a chain of conditions, and the program enters the block of code corresponding to the first true condition encountered. An optional else statement can be used at the end to handle situations where none of the conditions are true.

Syntax of the if-else-if ladder:

if (condition1) {

// Executes when condition1 is true

} else if (condition2) {

// Executes when condition2 is true

} else {

// Executes when all conditions are false

}

Example:

public class Student {

public static void main(String[] args) {

String city = "Delhi";

if (city.equals("Meerut")) {

System.out.println("city is Meerut");

} else if (city.equals("Noida")) {

System.out.println("city is Noida");

} else if (city.equals("Agra")) {

System.out.println("city is Agra");

} else {

System.out.println(city);

}

}

}

Output:

Delhi

Nested if-statement:

In nested if-statements, an if statement can contain another if or if-else statement inside it.

Syntax of Nested if-statement:

if (condition1) {

// Executes when condition1 is true

if (condition2) {

// Executes when condition2 is true

} else {

// Executes when condition2 is false

}

}

Consider the following example:

public class Student {

public static void main(String[] args) {

String address = "Delhi, India";

if (address.endsWith("India")) {

if (address.contains("Meerut")) {

System.out.println("Your city is Meerut");

} else if (address.contains("Noida")) {

System.out.println("Your city is Noida");

} else {

System.out.println(address.split(",")[0]);

}

} else {

System.out.println("You are not living in India");

}

}

}

Output:

Delhi

Switch

Imprint

Publisher: BookRix GmbH & Co. KG

Text: Ummed Singh
Images: Mitesh Srivastav
Cover: Lokesh Pandey
Editing: Ummed Singh
Proofreading: Ajay Sharma
Translation: Anand Kumar
Layout: Mahesh Babu
Publication Date: 08-06-2023
ISBN: 978-3-7554-4902-7

All Rights Reserved

Next Page
Page 1 /