A guide to using "if" stmts (nested)
1... ALWAYS VERTICALL LINE UP OPENING AND CLOSING BRACKETS
2... EVERY TIME YOU NEST A LITLE DEEPER MAKE SURE YOU INDENT
3... KEEP YOU INDENTS AT THE LEVEL OF THE NESTING
EG... ASSUME I AM DEALING WITH AGE, WEIGHT, AND HEIGHT
if ( age > 60)
{
old++; // old
etc.
}
else
{
young++; // young
etc...
if (weight > 500)
{
young_heavy ++; // young heavy
}
else
{
young_thin ++; // young non heavy
}
}
// all the logic above only dealt with age and weight
// regardless of which path it took everyone is meeting here
// notice how all the brackets have been paired and
// dealt with prior to this point --- important for example
// if the last right bracket was not entered yet then
// the statements that will follow will still be considered
// as part the "young" branch ( still in the "else' )
// and the problem will get worse .. all because of a
// bracket that was not entered
// Sure the system will flag errors for non matching brackets
// but it has no idea as to where you meant to put it-- so
// some people just throw one in at the end to pacify the
// computer (for matching pairs) and it does not solve the
// problem.
DECISION MAKING IN JAVA STUDY
CAREFULLY
Assume
input of rank,gender,height, weight
also assume male or female,
tall or short, sophomore or not, fat or skinny
If
( fat)
{
---- all fat
people ----
if(male)
{
---- fat males ---
if(tall)
{
------tall fat males ----
}
else
{
--- short fat
male
-----
}
}
else
{
--- fat female
-----
}
}
else
{
--- skinny ----
if ( tall )
{
--- tall skinny ----
if ( sophomore
)
{
--- tall
skinny sophomore
---
}
else
-- tall skinny
not sophomore ----
ONLY ONE STATEMENT
}
}
----
everybody meets here
next command