#Why does IJ say there need to be a return statement?

75 messages · Page 1 of 1 (latest)

gilded hare
#

Title + where would I put a return statement?
public static int oddPositive(int n) {
int total = 0;
for (int x = 0; x <= n; x++) {
if (x % 2 != 0) {
total += x;
} else {
total += 0;
}
}
System.out.print(total);
}

fathom havenBOT
#

This post has been reserved for your question.

Hey @gilded hare! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fathom havenBOT
gilded hare
#

​public static int oddPositive(int n) { int total = 0; for (int x = 0; x <= n; x++) { if (x % 2 != 0) { total += x; } else { total += 0; } } System.out.print(total); }

pearl plaza
gilded hare
#

uh maybe?

#

See i tried thinking of it like python.. cause thats what i learned first, dont really understand why theres a ++

pearl plaza
gilded hare
#

Write a short Java method that takes an integer n and returns the sum of all the odd positive integers less than or equal to n.*/

pearl plaza
#

The method should return an integer, denoted by the return type in the method declaration. That means that when the flow of execution exits the method, it should do so with an integer value, which can then be assigned to a variable in the place where this method is called from

gilded hare
#

oh yea i also have this in main Exercises.oddPositive(3);

pearl plaza
#

All non-void methods must return a value in all possible branches the flow of execution takes

#

In your case, after the scope of the for loop, you need to add the return keyword, followed by the value you want to return, which in this case is contained within the variable total

#

As a sidenote, you don't need the else branch there that just increments the variable by 0. I'm pretty sure you know why that's useless. An if statement does not need to be followed by an else branch

gilded hare
#

it doesnt in java?

pearl plaza
# gilded hare See i tried thinking of it like python.. cause thats what i learned first, dont ...

Finally, the ++ is known as the increment operator. It is a single operand operator which can be used on numeric data types, and it basically just increments the variable by 1. Depending on whether you write the ++ before or after the variable, it is called the post and pre increment operator respectively. The former increments the variable first, then it retrieves the value from the variable, the former gets the value first, then increments

pearl plaza
gilded hare
#

so basically i can delete eveything in the else land

pearl plaza
pearl plaza
#

Just as there is the pre and post increment operators, there is also the pre and post decrement operator, written as --

gilded hare
#

so it should look like this? int total = 0; for (int x = 0; x <= n; x++) { if (x % 2 != 0) { total += x; return total; } System.out.print(total); }

#

idk why it isnt lined up

#

but we dont talk about that

pearl plaza
#

No. The return should be after the scope of the loop, not inside the scope of the if branch

gilded hare
#

so basically im learning.. that i need to turn off python brain

pearl plaza
#

Because you want it to iterate over all numbers before n, then return the sum

#

If you leave it like that, it would just start iterating at 0, the if check will fail, iterate to 1, the if statement passes, it increments total by 1, and then it returns, leaving you with only 1

#

In this context, scope means the lines of code surrounded by the curly braces {} of an expression

gilded hare
#
         int total = 0;
         for (int x = 0; x <= n; x++) {
             if (x % 2 != 0) {
                 total += x;}
         return total;
         System.out.print(total);
     }```
pearl plaza
#

I think you are missing a closing curly brace before the return

#

Since you have two tested control flow statements

gilded hare
#
         int total = 0;
         for (int x = 0; x <= n; x++) {
             if (x % 2 != 0) {
                 total += x;}
             }return total;
         System.out.print(total);
     }```
pearl plaza
#

Closing bracket xd

#

You can just edit the message

gilded hare
#

see now its telling me the system.out.print is unreachable

#

these brackets are gonna be the fuckin death of me

#

OHH

#

IT HAS TO BE THE OPPOSITE WAU

pearl plaza
#

Python being the only language that defines scope with white space and indentation 😔

gilded hare
#

I have python brain mann

#

why is it outputting like this Here is the sum of the odd:0 Here is the sum of the odd:1 Here is the sum of the odd:1 Here is the sum of the odd:4

pearl plaza
#

Can you show me your current code?

gilded hare
#

main Exercises.oddPositive(3); other file ```public static int oddPositive(int n) {
int total = 0;
for (int x = 0; x <= n; x++) {
if (x % 2 != 0) {
total += x;}
System.out.print("\nHere is the sum of the odd:" + total);
}return total;
}

#

at least the last one of the output is correct

#

nvm

#

i fixed

pearl plaza
#

Think about the flow of execution in your program. You have your print statement within the scope of the for loop. This means that for every iteration of the loop, you are printing out the value of the total variable that it has at that moment

gilded hare
#

I did int odd = Exercises.oddPositive(3); System.out.print("\nHere is the sum of the odd:" + odd); instead

pearl plaza
#

And what are you getting now?

gilded hare
#

Here is the sum of the odd:4

#

our teacher showed us that way with boolean so.. i thought it could work

pearl plaza
#

1 + 3 = 4

gilded hare
#

WOO HOO it works

#

two down two more to go...

#

out of curosity would there have been an easier way to do that

#

this way makes sense but knowing other ways is nice

pearl plaza
#

I don't know about easier way, but I do know about a shorter way

#

I know 2 shorter ways, one easy to understand, and one harder to understand. Do you want to see them xd

gilded hare
#

ye

#

cause this was supposed to be "short" and idk the definition of short

#

cause i had to do sum of all positive intergers and that was uh 3 lines

pearl plaza
# gilded hare ye

short easy to understand

    private static int oddPositive(int n){

        int total = 0;

        for(int x = 1; x <= n; x += 2){
            total += x;
        }

        return total;

    }

short hard to understand

    private static int oddPositive(int n){

        int total = IntStream.rangeClosed(1, n).filter(i -> i % 2 != 0).sum();

        return total;

    }
#

first one starts iterating from one instead of 0, but instead of incrementing the looping variable by 1 for each iteration, it increments it by 2. That way you know that x will never be an even number, so you can avoid the extra check

#

the second one is based on a programming paradigm called functional programming. Essnetially, I'm creating a list of numbers than range from 1 to the inputted number inclusive, then applying a filter function that only leaves the odd numbers, then summing them all up

gilded hare
#

the second one is like the for x in range, in python

pearl plaza
#

maybe? I don't know? Im not familiar with Python

gilded hare
#

in python, in range returns a list of numbers, which then you can do whatever you want with

pearl plaza
#

anyways. Let me know if you need further help

gilded hare
#

yes person! I might be back tomorrow to deal with a different one I dont know how to do

#

how does one close this

pearl plaza