Ad Code

Responsive Advertisement

Basic Java Program - 05

Question 05: .
Download hole Program / Project code, by clicking following link:
State the condition when the else part will be executed with example ?
The else part of an if-else construct in Java will be executed when the condition specified in the if statement is false. In other words, if the condition in the if block is not met, the code in the else block will be executed.

Here's an example to illustrate when the else part will be executed:
int score = 55;
if (score >= 60) {
System.out.println("You passed the exam.");
} else {
System.out.println("You did not pass the exam.");
}

In this example, the condition in the if statement is score >= 60. If the value of score is greater than or equal to 60, the condition is true, and the code within the if block will be executed, displaying "You passed the exam."

However, if the value of score is less than 60 (for example, 55, as in the example), the condition is false, and the code within the else block will be executed, displaying "You did not pass the exam."

So, in this case, the else part is executed because the condition in the if statement is not satisfied (score is less than 60).

Which of the following operator used in if:
1. Assignment operator (=)
2. Comparison operator (==)
In Java, the operator used in an if statement for comparison is ==. The operator == is used to check if two values are equal.
  • = is the assignment operator used to assign a value to a variable. It is not used for comparison in if statements. Using = in an if statement would be a syntax error or might not produce the intended results.
Here's how == is used in an if statement to check if two values are equal:
int x = 5;
if (x == 5) {
// Code to execute if x is equal to 5
}

In this example, the if statement checks if the value of x is equal to 5 using the == operator. If the condition is true, the code within the if block will be executed.
Programming Code:
Following code write in: BJ_P03.py
# Java Program # Thanks For Reading.
Output:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement