LIWA

Conditional statements in Java

ICSE Class 9 · Computer Applications · Chapter 8

What this chapter covers

Key points

Questions answered

Define a conditional statement. Name the conditional statements Java provides.
A conditional statement is a statement that checks a condition and, depending on whether it is true or false, decides which statement or block is executed next. Java provides `if`, `if-else`, the `if-else-if` ladder, nested `if` and `switch-case`.
What is a compound statement? Why should braces be used even for a single statement under `if`?
A compound statement (block) is a group of statements enclosed in `{ }` that Java treats as one statement. Braces prevent the error of adding a second statement later that the `if` does not control.
What is printed by `int a = 5; if (a > 10); System.out.println("Big");`? Give the reason.
`Big` is printed. The semicolon after the condition ends the `if` with an empty statement, so the `println` is not controlled by the condition and always runs.
Distinguish between `if` and `if-else`.
The `if` statement runs its block only when the condition is true and does nothing when it is false. The `if-else` statement runs one block when the condition is true and another block when it is false, so exactly one of the two always runs.
Write an `if-else` statement that prints "Eligible to vote" when `age` is 18 or more and "Not eligible" otherwise.
`if (age >= 18) System.out.println("Eligible to vote"); else System.out.println("Not eligible");`
Write a condition that is true when an integer `n` is a two-digit number ending with 5.
`n >= 10 && n <= 99 && n % 10 == 5` (for positive numbers).
Why must the conditions of a grade ladder be tested from the highest range downwards? What would 95 marks receive if `marks >= 75` were tested before `marks >= 90`?
The ladder stops at the first true condition. With `marks >= 75` first, 95 would satisfy it and get grade B; the test for A would never be reached.
A taxi charges ₹25 per km for the first 10 km and ₹18 per km for every km after that. Find the fare for 16 km using the slab method.
10 × 25 = ₹250 for the first slab; 6 × 18 = ₹108 for the rest; fare = 250 + 108 = ₹358.

Past board exam questions

ICSE 2027 sample paper · 15 marks

A laptop company gives a discount to its customers based on the number of laptops purchased. If the number of laptops is more than three, the customers get a discount of 10% on the total cost, otherwise the discount is zero. Define a class “laptop” to accept the name of the company [String], name of the customer [String], number of laptops [int], and the price of each laptop [double] from the user using Scanner class methods only. Calculate the discount [double] and the amount to be paid [double] by the customer. Discount = number of laptops × price × 10 % Amount to be paid by the customer = number of laptops × price - discount Print all the details.

Show answer
```java import java.util.Scanner; class laptop { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the name of the company: "); String company = sc.nextLine(); System.out.print("Enter the name of the customer: "); String customer = sc.nextLine(); System.out.print("Enter the number of laptops: "); int n = sc.nextInt(); System.out.print("Enter the price of each laptop: "); double price = sc.nextDouble(); double discount; if (n > 3) discount = n * price * 10.0 / 100; else discount = 0.0; double amount = n * …

ICSE 2027 sample paper · 15 marks

In a hill station, the bikes are given for rent on an hourly basis as follows: | Number of hours | Rate | |---|---| | Upto 2 hours | Rs.150 per hour | | For Next 2 hours (each additional hour) | Rs.100 per hour | | For above 4 hours (each additional hour) | Rs.75 per hour | Define a class “bike” to accept the name of the customer and number of hours he would like to hire the bike, calculate and display the amount to be paid by him as: Dear <name of the customer> You need to pay <amount> for <number of hours> hours.

Show answer
```java import java.util.Scanner; class bike { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the name of the customer: "); String name = sc.nextLine(); System.out.print("Enter the number of hours: "); int h = sc.nextInt(); double amt; if (h <= 2) amt = h * 150; else if (h <= 4) amt = 2 * 150 + (h - 2) * 100; else amt = 2 * 150 + 2 * 100 + (h - 4) * 75; System.out.println("Dear " + name); System.out.println("You need to pay " + amt + " for " + h + " hours."); } } ``` Variable description: name – …

ICSE 2027 sample paper · 15 marks

(i) Define a class “roman” to display the corresponding Roman numeral of a Hindu Arabic number (1 to 5) accepted from the user. The program should be designed using switch case construct only. (ii) What are relational operators in Java? Name any two types of relational operators.

Show answer
(i) ```java import java.util.Scanner; class roman { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number (1 to 5): "); int n = sc.nextInt(); switch (n) { case 1: System.out.println("I"); break; case 2: System.out.println("II"); break; case 3: System.out.println("III"); break; case 4: System.out.println("IV"); break; case 5: System.out.println("V"); break; default: System.out.println("Invalid number"); } } } ``` Variable description: n – int, the Hindu-Arabic number (1 to 5) entered by the …

ICSE 2027 sample paper · 15 marks

Write a menu-driven program to calculate the area of an equilateral triangle and a scalene triangle as per the user’s choice (using switch case only). (i) Scalene triangle area = √(s(s-a)(s-b)(s-c)) where a, b, c are the sides and s = (a+b+c)/(2). (ii) Equilateral triangle area = (√(3))/(4)a2, a = side of the triangle

Show answer
```java import java.util.Scanner; class menu { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("1. Area of a Scalene triangle"); System.out.println("2. Area of an Equilateral triangle"); System.out.print("Enter your choice: "); int ch = sc.nextInt(); switch (ch) { case 1: System.out.print("Enter the sides a, b and c: "); double a = sc.nextDouble(); double b = sc.nextDouble(); double c = sc.nextDouble(); double s = (a + b + c) / 2; double area1 = Math.sqrt(s * (s - a) * (s …

ICSE 2027 sample paper · 1 mark

What is the purpose of the if-else-if-else ladder?

  1. iteration
  2. to check for one condition only
  3. to check multiple conditions sequentially
  4. to declare a variable
Show answer
to check multiple conditions sequentially

ICSE 2027 sample paper · 2 marks

Given below is the program segment ```java if(a%b==0) System.out.println(a/b); else System.out.println(a%b); ``` Write the output of the program segment when: (a) a = 50, b = 5 (b) a = 50, b = 7

Show answer
(a) 50%5 = 0, so a/b is printed: 10 (b) 50%7 = 1 (not 0), so a%b is printed: 1