Lecture3- Branches Flashcards
What is a Boolean expression?
A statement that evaluates to true or false.
Examples: 4 == 7 → false, 2 < 3 → true, x > 20 (depends on x)
What are relational (comparison) operators in Java?
Operators that compare two values and return true or false.
Operators include: * > (Greater than) * < (Less than) * >= (Greater than or equal to) * <= (Less than or equal to) * == (Equal to) * != (Not equal to) ⚠ Note: = is assignment, == is equality.
What are branches in Java?
Statements that let the program choose between alternative actions based on conditions.
Example: if (temperature > 30) { System.out.println(“It’s hot outside!”); } else { System.out.println(“It’s cool outside.”); }
What is the syntax for an if statement?
if ( condition )
statement;
doesn’t require curly brackets
Example: if (max == 5) { y = x + y; }
What is a block statement in Java?
A group of multiple statements enclosed in {}.
- Indentation alone is not enough! thru it may be for an if statment of there is only one line of code in the body.
Example: if (mouseX < width/2) { fill(255); rect(0, 0, width/2, height); } ⚠ Indentation is NOT enough; use {}!
What is an if-else statement?
Executes one block of code if the condition is true, otherwise executes another.
Example: if (userAge < 25) { insurancePrice = 4800; } else { insurancePrice = 2200; }
What are nested if statements?
An if statement inside another if or else.
Example: if (x > 0) { if (y > 0) { System.out.println(“x and y are positive”); } } ⚠ Braces {} help prevent confusion when using else.
What is a cascading if-else?
A series of if-else conditions that check multiple cases.
Example: if (numYears == 1) { System.out.println(“First year!”); } else if (numYears == 10) { System.out.println(“A decade!”); } else { System.out.println(“Nothing special.”); }
What are Java’s logical operators?
Operators that combine Boolean expressions.
Operators include: * ! (NOT) * && (AND) * || (OR) Example: if (temp > 98.6 || hasRash) { System.out.println(“Go to the doctor!”); }
What does ! (NOT) do?
Reverses a Boolean value.
Example: boolean isRaining = false; System.out.println(!isRaining); // Output: true
How do you compare strings in Java?
Use .equals() for content comparison.
Example: if (str1.equals(str2)) { System.out.println(“Strings are equal”); } ⚠ Do NOT use == for content comparison.
What is the purpose of a switch statement in Java?
It’s an alternative to if statements. The switch statement provides another way to decide which statement to execute next by evaluating an expression and attempting to match the result to one of several possible cases.
What is the general syntax of a switch statement?
```java
switch (expression) {
case value1:
statement-list1;
break;
case value2:
statement-list2;
break;
case value3:
statement-list3;
break;
default:
statement-list4;
break;
}
~~~
What is the purpose of the break
statement in a switch statement?
The break
statement causes control to transfer to the end of the switch statement. Without it, the flow of control will continue into the next case.
What happens if a break statement is omitted from a case in a switch statement?
If a break statement is omitted, the flow of control continues into the next case, which may be appropriate in certain situations but is typically avoided to ensure only the statements of one case are executed.
What is the role of the default
case in a switch statement?
The default
case has no associated value and is executed if no other case value matches. If no default
case exists and no values match, control falls through to the statement after the switch.
Can a switch expression be any type?
A switch expression can be a byte, short, char, int, or a String. It cannot be a boolean, float, or double. Relational expressions cannot be used with a switch statement.
What is a string in Java?
A string is a sequence of characters in memory. Each character in the string has an index, starting with 0.
How are two strings considered equal in Java?
Two strings are equal if:
- Both strings have the same number of characters.
- Each corresponding character is identical.
- Case matters (e.g., “Book” ≠ “book”).
What does str1.equals(str2)
do in Java?
It returns true if str1
and str2
have the same number of characters and the same case.
How does the ==
operator compare strings in Java?
The ==
operator compares the memory addresses of the strings, not their contents.
What does str1.compareTo(str2)
return?
It returns:
- A negative number if str1
is lexicographically less than str2
.
- 0 if str1
is equal to str2
.
- A positive number if str1
is lexicographically greater than str2
.
How do you compare strings while ignoring case in Java?
Use str1.equalsIgnoreCase(str2)
or str1.compareToIgnoreCase(str2)
.
What does str1.length()
return?
It returns the number of characters in the string str1
.