Java Conditions and If Statements PDF

Summary

This document provides examples of Java conditional statements, including if-else, if-else-if, and nested if conditions. It also includes an example of how to use the Scanner class for user input, demonstrating how to obtain boolean, byte, double, float, int, and string values from a user. The document is a practical guide to these programming concepts and demonstrates different scenarios with examples.

Full Transcript

JAVA USER INPUT The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. Input Types Method Description nextBool...

JAVA USER INPUT The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. Input Types Method Description nextBoolean() Reads a boolean value from the user nextByte() Reads a byte value from the user nextDouble() Reads a double value from the user nextFloat() Reads a float value from the user nextInt() Reads a int value from the user nextLine() Reads a String value from the user nextLong() Reads a long value from the user nextShort() Reads a short value from the user Example import java.util.Scanner; // Import the Scanner class class Main { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); // Create a Scanner object System.out.println("Enter username"); String userName = myObj.nextLine(); // Read user input System.out.println("Username is: " + userName); // Output user input } } Import Scanner Class We need to import the java.util.Scanner package before we can use the Scanner class. import java.util.Scanner; The System.in parameter is used to take input from the standard input. It works just like taking inputs from the keyboard. Scanner myObj = new Scanner(System.in); The nextLine() method of the Scanner class to read a line of text from the user. String userName = myObj.nextLine(); Example import java.util.Scanner; class Main { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); System.out.println("Enter name, age and salary:"); // String input String name = myObj.nextLine(); // Numerical input int age = myObj.nextInt(); double salary = myObj.nextDouble(); // Output input by user System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Salary: " + salary); } } JAVA CONDITIONS AND IF STATEMENTS Java has the following conditional statements:  Use if to specify a block of code to be executed, if a specified condition is true  Use else to specify a block of code to be executed, if the same condition is false  Use else if to specify a new condition to test, if the first condition is false  Use switch to specify many alternative blocks of code to be executed IF STATEMENT Syntax if (condition) { // block of code to be executed if the condition is true } Example import java.util.Scanner; public class PositiveNumberChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (number > 0) { System.out.println(number + " is positive."); } scanner.close(); } } The scanner.close() method in Java is used to close a Scanner object that you’ve created to read input (usually from the keyboard or a file). ELSE STATEMENT Syntax if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false } Example import java.util.Scanner; public class PositiveNumberChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (number > 0) { System.out.println(number + " is positive."); }else { System.out.println(number + " is negative."); } scanner.close(); } } ELSE IF STATEMENT Syntax if (condition1) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false } Example import java.util.Scanner; public class NumberSignChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (number > 0) { System.out.println(number + " is positive."); } else if (number < 0) { System.out.println(number + " is negative."); } else { System.out.println("The number is zero."); } scanner.close(); } } NESTED IF CONDITION Nested means within. Nested if condition means if-within-if. Nested if condition comes under decision-making statement in Java. There could be infinite if conditions inside an if condition. NESTED IF Syntax if( condition ){ if( condition ){ if( condition ){ //block of code to be executed. } } } Example 1 int x = 30; int y = 10; if( x == 30 ) { if( y == 10 ) { System.out.print("X = 30 and Y = 10"); } } Example 2 int x = 30; int y = 10; if( x < 30 ) { System.out.print("X < 30"); } else { if( y > 9 ) { System.out.print("X > 30 and Y > 9"); } } Example 3 import java.util.Scanner; public class RangeChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (number >= 0) { if (number

Use Quizgecko on...
Browser
Browser