Control Statements A 33
Control Statements A 33
1. if statement
***************
package programmesofcorejava;
class IfStatementEx
{
public static void main(String[] args)
{
int a = 10;
if(a>=5)
{
System.out.println(" QSPIDERS ");
}
}
}
Output:
QSPIDERS
2. if statement Ex2
package programmesofcorejava;
class IfStatementEx
{
public static void main(String[] args)
{
int a = 1;
if(a>=5)
{
System.out.println(" QSPIDERS ");
}
// out of the if block
}
}
Output:
Blank console as the condition is returning false
3. if else statement
package programmesofcorejava;
class IfElseStatementEx
{
public static void main(String[] args)
{
int x = 20;
if(x>0)
{
System.out.println(" POSITIVE NUMBER ");
}
else
{
System.out.println(" NEGATIVE NUMBER ");
}
}
}
Output
POSITIVE NUMBER
4. if else statement
package programmesofcorejava;
class IfElseStatementEx
{
public static void main(String[] args)
{
int x = -20;
if(x>0)
{
System.out.println(" POSITIVE NUMBER ");
}
else
{
System.out.println(" NEGATIVE NUMBER ");
}
}
}
Output:
NEGATIVE NUMBER
package programmesofcorejava;
class NestedIfElseEx
{
public static void main(String[] args)
{
int y = -35;
if(y>0)
{
System.out.println(" POSITIVE NUMBER");
}
else if(y<0)
{
System.out.println(" NEGATIVE NUMBER");
}
else
{
System.out.println(" ZERO ");
}
}
}
Output:
NEGATIVE NUMBER
6. Nested if else
package programmesofcorejava;
class NestedIfElseEx2
{
public static void main(String[] args)
{
int a = 100;
if(a>0)
{
System.out.println(" The value of a is greater than 0");
}
else if(a>=10)
{
System.out.println(" The value of a is greater than or equal to
10");
}
else if(a!= 0)
{
System.out.println(" The value of a is not equal to 0");
}
else if(a == 100)
{
System.out.println(" The value of a is equal to 100");
}
else if(a<=1000)
{
System.out.println(" The value of a is less than or equal to
1000");
}
else
{
System.out.println(" The value of a is 100! ");
}
}
}
Output:
The value of a is greater than 0
7. Switch statement
package programmesofcorejava;
class SwitchEx
{
public static void main(String[] args)
{
int a = 1034;
switch(a)
{
case 1:
{
System.out.println(" BRINJAL ");
}
case 10:
{
System.out.println(" POTATO ");
}
case 100:
{
System.out.println(" TOMATO ");
}
case 21:
{
System.out.println(" DRUMSTICK ");
}
case 11:
{
System.out.println(" LADIESFINGER ");
}
case 31:
{
System.out.println(" SPINACH ");
}
default:
{
System.out.println(" These are the different types of vegetables!
");
}
}
}
}
Output:
These are the different types of vegetables!
package programmesofcorejava;
class SwitchEx
{
public static void main(String[] args)
{
int a = 100;
switch(a)
{
case 1:
{
System.out.println(" BRINJAL ");
}
break;
case 10:
{
System.out.println(" POTATO ");
}
break;
case 100:
{
System.out.println(" TOMATO ");
}
break;
case 21:
{
System.out.println(" DRUMSTICK ");
}
break;
case 11:
{
System.out.println(" LADIESFINGER ");
}
break;
case 31:
{
System.out.println(" SPINACH ");
}
break;
default:
{
System.out.println(" These are the different types of vegetables!
");
}
}
// out of the switch statement
}
}
Output:
TOMATO
package programmesofcorejava;
class SwitchGroupingEx
{
public static void main(String[] args)
{
int a = 100;
switch(a)
{
// Grouping ==> combining the similar cases with a common block
case 10:
case 11:
case 100:
case 1:
{
System.out.println(" POTATO ");
}
break;
case 21:
{
System.out.println(" DRUMSTICK ");
}
break;
case 31:
{
System.out.println(" SPINACH ");
}
break;
default:
{
System.out.println(" These are the different types of vegetables!
");
}
}
// out of the switch statement
}
}
Output:
POTATO