public class HelloWorld
{
public static void main(Strings args[])
{
System.out.print("Hello World");
}
}
public class Example {
public static void main(String[] args) {
// Statements within the method body
System.out.println("Hello, "); // This will be printed in one line
System.out.print("Tim");
}
}
Conditional logic in Java involves making decisions based on certain conditions. The if-then statement is the most basic control flow statement, allowing execution of a code block only if a specific condition evaluates to true.
if (isAlien == false) {
System.out.println("It is not an alien!");
}
Explanation:
Conditional operators are used to compare values and determine whether conditions are met.
isAlien = false; // Assigning the value 'false' to the variable 'isAlien'
if (isAlien == false) // Checking if 'isAlien' is equal to 'false'
System.out.println("It is not an alien!");
The placement of semicolons and the use of code blocks impact the behavior of the if-then statement.
if (isAlien == false); // Incorrect use of semicolon
System.out.println("It is not an alien!");
Explanation:
Code blocks enhance the clarity and functionality of conditional statements.
if (isAlien == false) {
System.out.println("It is not an alien!");
System.out.println("Welcome, Earthling!");
}
Explanation:
The if-then statement is a fundamental building block in Java programming for making decisions based on conditions.
if (isAlien == false) {
System.out.println("It is not an alien!");
System.out.println("Welcome, Earthling!");
} else {
System.out.println("It is an alien!");
System.out.println("Greetings, Extraterrestrial!");
}
int topScore = 80;
int secondTopScore = 81;
if (topScore > secondTopScore && topScore < 100) {
System.out.println("Greater then second top score and less than 100")
}
if (topScore != 100) {
// Code block executed
}
if (topScore > 100) {
// Code block executed
}
if (topScore >= 100) {
// Code block executed
}
if (topScore < 100) {
// Code block executed
}
if (topScore <= 100) {
// Code block executed
}
Operator precedence determines the order in which operators are evaluated within an expression.
Operators with higher precedence are evaluated before those with lower precedence.
Examples of common operator precedence:
/, Modulus %-Using parentheses can modify precedence and ensure desired evaluation order.
Understanding operator precedence is essential for writing accurate expressions.
In the previous video, we introduced the logical AND operator (&&), requiring both conditions to be true.
Now, let's delve into the logical OR operator (||), which works similarly, but requires at least one condition to be true.
Example:
if (condition1 || condition2) {
// Code block executed if either condition is true
}
Bitwise operators & and | exist, but they are used less frequently.
Let's put the logical OR operator into action with a new code example.
Here's an example demonstrating the logical OR operator.
Given topScore is 80, and secondTopScore is 81:
if (topScore > 90 || secondTopScore <= 90) {
// Code block executed if either condition is true
}
The left condition (topScore > 90) is false.
The right condition (secondTopScore <= 90) is true.
Since at least one condition is true, the code block will be executed.
Let's conclude this topic and preview what's coming next.
Consider comparing a variable to a boolean value.
boolean isCar = false;
if (isCar = true) { // Incorrect use of assignment operator
System.out.println("This is not supposed to happen");
}
// although we are using assignment operator but still the value is printed that's because after successfully assigning of the value it returns 'true'.
Explanation:
Fixing the issue:
boolean isCar = false;
if (isCar == true) { // Use '==' for comparison
System.out.println("This is not supposed to happen");
}
Shortcuts:
Understanding the difference between the assignment and equality operators is crucial.
Always use == when comparing values, and = only for assignments.
Shortcut usage is recommended for more concise and readable code.
Next, we'll explore the ternary operator in the upcoming video.
Conditional operator with three operands.
Structure: operand1 ? operand2 : operand3
Test if operand1 is true, return operand2; otherwise, return operand3.
String makeOfCar = "Volkswagen";
boolean isDomestic = (makeOfCar.equals("Volkswagen")) ? false : true;
System.out.println("Is the car domestic? " + isDomestic);
int ageOfClient = 20;
String ageText = (ageOfClient >= 18) ? "Over 18" : "Still a kid";
System.out.println("Age: " + ageText);
Replaces if-else for concise conditional assignments.
Structure: operand1 ? operand2 : operand3
Operator precedence determines the order in which operators are evaluated within an expression.
Operators with higher precedence are evaluated before those with lower precedence.
Examples of common operator precedence:
/, Modulus %-Using parentheses can modify precedence and ensure desired evaluation order.
Understanding operator precedence is essential for writing accurate expressions.
Showcases priority of evaluating expressions
Precedence | Operation
----------------------
15 | Parentheses
14 | Unary operators (positive, negative, increment, decrement, logical NOT, bitwise NOT)
13 | Multiplicative operators (*, /, %)
12 | Additive operators (+, -)
11 | Shift operators (<<, >>, >>>)
10 | Relational operators (<, >, <=, >=, instanceof)
9 | Equality operators (==, !=)
8 | Bitwise AND (&)
7 | Bitwise XOR (^)
6 | Bitwise OR (|)
5 | Logical AND (&&)
4 | Logical OR (||)
3 | Ternary operator (condition ? expr1 : expr2)
2 | Assignment operators (=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=)
1 | Comma operator (,)