CSCE145 F2026 01 Branching Statements Part 01
Overview
Java branching statements use Boolean conditions to choose which code runs, with flowcharts, if/else syntax, comparison operators, and compound logic clarifying those decisions. A console-based math challenge demonstrates the ideas using Scanner input, string comparisons, Random.nextInt bounds, constants, and Math.pow with a cast.
Key takeaways
- In Java, an if condition must evaluate to a Boolean; if runs its body when true, while a linked else runs only when that condition is false.
- The Boolean operators && and || differ in their truth requirements: AND needs both conditions true, whereas OR needs at least one.
- Use == for primitive integer comparisons, but avoid it for checking String contents; use equals() or equalsIgnoreCase() instead.
- Random.nextInt(bound) includes zero and excludes the bound, so nextInt(100) generates 0 to 99 and nextInt(10) generates 0 to 9.
- Math.pow(base, exponent) returns a double, and casting it to int can discard the fractional part; also keep exponent ranges modest to avoid enormous results.
- A named constant such as RNG_RANGE prevents duplicated magic numbers and lets one edit change multiple random-number bounds.
Chapters
- Flow control determines program action order: branching selects among actions, while loops repeat until a stopping condition.
- Flowcharts use boxes for statements, diamonds for true/false decisions, and arrows to show execution paths.
- An if statement runs its curly-braced body only when its Boolean expression is true; an else requires a preceding if and runs when that condition is false.
- Java if/else statements do not take a trailing semicolon, and curly braces are recommended for their bodies.
- Java comparison operators include ==, !=, <, >, <=, and >=, each producing a Boolean true-or-false result.
- The && operator is true only when both conditions are true; || is true when at least one condition is true.
- A truth table for A and B shows that both AND and OR are false when both inputs are false, while only AND is false when exactly one input is true.
- Negating comparisons reverses their relationship: !(A < B) means A >= B, and !(A == B) means A != B; negating AND and OR swaps the operators.
- The math challenge program imports java.util.Scanner and connects a Scanner named keyboard to System.in.
- The prompt uses the string escape sequence \n for a new line and \" to display literal quotation marks.
- keyboard.nextBoolean() stores the user's true-or-false response in a Boolean variable; if (play) starts the challenge, while else prints a refusal and System.exit(0) stops execution.
- Testing true and false confirms that the selected branch runs and the other branch is skipped.
- The program computes 2 + 3, reads an integer answer with keyboard.nextInt(), and compares it with the result using ==.
- A correct answer prints a win message; an incorrect answer reaches the else branch.
- Integer equality is suitable for this exact addition check, but double values are approximations and can make direct == comparisons unreliable.
- For floating-point comparisons, use a tolerance so values within a chosen plus-or-minus range count as equal; use object methods such as equals() to compare object contents.
- Changing the prompt from true/false to yes/no requires reading a String with keyboard.nextLine() instead of nextBoolean().
- The condition play.equals("yes") returns a Boolean suitable for an if statement and compares string contents rather than object references.
- equals() is case-sensitive, so an input of YES does not match "yes".
- equalsIgnoreCase("yes") accepts uppercase or lowercase variations without changing the intended branch.
- Import java.util.Random and construct a Random object to generate operands instead of assigning fixed values.
- nextInt(100) returns values from 0 through 99: zero is inclusive, while the bound 100 is exclusive.
- A public static final int constant named RNG_RANGE centralizes the random bound, so changing it updates both operand generators.
- The randomized addition examples produce varying problems, such as 85 + 23 and 10 + 6.
- Java's ^ operator is exclusive OR, not exponentiation; Math.pow(num1, num2) performs the power operation.
- Math.pow returns a double, so the example casts its result to int before storing it in an integer variable.
- A bound of 100 permits operands as large as 99, which can produce an impractically large result such as 99 to the 99th power; reducing RNG_RANGE to 10 limits operands to 0 to 9.
- Using RNG_RANGE instead of repeated magic numbers makes one constant change update both random operand calls.
Transcript excerpt
0:01 Hey everyone, and how goes it? Welcome back to Computer Science 145. Today, we're going to talk about branching statements. Flow control is the order in which a program performs its actions. There are two types of flow control elements. One called a branching statement, and another one called loop statements. A branching statement chooses between two or more possible actions, while a loop repeats an action until a stopping condition occurs. As programmers, there's a couple of pieces of advice that I can give to anyone, and one that I use often is when in doubt, draw it out. One way we could do this is using what's called a flowchart. These diagram the flow of a program. And the way that you can think about this
0:48 are boxes are the statements, where diamonds are decisions which have a true and a false branch. Arrows indicate the flow of the statements and their decisions. And pseudo code is mostly used. Now, the essential element of a branching statement is called the if statement. Now, if the boolean expression inside the argument of the if statement is true, then the body of the if statement is executed, but otherwise, it's ignored. We can see over here, this is the syntax of an if statement. And remember, whenever we saw anything inside of parenthesis, this counts as an argument. What an if statement expects is what's
1:35 called a boolean expression. We're going to go into more detail about these later, but essentially, a boolean expression is an expression that yields true or false. So, if this is true, then these statements inside the body, in other words, inside the curly braces of the if statement, are run. Putting curly braces denotes the body of the if statement and I strongly encourage that you use these when you're
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.