0% found this document useful (0 votes)
13 views

CH 2

The document discusses various conditional statements in Java including if, if-else, if-else-if ladder, and nested if statements. It provides the syntax and examples of using each statement to check conditions and execute code blocks. The if statement executes code if a condition is true, while if-else executes one of two code blocks based on if the condition is true or false. An if-else-if ladder allows checking multiple conditions and executing matching code. Nested if statements check inner conditions only if outer conditions are true.

Uploaded by

Vivek Bhogayta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

CH 2

The document discusses various conditional statements in Java including if, if-else, if-else-if ladder, and nested if statements. It provides the syntax and examples of using each statement to check conditions and execute code blocks. The if statement executes code if a condition is true, while if-else executes one of two code blocks based on if the condition is true or false. An if-else-if ladder allows checking multiple conditions and executing matching code. Nested if statements check inner conditions only if outer conditions are true.

Uploaded by

Vivek Bhogayta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CHEPTER: 2

SELECTION, MATHEMATICAL FUNCTION &


LOOPS

 JAVA LANGUAGE SPECIFICATION API:

The Java ifstatement is used to test the condition.

o if statement
o if-else statement
o if-else-if ladder
o nested if statement

 Java IF Statement:

The Java if statement tests the condition. It executes the if block if condition is true.

Syntax:

if(condition)
{
//code to be executed
}

1
Example:

public class IfExample


{
public static void main(String[] args)
{
//defining an 'age' variable
int age=20;
//checking the age
if(age>18)
{
System.out.print("Age is greater than 18");
}
}
}

Output:

Age is greater than 18

 Java if-else Statement:

The Java if-else statement also tests the condition. It executes the if block if
condition is true otherwise else block is executed.

Syntax:

2
if(condition)
{
//code if condition is true
}
else
{
//code if condition is false
}

Leap Year Example:

A year is leap, if it is divisible by 4 and 400. But, not by 100.

Public class Leap {


public static void main(String[] args)
{
int year=2020;
if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0))
{
System.out.println("LEAP YEAR");
}
. else{
. System.out.println("COMMON YEAR");
. }
}

3
}

Output:

LEAP YEAR

 Java if-if-else-if ladder Statement:


The if-else-if ladder statement executes one condition from multiple statements.

Syntax:

if(condition1){
//code to be executed if condition1 is true
}
else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}

4
Program to check POSITIVE, NEGATIVE or ZERO:

public class PositiveNegativeExample {


public static void main(String[] args) {
int number=-13;
. if(number>0){
. System.out.println("POSITIVE");
. }
else if(number<0){
. System.out.println("NEGATIVE");
. }else{
. System.out.println("ZERO");
. }
}
}

Output:

NEGATIVE

 Java Nested if Statement:


The nested if statement represents the if block within another if block. Here, the inner if block condition
executes only when outer if block condition is true.

Syntax:

if(condition){
//code to be executed
if(condition){
//code to be executed
}
}

5
Example:

//Java Program to demonstrate the use of Nested If Statement.


public class JavaNestedIfExample {
public static void main(String[] args) {
//Creating two variables for age and weight
int age=20;
int weight=80;
//applying condition on age and weight
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donate blood");
}
}
}}

6
Output:

You are eligible to donate blood

You might also like