java - Why is there a "deadbranch" in my code? -
below code working fine until last if-else. appears i've done wrong boolean variables cangraduate , onprobation. perhaps i'm reassigning them incorrectly in prior if-else statements. deadbranch occurs @ else half of last if-else.
package lab5; import java.util.scanner; public class lab5 { public static void main(string[] args) { //creates scanner object scanner scanner = new scanner(system.in); //part ii //creating variables double gpa; int totalcreditstaken; int mathsciencecredits; int liberalartscredits; int electivecredits; boolean cangraduate = true; boolean onprobation = false; //prompts user imput system.out.println("what gpa?"); gpa = scanner.nextdouble(); system.out.println("what's total amount of credits you've taken?"); totalcreditstaken = scanner.nextint(); system.out.println("how many math , science credits have taken?"); mathsciencecredits = scanner.nextint(); system.out.println("how many liberal arts credits have taken?"); liberalartscredits = scanner.nextint(); system.out.println("how many elective credits have taken?"); electivecredits = scanner.nextint(); //creates first "if" statment determine if gpa high enough on track or on probation if (gpa < 2.0){ system.out.println("you're on academic probation."); onprobation = true; } //part iii //creates conditional see if there's enough credits graduate if (totalcreditstaken < 40 ){ system.out.println("you need more credit(s) graduate."); cangraduate = false; } else{ system.out.println("examining credit breakdown..."); cangraduate = true; } //part vi //nested if-else if-else determine if student qualifies ba or bs if ((mathsciencecredits >= 9) && (electivecredits >= 10)){ system.out.println("you qualify bs degree."); cangraduate = true; } else if ((liberalartscredits >= 9) && (electivecredits >= 10)){ system.out.println("you qualify ba degree."); cangraduate = true; } else{ system.out.println("you don't meet degree requirments."); cangraduate = false; } //part v //uses if statement either congradulate student or tell student take more classes if ((onprobation = true) || (cangraduate = false)){ system.out.println("you don't qualify graduate."); } else{ system.out.println("congradualations qualify graduate."); } } }
a bit more explanation what's happening here.
in java, =
operator assignment, not comparison (the comparison operator ==
). if a
int
, a = 3
means "put value 3 in variable a
".
but assignment expression. in addition putting value in variable, expression evaluates value assigned.
so value of expression a = 3
3. can things like:
system.out.println( = 3 );
this both put 3 in a
, , print 3
on console.
usually, java doesn't allow confuse between =
, ==
. if variable int
or float
or string
, writing statement like:
if ( = 3 ) ... // compilation error
will not work because value of expression 3, int
value, , if
expects expression of type boolean
. tell expression wrong, , you'll notice: "oh, meant ==
".
but if type of a
boolean
, writing a = false
or a = true
assignment, returns value assigned - boolean
. because of that, can write
if ( = false ) ... // compiles correctly
and compiler won't complain, because value of expression boolean
, that's if
expects. compiler doesn't know meant compare. knows got expression of appropriate type.
for reason recommended never compare boolean
variables @ all. instead of
if ( == true )
it correct write
if ( )
because if
succeed when a
true
, fail when a
false
. no need compare! it's important give variable name did - cangraduate
name, , statement like
if ( cangraduate )
is nicely readable "if [the user] can graduate...".
for false
, can use
if ( ! cangraduate )
it's not nice-sounding in english, it's clear enough, , clearer if ( cangraduate == false )
, added bonus not miss =
, write if ( cangraduate = false )
mistake.
Comments
Post a Comment