Studying Java-Part 10-if statement

1.First of all

Well, from here it becomes like programming.

Programming shines with control statements! !! One of the control statements! !! Let's study the very important "** if statement **"! !!

2. if statement

"** if statement **" is the flow of processing,

"If ..." do the following "If not" Do not do the next

The process is branched according to the conditions.

For example, in the real world

"If it rains" → "Hold an umbrella" "If you want to eat rice" → "Cook rice" "If you want to eat pasta" → "Boil pasta"

It's like that. Many if statements are hidden in everyday life.

2.1. if statement-basic

Then! !! !! Let's actually look at the basic syntax! !!

Main.java


public class Main {
	public static void main(String[] args) {

		//↓ If statement starts here
		if (Conditional expression) {

		}
		//↑ This is the end of the if statement
	}
}

Writing an if statement is simple, isn't it? if (conditional expression) {processing content} is the standard! !!

Write the conditional expression you want to cause a conditional branch in the place where it is written as a conditional expression. The result of the conditional expression is either "true" or "false".

2.2. if statement-application

I'll give you a little bit of reality.

For example,

Main.java


public class Main {
	public static void main(String[] args) {

		//Current weather conditions
		String weather = "rain";

		//Determine what the current weather is like
		if (weather.equals("rain")) {
			//It's raining Let's output a comment
			System.out.println("It's raining today. I have to bring an umbrella");
		}

	}
}

There is such a source.

To explain the process,

A variable called "weather" that represents the current weather conditions has been declared and initialized with "rain".

Then I entered the if statement, which is the heart. The conditional expression here is

「weather.equals("rain")」

something like. Is this the same character string that "weather" has as "" rain ""? It is a conditional expression.

If they are the same, they are replaced with "true", and if they are not the same, they are replaced with "false". "True" is true, which means that the condition was met "False" is false, meaning that the condition was not met

As an image, if (weather.equals("rain")) { ... }  ↓ if (true) { ... } It's like that.

When the conditional expression of the if statement becomes "true", the processing inside {} is performed! This time, the output was "It's raining today. I have to bring an umbrella."

Roughly summarized If the first condition "weather.equals (" rain ")" is met,

Output "It's raining today. I have to bring an umbrella"


And for example If you enter "" Fine weather "" in the initialization of "weather", ...

Main.java


public class Main {
	public static void main(String[] args) {

		//Current weather conditions
		String weather = "Fine weather";

		//Determine what the current weather is like
		if (weather.equals("rain")) {
			//It's raining Let's output a comment
			System.out.println("It's raining today. I have to bring an umbrella");
		}

	}
}

Nothing comes out. lonely. It's also important to note that the if statement didn't apply and the "System.out.println (" It's raining today. You have to bring an umbrella ");" wasn't processed! !!

3. if --other statement

Next is the "** if --el e **" statement.

this is, if Perform "if" processing else Perform processing "if there is something that does not apply to if"

It would be nice if you could have an image of else attached to an if statement such as. The else statement will always pick up any conditions that do not apply to the if statement.

3.1. if --other statement --basic

The basic syntax is

Main.java


public class Main {
	public static void main(String[] args) {

		// ↓if -else statement from here
		if (Conditional expression_1) {
			//Conditional expression_What happens if 1 is true
		} else {
			//What happens when you don't hit the if statement
		}
		// ↑if -This is the end of the else statement

	}
}

3.2. if --other statement --application

For example

Main.java


public class Main {
	public static void main(String[] args) {

		//Current weather conditions
		String weather = "Fine weather";

		//Determine what the current weather is like
		if (weather.equals("rain")) {
			//It's raining Let's output a comment
			System.out.println("It's raining today. I have to bring an umbrella");
		} else {
			//It seems that the above conditions were not met.
			System.out.println("It looks like good weather today. Roomba Roomba");
		}

	}
}

This program is the same as before

To explain,

If the first condition "weather.equals (" rain ")" is met,

"It's raining today. I have to bring an umbrella."

First, if the conditions are not met

