Friday, 16 March 2018

Java if statement in android

Java if statement in android

Java If statement is used for check conditions.

If condition check the block of code if condition is true of false.

If statement always return true of false.

When we use java if statement in android

       When you want to check a condition like if person age is below 18 he/she is children and if person age is above 18 and below 50 he/she is young otherwise he/she is old.

       In a programming language if statement is used for check alternate options like true of false.

Syntax :

if(condition)
{
         //True statement
}
else
{
        //False statemtn
}

Example

String str="Darvin";
if(str.equals("Darvin"))
{
    Toast.makeText(getApplicationContext(),"true",Toast.LENGTH_SHORT).show();
}
else{
    Toast.makeText(getApplicationContext(),"false",Toast.LENGTH_SHORT).show();
}

Output

true

Java if,else if and else statement in android

String str="Darvin";
if(str.equals("Darvin"))
{
    Toast.makeText(getApplicationContext(),"Darvin",Toast.LENGTH_SHORT).show();
}
else if (str.equals("Info"))
{
    Toast.makeText(getApplicationContext(),"Info",Toast.LENGTH_SHORT).show();
}
else{
    Toast.makeText(getApplicationContext(),"Default",Toast.LENGTH_SHORT).show();
}

Output

Darvin


Share This

0 comments: