Check statement in java

Control Statements in Java

Control statements are an essential part of any programming language. They allow us to control the flow of our program, determining which lines of code are executed and in what order. In this blog post, we’ll be focusing on control statements in Java.

Java has three main types of control statements:

  1. Conditional statements – These statements allow us to execute a block of code only if a certain condition is met. Java has two types of conditional statements: the if statement and the switch statement.
  2. Loop statements – These statements allow us to execute a block of code multiple times. Java has three types of loop statements: the for loop, the while loop, and the do-while loop.
  3. Transfer statements – These statements allow us to transfer control from one part of your program to another. Java has three types of transfer statements: the break statement, the continue statement, and the return statement.

Let’s take a closer look at each of these control statements in Java.

Conditional statements

Java has two types of conditional statements: the if statement and the switch statement.

The if statement

The if statement allows us to execute a block of code only if a certain condition is true. Here is the general syntax for an if statement in Java:

We can also include an else clause to execute a block of code if the condition is false:

We can also include multiple else if clauses to check for multiple conditions:

if (condition1) < // code to be executed if condition1 is true >else if (condition2) < // code to be executed if condition1 is false and condition2 is true >else < // code to be executed if condition1 and condition2 are false >

The switch statement

The switch statement allows us to execute a block of code based on the value of a variable. Here is the general syntax for a switch statement in Java:

The break statement is used to exit the switch statement once a matching case is found. If we omit the break statement, the code will continue to be executed until the end of the switch statement.

Loop statements

The for loop

The for loop is used to execute a block of code a specified number of times. Here is the general syntax for a for loop in Java:

for (initialization; condition; increment) < // code to be executed >

The initialization section is executed once at the beginning of the loop. It is typically used to initialize a loop variable.

The condition is checked before each iteration of the loop. If the condition is true, the code inside the loop is executed. If the condition is false, the loop is terminated.

The increment section is executed at the end of each iteration of the loop. It is typically used to update the loop variable.

Here is an example of a for loop that counts from 1 to 10:

This for loop will print the numbers 1 through 10 to the console.

The while loop

The while loop is used to execute a block of code as long as a certain condition is true. Here is the general syntax for a while loop in Java:

Читайте также:  Using Where Clause

It’s important to include a way to change the value of the condition inside the loop, or else the loop will become an infinite loop and your program will never end.

The do-while loop

The do-while loop is similar to the while loop, except that it is guaranteed to execute at least once. Here is the general syntax for a do-while loop in Java:

Like the while loop, it’s important to include a way to change the value of the condition inside the loop to prevent infinite loops.

Transfer statements

Java has three types of transfer statements: the break statement, the continue statement, and the return statement.

The break statement

The break statement in Java is used to exit a loop early. It can be used within a while , do-while , or for loop.

Example of using the break statement within a for loop is given below:

for (int i = 0; i < 10; i++) < if (i == 5) < break; >System.out.println(i); > 

In this example, the loop will iterate from 0 to 4, printing each number to the console. When the loop variable i reaches 5, the if statement will evaluate to true and the break statement will be executed, causing the loop to exit and the program to continue with the next statement after the loop.

