java beginner 3

Increment and decrement

Since = in the program is an assignment, it can be self-assigned. I remember (substituting the right side for the left side)!

int x = 5;
x = x + 1;
//x becomes 6

++ is called the increment operator. This is an instruction to add 1 to the value of a variable and assign it. x++;

x = + 1;
//Both are the same, 1 is added to x

--Is called decrement and subtracts one.

Truth value

There is a type called boolean. boolean can be true or false 。 Also called the truth value.

Remember that if it's true, it's true. If it's a lie, it's false. Alternatively, true if the switch is on, false if off

boolean a = true;

Comparison operator

The comparison operator compares the left and right sides and returns a boolean.

==  //equal(Mathematical=This is close to)
!=  //Not equal
>   //Left is larger than right
<  //Left is less than right
>=  //Left is above right
<=  //Left is below right

Conditional branch if

It branches depending on whether the passed expression is true or false. if (conditional expression) {Process to be executed when the condition is true}

If you use else, the process that is executed when false is applied. if (conditional expression) {when the condition is true} else {when the condition is false}

You can use elseif to add another condition when false. if (condition 1) {when condition 1 is true} else if (condition 2) {when condition 1 is false and condition 2 is true} else {when both condition 1 and 2 are false}

Logical operator

Multiple conditions can be combined by using logical operators

! (NOT) Negation Example: When a is true,! A is false

&& (AND) The left and right sides are true (if both are the same) Example (a == 5 && b == 3) true when a is 5 and b is 3.

|| (OR)The left or right side is true(If either is ture, ok) Example(a<5 || b>=3)True when a is less than 5 or b is 3 or more

If there are no parentheses, calculate in the order of NOT AND OR

for statement

You can repeat it using the for statement.

for(int i = 0; i < 5; i++) {
System.out.println(i);
}
System.out.println(0);
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);

Both are the same. for (variable used for Kaoru; condition to repeat; what to do once finished) The inside of the wave bracket is repeated. Counter variables can be used inside.

Nesting for statements

You can also put a for statement inside a for statement.

for (int i = 0; i < 5; i++) {
 for (int j = 0; j < 5; i++) {
  //Inside is repeated 5 times
}
   //When you come here, the outside is over once
}

Be careful not to make the counters the same! (In this example, it will be i and j.)

Let's display the multiplication table!

1,2,3,4,5,6,7,8,9, 2,4,6,8,10,12,14,16 ,,,Continue

Let's write a program that shows the multiplication table!

public class hello {
	public static void main(String[] args) {
		for (int i = 1; i < 10; i++) {
			String a = "";
			for (int j = 1; j < 10; j++) {
				a += i * j + ",";
			}
			System.out.println(a);
		}
		}
	}
1,2,3,4,5,6,7,8,9,
2,4,6,8,10,12,14,16,18,
3,6,9,12,15,18,21,24,27,
4,8,12,16,20,24,28,32,36,
5,10,15,20,25,30,35,40,45,
6,12,18,24,30,36,42,48,54,
7,14,21,28,35,42,49,56,63,
8,16,24,32,40,48,56,64,72,
9,18,27,36,45,54,63,72,81,

It feels like ^^

I think there are other ways, so use the one that suits you ♪

Other repetition

while (condition) {}

while (x < y) {
//Repeated as long as x is less than y
}

If the condition is false from the beginning, it will never be executed

do {} while (condition) Even if the condition is false, it will be executed once for the time being

home work!

Let's make rock-paper-scissors

Goo is 1 Choki is 2 Par is 3 Enter the number of the first hand on the keyboard Enter the number of the second hand on the keyboard Show on the screen which one won and Aiko

Let's write a program!

Recommended Posts

java beginner 4
java beginner 3
java beginner
Java Beginner Exercises
Java Exercise "Beginner"
Java
Java
Progate Java (Beginner) Review & Summary
Java starting from beginner, override
Java, instance starting from beginner
Java starting from beginner, inheritance
[Beginner] Java basic "array" description
Solve AtCoder Beginner Contest 151 in java
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Differences between "beginner" Java and Kotlin
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java string
java (array)
Java serialization
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
Solve AtCoder Beginner Contest 153 in java
[Java beginner] About abstraction and interface
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Polymorphism
Studying Java # 0
Java review
Java features
[Java] Inheritance
FastScanner Java
Java features
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java