0% found this document useful (0 votes)
114 views

Java Decision Making

The document discusses 4 types of decision making statements in Java: 1) if statements evaluate a condition and execute code if true, 2) if-else statements evaluate a condition and execute one block of code if true and another if false, 3) else-if statements allow multiple conditions to be checked in sequence, and 4) switch statements compare a variable to multiple case values and execute code for a matching case. Syntax and examples are provided for each statement type to demonstrate their usage in Java programs.

Uploaded by

Nikunj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

Java Decision Making

The document discusses 4 types of decision making statements in Java: 1) if statements evaluate a condition and execute code if true, 2) if-else statements evaluate a condition and execute one block of code if true and another if false, 3) else-if statements allow multiple conditions to be checked in sequence, and 4) switch statements compare a variable to multiple case values and execute code for a matching case. Syntax and examples are provided for each statement type to demonstrate their usage in Java programs.

Uploaded by

Nikunj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Decision Making

1) If statement
2) if else
3) else If
4) switch

1) The basic syntax of if statement is:


Syntax:
if(test_expression)
{
statement 1;

statement 2; ...

Example of a Java Program to Demonstrate If statements :


public class Sample

public static void main(String args[]){

int a=20, b=30;

if(b>a)
System.out.println("b is greater");

2) The basic format of if else statement is:


Syntax:
if(test_expression)

{
//execute your code
}
else

{
//execute your code
}
Example of a Java Program to Demonstrate If else statements
public class Sample

public static void main(String args[])

int a = 80, b = 30;


if (b > a)
{
System.out.println("b is greater");
}
else
{
System.out.println("a is greater");
}

3) The basic format of else if statement is:


Syntax:
if(test_expression)
{
//execute your code
}
else if(test_expression n)
{
//execute your code
}
else
{
//execute your code
}

Example of a Java Program to Demonstrate else If statements


public class Sample {

public static void main(String args[]) {


int a = 30, b = 30;

if (b > a)
{
System.out.println("b is greater");
}
else if(a >b){
System.out.println("a is greater");
}
else {
System.out.println("Both are equal");
}
}
}

4) The basic format of switch statement is:

Syntax:
switch(variable)
{
case 1:
//execute your code
break;

case n:
//execute your code
break;

default:
//execute your code
break;
}
Example of a Java Program to Demonstrate Switch Statement
public class Sample {

public static void main(String args[]) {


int a = 5;

switch (a) {
case 1:
System.out.println("You chose One");
break;

case 2:
System.out.println("You chose Two");
break;

case 3:
System.out.println("You chose Three");
break;

case 4:
System.out.println("You chose Four");
break;

case 5:
System.out.println("You chose Five");
break;

default:
System.out.println("Invalid Choice. Enter a no between 1 and 5");
break;
}
}
}

You might also like