0% found this document useful (0 votes)
2 views6 pages

Control Statements A 33

The document provides examples of control statements in Java, including if statements, if-else statements, nested if-else statements, and switch statements. Each example includes code snippets and their corresponding outputs, demonstrating how these control structures function based on different conditions. The document highlights the importance of using control statements to manage the flow of a program based on specific conditions.

Uploaded by

ashwiniraia07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Control Statements A 33

The document provides examples of control statements in Java, including if statements, if-else statements, nested if-else statements, and switch statements. Each example includes code snippets and their corresponding outputs, demonstrating how these control structures function based on different conditions. The document highlights the importance of using control statements to manage the flow of a program based on specific conditions.

Uploaded by

ashwiniraia07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Control statements

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

5. Nested if else statement

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!

8. switch statement example2 along with break

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

9. Switch with grouping

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

You might also like