Output "It looks like good weather today. Lun Lun Rumba"

I think it's like that.

4 if --el e if --el e statement

One last thing about the if statement! The important thing! This is called the "** if --el e if --el e **" statement. This is the case when there are multiple conditions.

For example "If it's sunny, I'll do it" "If it's not sunny and it's cloudy, I'll do ..." "If it's raining, not sunny or cloudy, I'll do it." "Otherwise withdrawal" Let's think about the conditions like this.

4.1. if --other if --else statement --basic

First of all, how to write an expression

Main.java


public class Main {
	public static void main(String[] args) {

		// ↓if - else if -else statement from here
		if (Conditional expression_1) {
			//Conditional expression_What happens if 1 is true
		} else if(Conditional expression_2){
			//Conditional expression_What happens if 2 is true
		} else {
			//Processing performed when the above conditions are not met
		}
		// ↑if - else if -This is the end of the else statement

	}
}

It's a little messy. ..

4.2. if --other if --else statement --application

For example

Main.java


public class Main {
	public static void main(String[] args) {

		//Current weather conditions
		String weather = "Fine weather";

		//Determine what the current weather is like
		if (weather.equals("Fine weather")) {
			//It's sunny
			System.out.println("It's sunny today. Isn't it a sunny day with no cloudiness? Let's go play!");
		} else if (weather.equals("cloudy")) {
			//It's cloudy
			System.out.println("It's cloudy today! But it doesn't matter! Let's go play! !!");
		} else if (weather.equals("rain")) {
			//It's raining
			System.out.println("It's raining today. I need an umbrella, but let's go play! !!");
		} else {
			//The above conditions were not met
			System.out.println("I'm not sure about the weather today. Roomba Roomba, let's go play! !!");
		}

	}
}

To explain, If the first condition "weather.equals (" Fine weather ")" is met,

"It's sunny today. It's a sunny day with no cloudiness! Let's go play!"

If the first condition does not match and the second condition "weather.equals (" cloudy ")" matches,

"It's cloudy today! But it doesn't matter! Let's go play !!"

If the first and second conditions do not match and the third condition "weather.equals (" rain ")" matches,

"It's raining today. I need an umbrella, but let's go play !!"

If all the first, second and third conditions are not met,

"I don't know the weather today. Lun Lun Rumba, let's go play !!"

I made a program like this.

In the above program, "weather" is initialized with "" Fine weather "", so The first conditional expression should match, and you should see "It's sunny today. It's a sunny day with no cloudiness! Let's go play!"

If there is a caveat, once the condition is matched somewhere, the subsequent judgment of if --el e or if --el e --if statement is skipped! If you want to match with other conditions, you need to devise. It's a little careful.

I'm sure some of you have already done this, but if you change the value of "weather" to "cloudy", "rain", "koukonko", etc., the result will change and it will be interesting!

5. Conclusion

Thank you for reading this time as well! !! !! !! I hope you can read just the title! !!

The story changes a little, but I would like you to take some time to explain the conditional branching. I'm thinking of doing it slowly and slowly. I would be happy if you could get along with me!

Next time, I would like to explain about "switch statements". Noshi

Next time → "Study Java-Part 11-switch statement"

Recommended Posts

Studying Java-Part 10-if statement
Studying Java-Part 11-switch statement
if statement
Studying Java-Part 0-Overview
Studying Java-Part 4-Literal
Studying Java-Part 5-Constant
Studying Java-Part 2-Variables
Studying Java-Part 9-Scope
Studying Java-Part 7-Array
Studying Java-Part 3-Type
About Ruby if statement
10 Corresponds to if statement
Studying Java-Part 1-Hello World
About for statement and if statement
Let's understand the if statement!
Points for refactoring (if statement)
[Ruby] problem with if statement
[Ruby] conditional branch if statement
Java study # 4 (conditional branching / if statement)
About if statement and branch processing
Java, if statement / switch statement starting from beginner
[Ruby] if statement concept of conditional expression
Basics of java basics ② ~ if statement and switch statement ~