0% found this document useful (0 votes)
76 views15 pages

If The Test Is True Execute The Controlled Statements Execute The Statements A2er The If - Statement

The document describes different types of if statements in Java including basic if statements, nested if/else statements, and multi-branch if/else if statements. It provides examples of how to use each type of if statement to control program flow based on different conditions. It also discusses some best practices for structuring if statements, such as reducing redundancy and nesting.

Uploaded by

Srihari Madhavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views15 pages

If The Test Is True Execute The Controlled Statements Execute The Statements A2er The If - Statement

The document describes different types of if statements in Java including basic if statements, nested if/else statements, and multi-branch if/else if statements. It provides examples of how to use each type of if statement to control program flow based on different conditions. It also discusses some best practices for structuring if statements, such as reducing redundancy and nesting.

Uploaded by

Srihari Madhavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

if

 the  
true  
test  is  
true  

Basic  if  statement  construct   execute  the  


false   controlled  
statements  

execute  the  
statements  
a2er  the  if-­‐statement  
Can  you  spot  the  problem  in  this  code?  
Scanner console = new Scanner(System.in);
System.out.print(”How many magazines did you sell? ");
int percent = console.nextInt();
if (totalSales >= 50) {
System.out.println(”Congratualtions, you win an iPad");
}
if (totalSales >= 35) {
System.out.println("Congratualtions, you win an iPod");
}
if (totalSales >= 25) {
System.out.println("Congratualtions, you win a stylus");
}
if (totalSales >= 15) {
System.out.println("Congratualtions, you win a gift card");
} else {
System.out.println(”Keep up the good work!");
}
...
suppose you
sold 72
A  closer   no  
>=50?  
magazines

look  at  the   yes  

control   you  get  an  iPad  

flow  
no  
>=35?  

yes  

you  get  an  iPod  


Change   no  
the  tests,   >=50?  

making   yes  

each  one   you  get  an  iPad  

more   no   50  >  
sold  
explicit   >=35?  
yes  

you  get  an  iPod  


What  we  really  want…  
Scanner console = new Scanner(System.in);
System.out.print(”How many magazines did you sell? ");
int percent = console.nextInt();
if (totalSales >= 50) {
Greater  than  or  equal  to  50  -­‐you
System.out.println(”Congratualtions,  iPad  win an iPad");
}
if (totalSales >= 35) {
Otherwise,  if  greater  than  or  equal  tyou
System.out.println("Congratualtions, o  35  
win -­‐  iPod  
an iPod");
}
if (totalSales >= 25) {
Otherwise,  if  greater  than  or  equal  tyou
System.out.println("Congratualtions, o  25  win
-­‐  stylus  
a stylus");
}
if (totalSales >= 15) {
System.out.println("Congratualtions, you win a gift card");
} else {
System.out.println(”Keep up the good work!");
}
...
BeLer,  but  poor  style  
if (totalSales >= 50) {
System.out.println(”Congratulations, you have won an iPod");
} else {
if (totalSales >= 35) {
System.out.println("Congratulations, you have won an iPod");
} else {
if (totalSales >= 25) {
System.out.println("Congratulations, you have won a stylus");
} else {
if (totalSales >= 15) {
System.out.println("Congratulations, you have won a gift card");
} else {
System.out.println(”Keep up the good work!");
}
}
}
}
Try  the  mulN-­‐branch  if/else
Instead  of  deeply  nesNng  several  tests,  use  else-­‐if  statements  
Test  the  else-­‐if  when  no  other  test  has  been  true  thus  far  
if (test) {
statement(s);
} else if (test) {
statement(s); if  test  is   true  
} else { true  

statement(s); true  
if  test  is  
} true  

Here three statements can be


controlled with two tests
Try  the  mulN-­‐branch  if/else
•  Example:  
if (inputX > 0) {
System.out.println("Positive");
} else if (inputX < 0) {
System.out.println("Negative"); if  test  is   true  
} else { true  

System.out.println("Zero"); true  
if  test  is  
} true  

There are three statements to execute, one


for each condition. Two tests will suffice.
Suppose  there  are  three  statements,  one  for  each  condiNon  and  a  
third  case  where  no  statement  should  be  executed.    This  model  
will  not  accommodate  that  situaNon  

we have one more case


that should bypass all
statements
MulN-­‐branch  if/else if
•  If  the  set  of  condiNonals  ends  in  else,  one  code  path  must  be  taken.  
•  If  the  set  of  condiNonals  ends  with  if,  the  program  might  not  execute  any  
path;  there  is  a  bypass.  
if (test) {
if  test    
statement(s); true  
} else if (test) {
if  test    
statement(s); true  
} else if (test) {
if  test    
statement(s); true  
}  
MulN-­‐branch  if/else/if
if  test    
true  

if  test    
true  

if  test    
true  
Example:  
if (grade == ‘A’) {

System.out.println("You are eligible for the Dean’s list!");

} else if (grade == ‘B’) {

System.out.println("You are eligible for the Honors list!");

} else if (grade == ‘C’) {

System.out.println("You still have time to raise your


grade.");

}
Review  if  statements  
•  Devise  scenarios  for  different  types  of  if  statements  
•  Consider  
–  How  many  different  outcomes?  
–  Should  there  be  a  path  where  no  statement  is  executed?  
–  Can  you  use  well  designed  tests  to  combine  condiNons  and  
eliminate  if  branches?  
–  Can  you  reduce  redundancy?  
Returning  to  our  game  problem  
•  Three  condiNons:  every  player  falls  into  one  of  
these  condiNons  
NE  
–  NW,  purple   True   or   False  
SW?  
–  SE,  red   False   True  
SE?  
–  SW,  NE,  green  
•  if/else  if/else  
•  try  wriNng  this  code  
Assume  a  player’s  posiNon  is  given  as  x,y  coordiantes  p1.x  and  
p1.y    

Note  that    
   posiNve  x  *  posiNve  y  =  posiNve  result,  NE,  green  
   negaNve  x  *  negaNve  y  =  posiNve  result,  SW,  green  
   posiNve  x  *  negaNve  y  =  negaNve  result,  SE,  red  
   negaNve  x  *  posiNve  y  =  negaNve  result,  NW,  purple  
   posiNve  x  *  posiNve  y  =  posiNve  result,  NE,  green  
   negaNve  x  *  negaNve  y  =  posiNve  result,  SW,  green  
   posiNve  x  *  negaNve  y  =  negaNve  result,  SE,  red  
   negaNve  x  *  posiNve  y  =  negaNve  result,  NW,  purple  

int position = p1.x * p1.y;

if (position >= 0) {
p1.color = green;
} else if (p1.x > 0) { Only players with
negative location
p1.color = red;
will test the p1.x
} else { therefore the only
p1.color = purple; two cases left are
} red or purple

You might also like