Java Conditional Statements PDF
Document Details
Uploaded by VibrantSard4401
King Khalid University
Tags
Summary
This document provides a tutorial on Java conditional statements, including examples of if, else if, and else statements, switch statements, and logical operators. The document also includes examples of Java programs demonstrating conditional logic.
Full Transcript
Topic 6: Java Conditional Statements Reference: Introduction to programming Java: With a problem-solving approach, 2nd Edition – Chapter 4: pages 116-131 Conditions and Boolean Values Condition is a statement that involves a type of comparison. Java comparison operators: > , <...
Topic 6: Java Conditional Statements Reference: Introduction to programming Java: With a problem-solving approach, 2nd Edition – Chapter 4: pages 116-131 Conditions and Boolean Values Condition is a statement that involves a type of comparison. Java comparison operators: > , < , >= , 0) System.out.println("a is greater than 0"); } } Form 1 Example Program 2: //This program checks if age is less than zero or not. public class MyClass { public static void main(String[] args) { int age= 20; if(age < 0) System.out.println("Age cannot be less than 0 years!"); if(age >= 0) System.out.println("Your Age is : " + age); } } Form 2: if, else Form 2 Example Program 1: //This program checks if a number is greater than zero or not public class MyClass { public static void main(String[] args) { int a= -1; if(a>0) System.out.println("a is greater than 0"); else System.out.println("a is less than or equal 0"); } } Form 2 Example Program 2: //This program checks if an input is “Y” or not import java.util.Scanner; public class MyClass { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.println("Do you have a job? Y/N"); char response = stdIn.next().charAt(0); if(response == 'Y') System.out.println("Good. Keep the hard work."); else System.out.println("Extra money is always good."); } Form 3: if, else if, else Form 3 Example Program 1: //This program checks if a number is greater than zero, less than zero, or equal to zero public class MyClass { public static void main(String[] args) { int a= -1; if(a>0) System.out.println("a is greater than 0"); else if (a==0) System.out.println("a is equal 0"); else System.out.println("a is less than 0"); } Form 3 Example Program 2: //This program checks if the input is “coffee”, “tea”, or something else. import java.util.Scanner; public class MyClass { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.println("We have: \n -coffee \n -tea" + "\n"); System.out.println("What is your order?"); String order; order = stdIn.nextLine(); if(order.equals("coffee")) System.out.println("Coffee is 10SR"); else if(order.equals("tea")) System.out.println("Black Tea is 5SR"); else System.out.println("Sorry. We don't have that."); } } Logical Operators: && , ||, ! Conditions are generally two types: 1. Simple condition: e.g. x > y 2. Compound condition when two or more simple conditions are joined together using a logical operator. Logical Operators in Java: && || ! Examples of compound conditions: x > y && y==2 a == 10 || b < 5 ! (d == 3) Logical Operator Example Program 1: //This program compares three numbers and prints the maximum public class MyClass { public static void main(String[] args){ int n1=6, n2=4, n3=5; if(n1 > n2 && n1 > n3) System.out.println(n1 + "is the largest number"); else if (n2 > n1 && n2 > n3) System.out.println(n2 + "is the largest number"); else if (n3 > n1 && n3 > n2) System.out.println(n3 + "is the largest number"); else System.out.println("Two numbers or more are equals"); } } Logical Operator Example Program 2: //This program checks if the input is “y”, “n”, or something else import java.util.Scanner; public class MyClass { public static void main(String[] args){ Scanner stdin = new Scanner(System.in); System.out.println("Do you think Java is easy to learn? Y/N "); char response = stdin.next().charAt(0); if(response == 'Y' || response == 'y') System.out.println("Fantastic! "); else if (response == 'N' || response == 'n') System.out.println("Maybe with practice it will be easy."); else System.out.println("Incorrect response! "); } } Logical Operator Example Program 3: //This program checks if a number is even or odd. import java.util.Scanner; public class MyClass { public static void main(String[] args){ Scanner stdin = new Scanner(System.in); System.out.println("Please enter an integer: "); int number = stdin.nextInt(); if(!(number%2==1)) System.out.println("Entered number is even."); else System.out.println("Entered number is odd."); } } Switch Statement The switch statement works similarly to the “if, else if, else” form of the if statement. In that, it allows you to follow one of several paths. Key difference is that the switch statement’s determination of which path to take is based on a single value. The condition in the switch statement is always “equal” “== ”. Conditions like >, =,