Computer Applications
Answer
Two differences between if-else-if and switch-case statements are:
- switch-case statement can only test for equality whereas if-else-if can test for any Boolean expression.
- switch-case tests the same expression against constant values while if-else-if can use different expression involving unrelated variables.
Related Questions
A triangle is said to be an 'Equable Triangle', if the area of the triangle is equal to its perimeter. Write a program to enter three sides of a triangle. Check and print whether the triangle is equable or not.
For example, a right angled triangle with sides 5, 12 and 13 has its area and perimeter both equal to 30.Differentiate between if else if and switch-case statements
Rewrite the following code using single if statement.
if(code=='g') System.out.println("GREEN"); else if(code=='G') System.out.println("GREEN");The statement that brings the control back to the calling method is:
- break
- System.exit(0)
- continue
- return