Java Conditional Statements
Conditional statements are used to insert verification points in addition to for Error handling.
Two types of Conditional statements inward Java
1) if statement
2) switch statement
------------------------------
Types of Conditions
1) Single condition
Ex:
if (a > b) {
----
----
2) Compound status
Ex:
if ((a > b) && or || (a > c)) {
--------
----------
3) Nested Condition
if (a > b){}
if (a < c) {}
if (a < d) {
----
----
}
------------------------------
Usage of Conditional Statements
1) Executing a block of statements when status is true.
Syntax:
if (Condition) {
Statements
---------
---------
}
Example:public static void original (String [] args){
int a, b;
a =10;
b =5;
if (a > b) {
System.out.println("A is a Big Nuber");
}
------------------------------------
2) Executing a block of statements when chemical compound status is true
Syntax:
if ((condition) && (condition2)) {
Statements
----------
-----------
}
Example:int a, b, c = 2;
a =10;
b =5;
if ((a > b) && (a > c)) {
System.out.println("A is a Big Nuber");
}
-------------------------------------------
3) Executing a block of statements when status is true. otherwise executing simply about other block of statements.
Syntax:
if (Condition) {
Statements
----------
--------
}
else {
statements
---------
--------
}
Example:
public static void original (String [] args){
int a, b;
a =10;
b =50;
if (a > b) {
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}
-----------------------------------------
4) Decide amid several Alternates (else if structure)
Syntax:
if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else
{
statements
----------
--------
}
Example:
public static void original (String [] args){
int a = -100;
if ((a >= 1) && (a < 100)) {
System.out.println("A is a Small Number");
}
else if ((a > 100) && (a <= 1000)) {
System.out.println("A is a Medium Number");
}
else if (a > 1000) {
System.out.println("A is a Big Number");
}
else
{
System.out.println("A is either Zero or negative value");
}
--------------------------------------------
5) Executing a block of statements when to a greater extent than than i status is truthful (Nested if).
Syntax:
if (condition) {}
if (condition) {}
if (condition) {
Statements
--------
---------
}
else
{
Statements
---------
---------
}
Example:
public static void original (String [] args){
int a =10, b =7, c = 5, d = 13;
if (a > b){}
if (a > c) {}
if (a > d){
System.out.println("A is a Big Number");
}
else {
System.out.println("A is Not a Big Number");
}
---------------------------------------
6) Decide amid several alternates (using Switch instance structure).
Syntax:
switch (expression) {
case value:
Statements
-------
break;
case value:
Statements
-------
break;
case value:
Statements
-------
break;
default
Statements
-------
------
}
Example:char shape = 'X';
switch (grade){
instance 'A':
System.out.println ("Excellent");
break;
instance 'B':
System.out.println ("Well Done");
break;
instance 'C':
System.out.println ("Better");
break;
default:
System.out.println("Invalid Grade");
}
---------------------------------------------------------
Sumber http://www.gcreddy.com/
Conditional statements are used to insert verification points in addition to for Error handling.
Two types of Conditional statements inward Java
1) if statement
2) switch statement
------------------------------
Types of Conditions
1) Single condition
Ex:
if (a > b) {
----
----
2) Compound status
Ex:
if ((a > b) && or || (a > c)) {
--------
----------
3) Nested Condition
if (a > b){}
if (a < c) {}
if (a < d) {
----
----
}
------------------------------
Usage of Conditional Statements
1) Executing a block of statements when status is true.
Syntax:
if (Condition) {
Statements
---------
---------
}
Example:public static void original (String [] args){
int a, b;
a =10;
b =5;
if (a > b) {
System.out.println("A is a Big Nuber");
}
------------------------------------
2) Executing a block of statements when chemical compound status is true
Syntax:
if ((condition) && (condition2)) {
Statements
----------
-----------
}
Example:int a, b, c = 2;
a =10;
b =5;
if ((a > b) && (a > c)) {
System.out.println("A is a Big Nuber");
}
-------------------------------------------
3) Executing a block of statements when status is true. otherwise executing simply about other block of statements.
Syntax:
if (Condition) {
Statements
----------
--------
}
else {
statements
---------
--------
}
Example:
public static void original (String [] args){
int a, b;
a =10;
b =50;
if (a > b) {
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}
-----------------------------------------
4) Decide amid several Alternates (else if structure)
Syntax:
if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else
{
statements
----------
--------
}
Example:
public static void original (String [] args){
int a = -100;
if ((a >= 1) && (a < 100)) {
System.out.println("A is a Small Number");
}
else if ((a > 100) && (a <= 1000)) {
System.out.println("A is a Medium Number");
}
else if (a > 1000) {
System.out.println("A is a Big Number");
}
else
{
System.out.println("A is either Zero or negative value");
}
--------------------------------------------
5) Executing a block of statements when to a greater extent than than i status is truthful (Nested if).
Syntax:
if (condition) {}
if (condition) {}
if (condition) {
Statements
--------
---------
}
else
{
Statements
---------
---------
}
Example:
public static void original (String [] args){
int a =10, b =7, c = 5, d = 13;
if (a > b){}
if (a > c) {}
if (a > d){
System.out.println("A is a Big Number");
}
else {
System.out.println("A is Not a Big Number");
}
---------------------------------------
6) Decide amid several alternates (using Switch instance structure).
Syntax:
switch (expression) {
case value:
Statements
-------
break;
case value:
Statements
-------
break;
case value:
Statements
-------
break;
default
Statements
-------
------
}
Example:char shape = 'X';
switch (grade){
instance 'A':
System.out.println ("Excellent");
break;
instance 'B':
System.out.println ("Well Done");
break;
instance 'C':
System.out.println ("Better");
break;
default:
System.out.println("Invalid Grade");
}
---------------------------------------------------------