Conditional Statements in Java
Conditional Statements in Java
if:
Syntax: if(condition)
if(condition) statement1;
{ statement2;
// Statements to execute if
// condition is true // Here if the condition is true, if
} block
// will consider only statement1 to
be inside
// its block.
// Java program to illustrate If statement
class IfDemo
{
public static void main(String args[])
{
int i = 10;
if (i > 15)
System.out.println("10 is less than 15");
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Output:
i is smaller than 15
public class Main
{
public static void main(String args[])
{
int a = 15;
if (a > 20)
System.out.println("a is greater than 10");
else
System.out.println("a is less than 10");
System.out.println("Hello World!");
}
}
}
Output:
a is less than 10
Hello World!
nested-if:
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
// Java program to illustrate nested-if statement
class NestedIfDemo
{
public static void main(String args[])
{
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
System.out.println("i is smaller than 15");
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
System.out.println("i is smaller than 12 too");
else
System.out.println("i is greater than 15");
}
}
}
Output:
i is smaller than 15
i is smaller than 12 too
public class Main
{
public static void main(String args[])
{
int s = 18;
if (s > 10)
{
if (s%2==0)
System.out.println("s is an even number and greater than 10!");
else
System.out.println("s is a odd number and greater than 10!");
}
else
{
System.out.println("s is less than 10");
}
System.out.println("Hello World!");
}
}
Output:
s is an even number and greater than 10!
Hello World!
if-else-if ladder:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
// Java program to illustrate if-else-if ladder
class ifelseifDemo
{
public static void main(String args[])
{
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
Output:
i is 20
switch-case
Syntax:
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
// Java program to illustrate switch-case
class SwitchCaseDemo
{
public static void main(String args[])
{
int i = 9;
switch (i)
{
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
default:
System.out.println("i is greater than 2.");
}
}
}
Output:
i is greater than 2.
public class Music {
public static void main(String[] args) case 6:
{ musicInstrument = "Violin";
int instrument = 4;
String musicInstrument; break;
// switch statement with int data type case 7:
switch (instrument) {
case 1: musicInstrument = "Trumpet";
musicInstrument = "Guitar"; break;
break;
case 2: default:
musicInstrument = "Piano"; musicInstrument = "Invalid";
break;
case 3: break;
musicInstrument = "Drums"; }
break;
case 4: System.out.println(musicInstrument);
musicInstrument = "Flute"; }
break;
case 5: }
musicInstrument = "Ukelele";
break;
Output:
Flute
Rules To Remember
There are a certain rules one must keep in mind while declaring a switch case in
java. Following are a certain points to remember while writing a switch case in
java.
1.We cannot declare duplicate values in a switch case.
2.The values in the case and the data type of the variable in a switch case must be
same.
3.Variables are not allowed in a case, it must be a constant or a literal.
4.The break statement fulfills the purpose of terminating the sequence during
execution.
5.It is not necessary to include the break statement, the execution will move to
the next statement if the break statement is missing.
6.The default statement is optional as well, it can appear anywhere in the block.
Break Statement In Switch Case
case 7:
System.out.println("july");
break;
public class Example{
case 8:
public static void main(String args[]){
int month= 7;
System.out.println("august");
break;
switch(month){ case 9:
case 1 :
System.out.println("january");
System.out.println("september");
break; break;
case 2:
System.out.println("february");
case 10:
break; System.out.println("October");
case 3: break;
System.out.println("march");
break; case 11:
case 4: System.out.println("november");
System.out.println("april"); break;
break;
case 5: case 12:
System.out.println("may"); System.out.println("december");
break;
break;
case 6:
System.out.println("june"); default:
break; System.out.println("not valid");
}
}
}
Output: july
Nested Switch Case
public class Example{
public static void main(String args[]){
int tech = 2;
int course = 2;
switch(tech){
case 1:
System.out.println("python");
break;
case 2:
switch(course){
case 1:
System.out.println("J2EE");
break;
case 2:
System.out.println("advance java");
}
}
}
}
Output: advance java
Fall Through Switch Case
public class Example{
public static void main( String args[])
{
int courses = 2;
switch(courses){
case 1:
System.out.println("java");
case 2:
System.out.println("python");
case 3:
System.out.println("Devops");
case 4:
System.out.println("Automation testing");
case 5:
System.out.println("Hadoop");
case 6:
System.out.println("AWS");
default:
System.out.println("check out www.google.com for more");
}
}
}
Output: java
python
Devops
Automation testing
Hadoop
AWS
check out www.google.com for more
Enum In Switch Case
public class Example{
public enum day { s , m , t , w , th, fr, sa };
public static void main(String args[]){
course[] c = day.values();
for(day today : c)
{
switch (today){
case s :
System.out.println("Sunday");
break;
case m:
System.out.println("Monday");
break;
case t:
System.out.println("Tuesday");
break;
case w:
System.out.println("Wednesday");
break;
case th:
System.out.println("Thursday");
break;
case fr:
System.out.println("Friday");
break;
case sa:
System.out.println("Saturday");
break;
}
}
}
}
Output: Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
String In Switch Case
if (t)
return;
package MyPackage;
public class Demo {
public static void main(String[] args) {
int term=6;
for(int i=1;i<=term;i++){ for(int j=term;j>=i;j--){
System.out.print("* ");
}
System.out.println();//new line
}
}
}
Output:
* * * * *
* * * *
* * *
* *
*
What is a While Loop in
Java and how to use it?
Syntax:
while (condition) {
}
class Example {
public static void main(String args[]){
int i=10;
while(i>1){
System.out.println(i);
i--;
}
}
}
Output:
10
9
8
7
6
5
4
3
2
// Java While Loop example
package Loops;
import java.util.Scanner;
Syntax:
while (true){
statement(s);
}
class Example {
public static void main(String args[]){
int i=10;
while(i>1)
{
System.out.println(i);
i++;
}
}
}
What is a Do while loop in
Java and how to use it?
Syntax:
do{
//code to be executed
}while(condition);
public class Example {
do {
System.out.print("value of x : " + x );
x++;
System.out.print("n");
}while( x < 11 );
}
}
Output:
value of x : 1
value of x : 2
value of x : 3
value of x : 4
value of x : 5
value of x : 6
value of x : 7
value of x : 8
value of x : 9
value of x : 10
public class Main
{
public static void main(String args[])
{
int i = 20;
do
{
System.out.println(i);
i = i+1;
} while (i <= 20);
}
}
Output:
20
Infinite do-while loop in Java
Syntax:
do{
//code to be executed
}while(true);
public class DoWhileInfinite {
public static void main(String[] args) {
do{
System.out.println("infinitive do while loop");
}while(true);
}
}
Output:
infinitive do while loop
infinitive do while loop
infinitive do while loop
In order to exit the loop, press ctrl+c.