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
.
What does str1.concat(s2)
do in Java?
It creates a new string by appending s2
to the end of str1
.
What does str1.charAt(indexNumber)
do?
It returns the character at the specified index of the string str1
.
What does str1.indexOf(item)
return?
It returns the index of the first occurrence of item
in str1
. If item
is not found, it returns -1.
What does str1.lastIndexOf(item)
do?
It returns the index of the last occurrence of item
in str1
. If not found, it returns -1.
What does str1.substring(startIndex)
do?
It returns a substring starting from startIndex
to the end of the string.
What does str1.substring(startIndex, endIndex)
do?
It returns a substring starting at startIndex
and ending at endIndex - 1
.
What does str1.replace(findStr, replaceStr)
do?
It returns a new string where all occurrences of findStr
are replaced with replaceStr
.
What is a method in Java?
A method is a group of statements that perform a specific task. Methods have a name followed by parentheses ()
. The values inside the parentheses are called parameters.
What is Math.pow(a, b)
used for?
Math.pow(a, b)
is a method to compute ‘a’ raised to the power of ‘b’.
What does Math.random()
do in Java?
Math.random()
provides a random number.
What categories are mathematical methods in the Math class organized into?
The Math class methods are categorized as:
- Trigonometric methods
- Exponent methods
- Service methods
Do you need to import the Math class in Java?
No, the Math class is in the java.lang
package and is implicitly imported into all Java programs.
What is an example of a useful constant provided by the Math class?
An example is Math.PI
, which provides the value of Pi.
what does the Math.random() do?
returns a double random number between 0 (inclusive) and 1
(exclusive).
how do we modify the Math.random() command
a + (Math.random() * b);
returns a random double betwen a and (a+b)-1
Ex.
Math.random() * 10 will return a double between 0.000 to 9.0000,
Math.random() * 10 +1 will give 1-10
If you want an integer, just type cast the whole thing
What does import java.util.Random;
allow you to do?
It provides the ability to create a Random object and use all its properties.
What is the default behavior of a Random object for seeding?
By default, it uses a number based on the current time as the seed, creating a different sequence with each program run.
How do you create a Random object in Java?
Use Random rand = new Random();
.
How do you set a specific seed for a Random object?
Use the setSeed()
method, e.g., rand.setSeed(5);
.
How do you generate a random number within a specific range in Java?
Use the nextInt()
method, e.g., rand.nextInt(25);
(returns a value from 0-24).
How do you get a random number in the range 10-15?
Use rand.nextInt(6) + 10;
.
What is the char
data type used for in Java?
The char
data type is used to hold a single character.
How is a char
stored in Java?
A char
is stored as a number using a character encoding, such as ASCII or Unicode.
Can relational operators be used on char
data in Java?
Yes, relational operators can be used on char
data, as each character has a unique numeric Unicode value.
What is the result of the following code?
```java
char a = ‘a’;
System.out.println(++a);
~~~
The result is b
.
Can a char
be cast into a numeric type in Java?
Yes, a char
can be cast into any numeric type, and vice versa. When a char
is cast into a numeric type, its Unicode value is used.
What does indexOf(ch)
do?
Returns the index of the first occurrence of ch
in the string. Returns -1
if not matched.
What does indexOf(ch, fromIndex)
do?
Returns the index of the first occurrence of ch
after fromIndex
in the string. Returns -1
if not matched.
What does indexOf(s)
do?
Returns the index of the first occurrence of string s
in this string. Returns -1
if not matched.
What does indexOf(s, fromIndex)
do?
Returns the index of the first occurrence of string s
in this string after fromIndex
. Returns -1
if not matched.
What does lastIndexOf(ch)
do?
Returns the index of the last occurrence of ch
in the string. Returns -1
if not matched.
What does lastIndexOf(ch, fromIndex)
do?
Returns the index of the last occurrence of ch
before fromIndex
in this string. Returns -1
if not matched.
What does lastIndexOf(s)
do?
Returns the index of the last occurrence of string s
. Returns -1
if not matched.
What does lastIndexOf(s, fromIndex)
do?
Returns the index of the last occurrence of string s
before fromIndex
. Returns -1
if not matched.
What is a text box used for in programming?
A text box is used to display multiple lines of text.
How is a multi-line text box defined in programming?
A multi-line text box starts with three double quotations ("""
), followed by optional whitespace and a line terminator. The last line ends with three double quotations ("""
) again.
How does indentation work in multi-line text boxes?
Indentation can be used in a multi-line text box, and the compiler removes the maximum common left space characters from all the lines.
What happens to the right trailing spaces in a multi-line text box?
Right trailing space is trimmed by default. To keep the right trailing space, the escape character \s
should be used.
Can other escape characters be used in a multi-line text box?
Yes, other escape characters can also be used in a multi-line text box.