قالب وردپرس درنا توس
Home / Tips and Tricks / Use of if statements in Java

Use of if statements in Java

Coding of the night keypad

When I first learned to program, it was clear whether statements that had helped me fit with me. Once you can use if statements you can create all kinds of useful and interesting tools. In addition, it is a basic programming structure that you will use in almost any language.

This post will show you how to use if statements in Java.

See also: Using if statements in Python

Understanding if statements in Java

What is an if statement in Java?

Essentially, you use an “if statement” for “flow control”. If statements provide branch code that programs can use to respond dynamically to input.

The great thing about if statements is that they do exactly what they say and work as expected. In the “pseudocode” you are essentially saying:

“IF (something happens) THEN (do something else)”

“IF (something happens) THEN (do nothing)”


“IF (something happens) AND (something else happens) THEN (do something)”

This way you can control a player’s health in a computer game (IF (enemy bullet hits player) THEN (reduce health by one point)). It’s also like how you would quit the game if the player ran out of health (IF (health is zero) THEN (show game above screen)).

You can even use if statements to control the character: IF (player presses the right arrow) THEN (move player to the right).

How do I write if statements in Java?

What does that look like in the actual Java code?

In Java we don’t have to write “THEN”. Instead, we use an open curly bracket to say that everything that follows is part of that “block of code” and should be performed under the conditions we specified. We then close the block of code with the opposite curly bracket when we want to return to the main flow of code.

Just like in the pseudocode example I showed earlier, we’re still going to enclose the test statement in parentheses. However, this is written using value comparison operations.

For example:

int health = 2;
if (health == 0) {
  System.out.println("Game Over!");

As long as the health is zero, we see the words “Game Over!” on the screen, otherwise we won’t. “Health” is a variable, which means it can store a value. In our last post, you can learn more about variables in Java.

Nested if statements in Java

There are many other tricks you can do once you know how to use if statements in Java.

For example, you may also want to use “nested if statements”. This means that you have an “if within an if” and thereby can create a variety of conditions necessary for a particular action to be taken.

You do this as easily as you might hope; by placing an if statement within the code block of another if statement. You can nest as many if statements as you want. Just try not to lose track and remember to close the correct number of brackets!

When faced with an unwieldy number of these instructions, you may want to use another method, such as: B. Switch.

And + or

Alternatively, you can specify several conditions with “AND” and “OR” in the original statement. These also work as expected. Using and allows you to use two separate conditions that must be required for your code to run. If you use or, your block of code will run when either Condition is fulfilled.

In Java and is represented by two ampersands (&&) and / or by two vertical lines (||). These are in the parentheses to separate your test instructions, and you can use as many as you want.

For example:

int health = 2;
int time = 0;

if (health == 0 || time == 0) {
  System.out.println("Game Over!");

Otherwise instructions in Java

The last thing we’re going to look at today is the use of “else” statements. Otherwise, Java uses if statements to define what happens when the condition is met Not met.

We use the else keyword immediately after the closing bracket and then open another one

For example:

if (health == 0 || time == 0) 
  System.out.println("Game Over!");
} else {
  System.out.println("Game On!"");

We can also use an “else if” which tests a new condition, but only if the previous condition turns out not to be true.

For example:

if (health == 0 || time == 0) 
  System.out.println("Game Over!");
} else if (health == 1) {
  System.out.println("Low health!"");

Again, we can use as many of them as we want!

Close comments

So there you have it: How to Use Java If Statements!

With that understanding, you can now create dynamic, changing, and interactive apps! To see how this fits into the bigger picture, be sure to check out our free and comprehensive Java Beginner’s Guide. Or why not master the language in its entirety by trying some of the best free and paid resources for learning Java?

Source link