Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

How do we return an int in an if/else statement?

+14 votes
I am having trouble with the PracticeIt exercise 4.4, so if ya need some context take a look at that haha.
asked Sep 26, 2016 in CSC 211 (F16) by Hunter Bader (100 points)
yeah i'm confused on this as well because it wont allow me to create a new variable under the if statement to return and it wont let me return anything without a declared variable so i don't know where it belongs and cant find an if statement example with a return in the book
I declared the variable for days (as an int) right before the if statement, then returned it after all the if/else's were done.

2 Answers

+4 votes
 
Best answer
I assume you have figured out the if/else part of this problem; yes?

Check out page 158 of the text where you'll find an example of a method with a return statement. In that one (hypotenuse) it returns a double, but shows how to set the return type.
answered Sep 27, 2016 by Jon Clauss (100 points)
selected Sep 27, 2016 by Hunter Bader
Thank you so much. It was a problem of scope I believe. That page truly helped.
Yes, it *is* a problem of scope!
0 votes
you should make an if statement to check if the smaller of the 2 values a,b is a, then write a println statement, then declare int smaller and use if/else and return the integer
answered Oct 3, 2016 by Trung Le (Jayden) (100 points)
...