Nested if in java
By: aathishankaran
The if constructed in java can be nested. In other words, an if construct can be present inside the body of another if construct.
Example:
To decide discount percentage for selling a TV, the following are to be changed:
Type of TV:
If Black and White [B], discount will be 5% of selling price.
If typed of TV is colored [C], verify the size of TV screen:
For 14 inches, discount is 8% of selling price
For 21 inches, discount is 10% of selling price
class PROGRAM_BUY_TV {
char Type;
integer Size;
integer Dis_Perc=0;
display “EnternTV type (Color /BW):â€;
accept Type;
display “Enter TV screen size:â€;
accept Size;
if(Type == 'B')
{
Dis_perc=5;
}
else
{
if(Type == 'C')
{
if(Size==14)
{
Dis_Perc=8;
}
else
{
Dis_Perc=10;
}
}
}
}
It is very important to be sure which else construct goes with which if construct. In the examples, we have indented the else-related statements with their corresponding if-related statements. This indentation greatly aids the clarity of the code for a reader. Also, the number of opening if…else braces must match with the number of closed if…else braces.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Archived Comments
1. Respected Sir,
We are
View Tutorial By: Core Java at 2009-10-24 01:36:20
2. //EXAMPLE OF NESTED IF*****
//FIND OUT BIGE
View Tutorial By: Amitava Misra at 2009-11-03 06:11:33
3. two of them are equal and third one is less than b
View Tutorial By: nani at 2011-10-28 10:55:58
4. PROGRAM TO FIND OUT SMALLER NU:-
cl
View Tutorial By: ishaan gogna at 2012-05-09 19:00:49