Conditional statements in Java
ICSE Class 9 · Computer Applications · Chapter 8
What this chapter covers
- Flow of control and decision making
- The if statement
- The if-else statement
- The if-else-if ladder
- Nested if
- The switch-case statement
- The break statement and fall-through
- Choosing between if-else-if and switch
- Menu-driven programs
- Terminating a program with System.exit(0)
Key points
- Flow of control is the order of execution of statements: sequential, conditional or iterative.
- A condition is a `boolean` expression built with relational and logical operators.
- The `if` statement runs its block only when the condition is true.
- The `if-else` statement runs exactly one of two blocks.
- The `if-else-if` ladder tests conditions from the top and runs only the block of the first true one; the order of ranges matters.
- A nested `if` is an `if` inside another `if` or `else`; an `else` pairs with the nearest unmatched `if`.
- A semicolon after the condition of an `if` is a logical error; `=` in place of `==` is a syntax error for numbers.
- The `switch` statement compares one `int`, `char`, `byte`, `short` or `String` expression with unique constant case labels.
- The `default` case runs when no label matches; it is optional.
- The `break` statement ends the `switch`; without it, control falls through into the next case.
- Fall-through can be used on purpose by stacking labels that share the same statements.
- Use `switch` for exact values of one variable and the ladder for ranges, real numbers and combined conditions.
- A menu-driven program prints a menu, reads a choice and uses `switch` to perform the chosen task, with `default` for invalid choices.
- `System.exit(0)` terminates the whole program at once; the status code 0 means normal termination.
Questions answered
Define a conditional statement. Name the conditional statements Java provides.
What is a compound statement? Why should braces be used even for a single statement under `if`?
What is printed by `int a = 5; if (a > 10); System.out.println("Big");`? Give the reason.
Distinguish between `if` and `if-else`.
Write an `if-else` statement that prints "Eligible to vote" when `age` is 18 or more and "Not eligible" otherwise.
Write a condition that is true when an integer `n` is a two-digit number ending with 5.
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`?
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.
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
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
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
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
ICSE 2027 sample paper · 1 mark
What is the purpose of the if-else-if-else ladder?
- iteration
- to check for one condition only
- to check multiple conditions sequentially
- to declare a variable
Show answer
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