java4
java4
If-else statement: if else statement can be used for executing a group of statements based on a
condition.
Syntax :
if (condition) {
statements1; if block
else {
statements2; else block
satisfied
If the condition is satisfied then it will be executing if block and if the condition is not
then it will be executing else block.
Program 4:
class IfElseDemo {
publicstatic void main(Stringl] args) {
int n = 89;
if (n%2 == 0) {
System.out.println("Even Number");
else {
System.out.printin("Odd Number");
Rule 1: Specifying the condition to if else statement is mandatory and it should be of boolean type
(either a condition or boolean variable or boolean value).
Rule 2: Specifying the else block is optional.
Rule 3: Specifying the ( }is optional. If we don't specify the {}then it will consider only one
statement, and that one statement is mandatory. If we want to consider multiple statements then
Specifying the {} is mandatory. Within the (} we can specify any number of statements.
Rule 4: In if else statement we cannot execute both the blocks and we cannot
skip both the
blocks, it willalways execute exactly one block.