Decision Making

0
99

Decision Making

We use decision making when we want to skip some of the statements in our program as per the condition.
1.if statement:This statement checks for the if condition and executes the statements if it is true.
Example:
[java] public class greater
{
public static void main(String args[])
{
if(3>1)
{
System.out.println(“3 is greater than 1”);
}
}
}
[/java] Output:
3 is greater than 1.

2.if else-It checks for the condition and executes the statements in if block if the condition is true .If the condition is false,statements in the else block are executed.
Example:
[java] public class greater
{
public static void main(String args[])
{
if(3>8)
{
System.out.println(“False”);
}
else
{
System.out.println(“True”);
}
}
}
[/java]

3.nested if-It checks for multiple conditions.
Example:
[java] public class Greater
{
public static void main(String args[])
{
if(3>8)
{
System.out.println(“False”);
}
elseif(8>1)
{
System.out.println(“True”);
}
else
{
System.out.println(“Skip”);
}
}
}
[/java]

4.Switch case:This is based on the user’s choice.
Example

[java]

public class Choice

{

public static void main(String args[])

{

int n=1;

switch(n)

{

case 1:

System.out.println(“Choice 1”);

break;

case 2:
System.out.println(“Choice 2”);
break;

default:
System.out.println(“wrong choice”);
}
}
}

[/java]

Since n=1,we will get output:
Choice 1

We can also input the value of n instead of directly assigning it value.

Facebook Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.