CST 3513
Purpose of If Statements
Conditional Execution: If statements facilitate the validation or satisfaction of conditions.
Control Flow: They determine the path of execution based on boolean conditions (true or false).
Structure of If Statements
Conditional statement syntax:
if (condition) { // code to execute if condition is true }
else { // code to execute if condition is false }
Using Else If
The else if ladder expands the control structure to allow for multiple conditional branches:
Syntax:
if (condition1) { // code for condition1 } else if (condition2) { // code for condition2 } else { // code if all conditions are false }
Logical Operators
Relational Operators: Used to compare variables and return boolean values.
Greater than (
>), Less than (<), Equal to (==), Not equal to (!=)Greater than or equal to (
>=), Less than or equal to (<=)
Logical Operators: Used to combine multiple conditions.
AND(&&): true if both conditions are true.OR(||): true if at least one condition is true.NOT(!): inverts the truth value.
Example of Logical Operators
AND vs. OR:
Example:
if (conditionA && conditionB) { // code executed if both conditions are true } if (conditionA || conditionB) { // code executed if at least one condition is true }
Truth Table for Logical Operators
Understanding how the logical operations produce results: | Condition A | Condition B | A && B | A || B | |-------------|-------------|--------|--------| | true | true | true | true | | true | false | false | true | | false | true | false | true | | false | false | false | false |
Using If Statements in Java
Importing Libraries for User Input:
import java.util.Scanner;The
Scannerclass allows capturing user inputs from the keyboard.Example:
Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); int userInput = input.nextInt();
Switch Statements
Switch vs. If-Else:
Use switch statements for clean multi-way branching based on distinct cases.
Syntax:
switch (variable) { case value1: // execute code block break; case value2: // execute code block break; default: // execute if no cases match }Each case must have a unique value.
The break statement prevents fall-through.
Increment and Decrement Operators
Increment:
++(increases value by 1)Decrement:
--(decreases value by 1)Can be used either as postfix (
value++) or prefix (++value) based on the requirement of when the increment/decrement takes place.
Loops in Java
Types of Loops:
While Loop: Executes code as long as a specified condition is true.
while (condition) { // code to execute }Do-While Loop: Similar to the while loop, but guarantees at least one execution.
do { // code to execute } while (condition);For Loop: Used for iterating over a range of values.
for (initialization; condition; increment) { // code to execute }
Comparison of Strings
Strings are not compared using relational operators due to their reference nature. Instead, the compareTo method is utilized:
if (string1.compareTo(string2) < 0) { // string1 is less than string2 } else if (string1.compareTo(string2) > 0) { // string1 is greater than string2 } else { // strings are equal }Optional methods for case-insensitive comparisons:
equalsIgnoreCase,compareToIgnoreCase.