We can also use label with break statement. Let’s see an example below:

 outerloop: for (int i = 0; i < 3; i++) < innerloop: for (int j = 0; j < 3; j++) < if (i == 1 && j == 1) < break outerloop; >System.out.println("i = " + i + "; j wp-block-preformatted">i = 0; j = 0 i = 0; j = 1 i = 0; j = 2 i = 1; j = 0

Similarly, we can use break statement within a while loop as well. Let’s see an example below:

int i = 0; while (true) < if (i == 5) < break; >System.out.println(i); i++; > 

In this example, the while loop will run indefinitely (since the condition is always true) but the break statement will cause the loop to exit when the variable i reaches 5. This will prevent the program from running indefinitely and causing an infinite loop.

It’s worth noting that the break statement can also be used to exit a switch statement early. When a break statement is encountered within a switch block, the program will exit the block and continue with the next statement after the switch .

The continue statement

The continue statement is used to skip the rest of the current iteration of a loop and move on to the next iteration. When the continue statement is encountered inside a loop, control is transferred to the end of the current iteration and the loop continues with the next iteration.

Let’s take an example of the following code using continue statemtn in for loop:

for (int i = 0; i < 10; i++) < if (i % 2 == 0) < continue; >System.out.println(i); > 

Output of the above code will look like:

In the example code above, the loop iterates from 0 to 9, but the continue statement causes the program to skip over any iteration where i is an even number. This means that only odd numbers will be printed to the console. The if statement with the continue statement inside is used to check if the current iteration variable is an even number, if yes the current iteration will be skipped and next iteration will begin.

We can also use continue statement with label. The example of continue statement with label is given below:

 outerloop: for (int i = 0; i < 3; i++) < innerloop: for (int j = 0; j < 3; j++) < if (i == 1 && j == 1) < continue outerloop; >System.out.println("i = " + i + "; j wp-block-code"> i = 0; j = 0 i = 0; j = 1 i = 0; j = 2 i = 2; j = 0 i = 2; j = 1 i = 2; j = 2 

It’s important to use continue statement properly and in the appropriate context. Using it in the wrong place can lead to unexpected behavior and make the code more difficult to understand.

The return statement

The return statement in Java is used to exit a method and return a value to the calling method. Here’s an example of a simple method that uses the return statement:

public int add(int a, int b)

In the example above, the add method takes two integer parameters, a and b , and adds them together. The result is stored in the variable sum , which is then returned to the calling method using the return statement. This method can be called from another part of the program like this:

And the output of this code will be 5.

The return statement within a for loop

The return statement can be used within a loop in Java to exit the loop early and return a value to the calling method.

Following is an example of a method that uses a for loop and the return statement to search for a specific value in an array:

public int findValue(int[] array, int target) < for (int i = 0; i < array.length; i++) < if (array[i] == target) < return i; >> return -1; > 

In the example above, the findValue method takes two parameters: an array of integers, array , and an integer, target . The method uses a for loop to iterate through the elements of the array and checks if the current element is equal to the target using the if statement. If it finds a match, the method returns the index of the matching element using the return statement. If the target value is not found in the array, the method returns -1.

The return statement within a while loop

Following is an example of a method that uses a while loop and the return statement to search for a specific value in a list:

public boolean containsValue(List list, int target) < int i = 0; while (i < list.size()) < if (list.get(i) == target) < return true; >i++; > return false; > 

In the example above, the containsValue method takes a list of integers and an integer value as a target, it uses a while loop to iterate through the elements of the list and checks if the current element is equal to the target . If it finds a match, the method returns true using the return statement. If the target value is not found in the list, the method returns false .

Conclusion

Control statements are an essential part of programming in Java. They allow us to control the flow of our program, making it more flexible and easier to debug. By using conditional statements, loop statements, and transfer statements, we can create more complex and powerful programs in Java.

Источник

Java if. else Statement

In programming, we use the if..else statement to run a block of code among more than one alternatives.

For example, assigning grades (A, B, C) based on the percentage obtained by a student.

  • if the percentage is above 90, assign grade A
  • if the percentage is above 75, assign grade B
  • if the percentage is above 65, assign grade C

1. Java if (if-then) Statement

The syntax of an if-then statement is:

Here, condition is a boolean expression such as age >= 18 .

  • if condition evaluates to true , statements are executed
  • if condition evaluates to false , statements are skipped

Working of if Statement

if the number is greater than 0, code inside if block is executed, otherwise code inside if block is skipped

Example 1: Java if Statement

class IfStatement < public static void main(String[] args) < int number = 10; // checks if number is less than 0 if (number < 0) < System.out.println("The number is negative."); >System.out.println("Statement outside if block"); > >
Statement outside if block

Note: If you want to learn more about about test conditions, visit Java Relational Operators and Java Logical Operators.

We can also use Java Strings as the test condition.

Example 2: Java if with String

Best Programming Language

In the above example, we are comparing two strings in the if block.

2. Java if. else (if-then-else) Statement

The if statement executes a certain section of code if the test expression is evaluated to true . However, if the test expression is evaluated to false , it does nothing.

In this case, we can use an optional else block. Statements inside the body of else block are executed if the test expression is evaluated to false . This is known as the if-. else statement in Java.

The syntax of the if. else statement is:

Here, the program will do one task (codes inside if block) if the condition is true and another task (codes inside else block) if the condition is false .

How the if. else statement works?

If the condition is true, the code inside the if block is executed, otherwise, code inside the else block is executed

Example 3: Java if. else Statement

class Main < public static void main(String[] args) < int number = 10; // checks if number is greater than 0 if (number >0) < System.out.println("The number is positive."); >// execute this block // if number is not greater than 0 else < System.out.println("The number is not positive."); >System.out.println("Statement outside if. else block"); > >
The number is positive. Statement outside if. else block

In the above example, we have a variable named number . Here, the test expression number > 0 checks if number is greater than 0.

Since the value of the number is 10 , the test expression evaluates to true . Hence code inside the body of if is executed.

Now, change the value of the number to a negative integer. Let's say -5 .

If we run the program with the new value of number , the output will be:

The number is not positive. Statement outside if. else block

Here, the value of number is -5 . So the test expression evaluates to false . Hence code inside the body of else is executed.

3. Java if. else. if Statement

In Java, we have an if. else. if ladder, that can be used to execute one block of code among multiple other blocks.

if (condition1) < // codes >else if(condition2) < // codes >else if (condition3) < // codes >. . else < // codes >

Here, if statements are executed from the top towards the bottom. When the test condition is true , codes inside the body of that if block is executed. And, program control jumps outside the if. else. if ladder.

If all test expressions are false , codes inside the body of else are executed.

How the if. else. if ladder works?

If the first test condition if true, code inside first if block is executed, if the second condition is true, block inside second if is executed, and if all conditions are false, the else block is executed

Example 4: Java if. else. if Statement

class Main < public static void main(String[] args) < int number = 0; // checks if number is greater than 0 if (number >0) < System.out.println("The number is positive."); >// checks if number is less than 0 else if (number < 0) < System.out.println("The number is negative."); >// if both condition is false else < System.out.println("The number is 0."); >> >

In the above example, we are checking whether number is positive, negative, or zero. Here, we have two condition expressions:

Here, the value of number is 0 . So both the conditions evaluate to false . Hence the statement inside the body of else is executed.

Note: Java provides a special operator called ternary operator, which is a kind of shorthand notation of if. else. if statement. To learn about the ternary operator, visit Java Ternary Operator.

4. Java Nested if..else Statement

In Java, it is also possible to use if..else statements inside an if. else statement. It's called the nested if. else statement.

Here's a program to find the largest of 3 numbers using the nested if. else statement.

Example 5: Nested if. else Statement

class Main < public static void main(String[] args) < // declaring double type variables Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largest; // checks if n1 is greater than or equal to n2 if (n1 >= n2) < // if. else statement inside the if block // checks if n1 is greater than or equal to n3 if (n1 >= n3) < largest = n1; >else < largest = n3; >> else < // if..else statement inside else block // checks if n2 is greater than or equal to n3 if (n2 >= n3) < largest = n2; >else < largest = n3; >> System.out.println("Largest Number: " + largest); > >

In the above programs, we have assigned the value of variables ourselves to make this easier.

However, in real-world applications, these values may come from user input data, log files, form submission, etc.

Table of Contents

Источник

Оцените статью