You are on page 1of 3

if (condition) {

statements;
}

if (condition) {
statements;
} else {
statements;
}

if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
==========================

class IfElseDemo {
public static void main(String[] args) {

int testscore = 76;


char grade;

if (testscore >= 90) {


grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}

=================================

//

import java.util.Scanner;

class Test
{
public static void main(String[] args) throws Exception
{
Scanner in = new Scanner(System.in);
String firstName, lastName;
int organDonorAnswer;
char sex;
String gender;

System.out.println(" -=- Motor Vehicle Administration -=-");


System.out.println(" --- Driver's License Application ---");
System.out.print("First Name: ");
firstName = in.next();
System.out.print("Last Name: ");
lastName = in.next();
System.out.print("Sex(F=Female/M=Male): ");
sex = in.next().charAt(0);
System.out.print("Are you willing to be an Organ Donor(1=Yes/0=No)? ");
organDonorAnswer = in.nextInt();

if( sex == 'f' ) {


gender = "Female";
} else if( sex == 'F' ) {
gender = "Female";
} else if( sex == 'm' ) {
gender = "Male";
} else if( sex == 'M' ) {
gender = "Male";
} else {
gender = "Unknown";
}

System.out.println("\n -=- Motor Vehicle Administration -=-");


System.out.println(" --- Driver's License Information ---");
System.out.printf("Full Name: %s %s\n", firstName, lastName);
System.out.printf("Sex: %s\n", gender);
System.out.print("Organ Donor? ");
System.out.println(organDonorAnswer == 1 ? "Yes" : (organDonorAnswer == 0 ?
"No" : "Invalid Answer"));
}
}

============================

import java.util.Scanner;

class Test
{
public static void main(String[] args) throws Exception
{
Scanner in = new Scanner(System.in);

boolean b;

b = true;

System.out.println(b);

b = false;

System.out.println(b);

b = in.nextBoolean();
System.out.println(b);
if (b) {
System.out.println(b);
}

if (b == true) {
System.out.println(b);
}

}
}

=============================

You might also like