if statement in Java
In Java, the if statement is a fundamental control flow statement used to execute a block of code conditionally. It allows you to specify a condition, and if that condition evaluates to true, the code block enclosed within the if statement is executed. If the condition is false, the code block is skipped.
Here is the basic syntax of an if statement in Java:
if(condition){
Here's a breakdown of the components:
- if: This is the keyword that initiates the if statement.
- condition: This is a Boolean expression that you want to evaluate. If the condition is true, the code block is executed; otherwise, it's skipped.
- {}: These curly braces enclose the code block associated with the if statement. The code inside the curly braces is executed when the condition is true.
Here's a simple example:
In this example, if the age variable is greater than or equal to 18, the message "You are eligible for driving licence." will be printed. If age is less than 18, nothing will be printed because the if block will not execute.
Additionally, you can use an else statement to provide an alternative block of code to execute when the if condition is false. Here's how it works:
In this case, if age is less than 18, the message "You are a minor." will be printed because the else block is executed when the if condition is false.
Here's an example of how to use an if statement in Java to take input from the user and check if the entered number is odd or even:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In this program:
- We import the Scanner class to read input from the user.
- We create a Scanner object called scanner to read user input.
- We prompt the user to enter a number using System.out.print() and read the input using scanner.nextInt().
- We use an if statement to check if the entered number is even or odd. The condition number % 2 == 0 checks if the remainder of dividing number by 2 is equal to 0. If it is, the number is even; otherwise, it's odd.
- Depending on the outcome of the if condition, we print the appropriate message to the console.
- Finally, we close the Scanner object to release system resources.
When you run this program, it will take a number as input from the user and tell you whether it's odd or even.
IMPORTANT: For nested if statement, CLICK HERE.
EXERCISES
Here are some exercises to practice using if statements in Java. These exercises will help you become more comfortable with conditional statements and decision-making in your Java programs:
- Odd or Even Checker: Write a program that takes an integer as input and determines whether it's odd or even using an if statement.
- Grade Calculator: Create a program that calculates a student's letter grade based on their numerical score. Define your grading criteria and use if statements to assign grades.
- Leap Year Checker: Write a program that checks if a given year is a leap year or not. Leap years are those divisible by 4 but not divisible by 100 unless they are also divisible by 400.
- Age Classifier: Develop a program that categorizes a person's age into different groups such as infant, child, teenager, adult, or senior citizen using if statements.
- BMI Calculator: Create a BMI (Body Mass Index) calculator. Prompt the user for their weight (in kilograms) and height (in meters) and use if statements to classify them as underweight, normal weight, overweight, or obese based on BMI ranges.
- Tax Calculator: Build a program that calculates the income tax for an individual based on their income and tax brackets. Use if statements to determine the tax rate and calculate the tax owed.
- Number Comparison: Write a program that compares two numbers entered by the user and prints out whether the first number is greater than, less than, or equal to the second number using if statements.
- Vowel or Consonant Checker: Create a program that checks if a given character is a vowel or a consonant. Use if statements to make this determination.
- Palindrome Checker: Develop a program that checks if a given number is a palindrome (reads the same forwards and backward). Use if statements to make this determination.
e.g. 121, 12321 are palindrome numbers. - Ticket Price Calculator: Build a program that calculates the price of a movie ticket based on age and time of the movie. Use if statements to apply discounts for children, seniors, and matinee showings.
- Password Strength Checker: Create a program that evaluates the strength of a password entered by a user. Use if statements to check for criteria like length, the presence of special characters, numbers, and uppercase letters.
- Menu Selection: Create a menu-based program with different options (e.g., 1: for addition, 2: for subtration, 3: for multiplication, 4: for division). Accept 2 integer values from user and then display the result based on the given choice.
- File Type Identifier: Write a program that takes a file extension as input and uses if statements to determine the type of file (e.g., text, image, video) based on the extension.
- Roots of Quadratic Equation: Write a program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display “The equation has no real roots”.
- Random Month: Create a program that generates a random integer in the range of 1 to 12 and then presents the corresponding English month name, ranging from January to December, for each of the numbers 1 to 12.
These exercises cover a range of scenarios where if statements are commonly used in Java programming. They should help you practice and improve your skills in using conditional statements effectively.
Comments
Post a Comment