if, if-else, if-else-if, nested if


if, if-else, if-else-if, nested if

The if statements are the decision structures used in JAVA. It is most useful decision structure for testing for a condition and branch to the relevant statement in the program.

Simple and Poorly written program

An efficient programming approach

package com.Example;
import javax.swing.*;
import java.util.Scanner;
public class examResult
{

public static void main(String[] args)
{

int number;
String input;
input= JOptionPane.showInputDialog(“Please enter a number”);
number = Integer.parseInt(input);

if(number == 5)
{
JOptionPane.showMessageDialog(null, “The number is exactly equal to 5”);
}
if(number >5)
{
JOptionPane.showMessageDialog(null, “The number is > 5”);
}
if(number > 10)
{
JOptionPane.showMessageDialog(null, “The number is > 10”);
}
if(number <>>5)
{
JOptionPane.showMessageDialog(null, “The number is < 5”);
}

}

}

package com.Example;
import javax.swing.*;
import java.util.Scanner;
public class examResult
{

public static void main(String[] args)
{

int number;
Scanner in =new Scanner(System.in);
System.out.println(“Enter a number”);
number=in.nextInt();
if(number ==5)
{
JOptionPane.showMessageDialog(null, “The number is exactly equal to 5”);
}
else if(number >10)
{
JOptionPane.showMessageDialog(null, “The number is > 10″”);
}
else if (number >5)
{
JOptionPane.showMessageDialog(null, “Yhe number is > 5”);
}
else
{
JOptionPane.showMessageDialog(null, “he number is < 5”);
}

}

}

Example-2: A program to input the markss of a students and evaluate whether the students failed, is eligible for re-assessment, distinction, or passed.

package com.Example;
import javax.swing.*;
public class examResult
{

public static void main(String[] args)
{

int number;
String input;
input= JOptionPane.showInputDialog(“Please enter a number”);
number = Integer.parseInt(input);
if(number <50 && number >=45)
{

JOptionPane.showMessageDialog(null, “You can give reassessment”);

}
else if(number <45)
{

JOptionPane.showMessageDialog(null, “you failed”);

}
else
{

if(number >= 75)

{

JOptionPane.showMessageDialog(null, “you passed with distinction”);

}
else

{

JOptionPane.showMessageDialog(null, “You Passed”);

}

}

}

error: Content is protected !!