//If else Statement:-
- //the java if-else statement also tests the condition.
- //the condition after evaluation of if statement will be either true or false.
- //If this condition is true then the block of the statement connected to the if statement is executed.
- //If this condition is false then the block of the statement connected to the else statement is executed.
- synatx of If else statement.
if (condition)
{
// block of code to be executed if the condition is true
}
else
{
// block of code to be executed if the condition is false
}
====================================================================
//Program I:-
package controlStatements;
public class If_else_use
{
public static void main(String[] args)
{
//if my marks is greater than or equal to 35 then i am pass else i am fail.
int marks=35;
if (marks>=35)
{
System.out.println("I Am Pass");
}
else
{
System.out.println("I Am Fail");
}
}
}
//Output:-
I Am Pass
====================================================================
//Program
II:-
package controlStatements;
public class If_else_use
{
public static void main(String[] args)
{
//if my marks is greater than or equal to 35 then i am pass else i am fail.
int marks=15;
if (marks>=35)
{
System.out.println("I Am Pass");
}
else
{
System.out.println("I Am Fail");
}
}
}
//Output:-
I Am Fail
====================================================================
//Program III:-
package controlStatements;
public class If_else_use
{
public static void main(String[] args)
{
//If It is summer then temp is greater than 40 else temp is less than 40.
String season="Winter";
if (season=="summer")
{
System.out.println("Temp is greater than 40");
}
else
{
System.out.println("Temp is Less than 40");
}
}
}
//Output:-
Temp is Less than 40
====================================================================
//Program IV:-
package controlStatements;
public class If_else_use
{
public static void main(String[] args)
{
//If It is summer then temp is greater than 40 else temp is less than 40.
String season="summer";
if (season=="summer")
{
System.out.println("Temp is greater than 40");
}
else
{
System.out.println("Temp is Less than 40");
}
}
}
//Output:-
Temp is greater than 40
====================================================================
//Program V:-
package controlStatements;
public class If_else_use
{
public static void main(String[] args)
{
//3.If my grade is A , i got above 90% else i got below 90%.
char grade='B';
if (grade=='A')
{
System.out.println("I Got Above 90%");
}
else
{
System.out.println("I Got Below 90%");
}
}
}
//Output:-
I Got Below 90%
====================================================================
//Program VI:-
package controlStatements;
public class If_else_use
{
public static void main(String[] args)
{
//3.If my grade is A , i got above 90% else i got below 90%.
char grade='A';
if (grade=='A')
{
System.out.println("I Got Above 90%");
}
else
{
System.out.println("I Got Below 90%");
}
}
}
//Output:-
I Got Above 90%
====================================================================
0 Comments