Java Code

0.0(0)
studied byStudied by 1 person
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/7

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

8 Terms

1
New cards

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
4

2
New cards

While 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
4

3
New cards

Conditions 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");
}

4
New cards

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");
  
}

5
New cards

Comment

explains what your code is doing

Ex.

// This is a comment
System.out.println("Hello World");

6
New cards

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

7
New cards

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)

<p><span>determine the logic between variables or values, by combining multiple conditions</span></p><p><mark data-color="#406726" style="background-color: rgb(64, 103, 38); color: inherit;">Ex.</mark></p><ul><li><p><code>&amp;&amp;</code> - and - Returns true if both statements are true</p><ul><li><p><span>x &lt; 5 &amp;&amp;&nbsp; x &lt; 10</span></p></li></ul></li><li><p><code>||</code> - or - <span>Returns true if one of the statements is true</span></p><ul><li><p><span>x &lt; 5 || x &lt; 4</span></p></li></ul></li><li><p><code>!</code> - not - <span>Reverse the result, returns false if the result is true</span></p><ul><li><p><span>!(x &lt; 5 &amp;&amp; x &lt; 10)</span></p></li></ul></li></ul><p></p>
8
New cards

String

contains a collection of characters surrounded by double quotes ("")

Ex.

String greeting = "Hello";