قالب وردپرس درنا توس
Home / Tips and Tricks / How to reuse statements in Java

How to reuse statements in Java



Night keyboard coding

When I first learned to program, it took on statements that helped it all clicked into place for me. When you can use if statements, you can build all kinds of useful and interesting tools. In addition, this is a basic programming structure that you will use in almost any language.

In this post, we explore how to use if statements in Java.

See also: How to reuse statements in Python

Understand statements in Java

So, what is an if statement in Java?

Essentially, an “if statement” is what you use for “flow control.” If statements allow branch code that allows programs to respond dynamically to inputs.

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

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

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

Or:

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

This can let you control a player’s health in a computer game (IF (enemy’s bullet hits the player) THEN (reduce health by one point)). It is also how you would end the game when the player runs out of health (IF (health is zero) THEN (show the game across the screen)).

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

How to write about statements in Java

So, what does the actual Java code look like?

In Java we do not need to write “DANNA”. Instead, we use an open curly mount to say that everything that follows is part of that “code block” and should be run under the conditions we specify. We then close the code block with the opposite curly bracket when we want to return to the main flow of the code.

Like the pseudocode example I showed earlier, we still place the test expression in parentheses. However, this will be written using operations to compare values.

For example:

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

Here, as long as health equals zero, we will see the words “Game Over!” on the screen, otherwise we will not. “Health” is a variable, which means that it can store a value. Check out our latest post to learn more about variables in Java.

Encapsulated statements in Java

There are many other tricks you can perform when you understand how to use if statements in Java.

For example, you can also use “nested if statements.” This means that you have an “if in a if” and thereby can create a large number of conditions that are required for a certain action to take place.

You make this as easy as you might hope; by placing an if statement within the code block for another if statement. You can accommodate as many as you want, just try not to lose track and remember to close the correct number of parentheses!

If you find yourself with an awkward number of these statements, you may want to use another method, such as Switch.

And + or

Alternatively, you can set several conditions in the original statement with “AND” and “OR”. Again, these work as you would expect. When you use and can use two separate conditions that must be required for the code to run. When you use or your code block will be re-run either the condition is met.

In Java, and is represented by two characters (&&) and or is represented by two vertical lines (||). These are in parentheses to separate your test statements, 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 statements in Java

The last thing we are going to look at today is how to use “other” statements. Else works with if statements in Java by defining what should happen if the condition is not met.

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

For example:

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

We can also use an “other if” that tests a new condition but only if the previous condition turns out to be untrue.

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 these as we want!

Closing comments

So there you have it: this is how to reuse statements in Java!

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


Source link