1/7
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
For Loop
When you know exactly how many times you want to loop through a block of code
Ex.
for (int i = 0; i < 5; i++) {
System.out.println(i);
}Statement 1 sets a variable before the loop starts: int i = 0
Statement 2 defines the condition for the loop to run: i < 5. If the condition is true, the loop will run again; if it is false, the loop ends.
Statement 3 increases a value each time the code block has run: i++
Output:
0
1
2
3
4While Loop
Loops can execute a block of code as long as a specified condition is true.
Ex.
the code in the loop will run again and again, as long as a variable (i) is less than 5
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}Output:
0
1
2
3
4Conditions and If Statements
let you control the flow of your program - deciding which code runs, and which code is skipped
Conditions:
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to: a == b
Not equal to: a != b
Ex.
if (20 > 18) {
System.out.println("20 is greater than 18");
}If-Else Statements
runs a block of code when the condition in the if statement is false
Ex.
if (20 > x) {
System.out.println("20 is greater than x");
else
System.out.println("20 is less than x");
}Comment
explains what your code is doing
Ex.
// This is a comment
System.out.println("Hello World");Break
terminate a loop (for, while, do-while) or a switch statement
Ex.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exits the loop when i is 5
}
System.out.println(i);
}
// Output:
// 1
// 2
// 3
// 4
Logical Operators
determine the logic between variables or values, by combining multiple conditions
Ex.
&& - and - Returns true if both statements are true
x < 5 && x < 10
|| - or - Returns true if one of the statements is true
x < 5 || x < 4
! - not - Reverse the result, returns false if the result is true
!(x < 5 && x < 10)

String
contains a collection of characters surrounded by double quotes ("")
Ex.
String greeting = "Hello";