Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue Statement in a Java Function

I want to create logic such that: if s2 is null the debugger skips all the complex string manipulation and returns null instead of s1 + s2 + s3 as seen in the first if block. Am I wrong somewhere?

public static String helloWorld(String s1, String s2, String s3){
   if(s2==null){
     continue;
     return null;
   }

   ... lots of string manipulation involving s1, s2 and s3.

   return (s1+s2+s3);
}
like image 675
bouncingHippo Avatar asked Nov 30 '25 16:11

bouncingHippo


2 Answers

don't use continue there, continue is for loops, like

for(Foo foo : foolist){
    if (foo==null){
        continue;// with this the "for loop" will skip, and get the next element in the
                 // list, in other words, it will execute the next loop,
                 //ignoring the rest of the current loop
    }
    foo.dosomething();
    foo.dosomethingElse();
}

just do:

public static String helloWorld(String s1, String s2, String s3){
   if(s2==null){
     return null;
   }

   ... lots of string manipulation involving s1, s2 and s3.

   return (s1+s2+s3);
}
like image 127
2 revsfer13488 Avatar answered Dec 03 '25 06:12

2 revsfer13488


The continue statement is used for loops (for, while, do-while), not for if statements.

Your code should be

public static String helloWorld(String s1, String s2, String s3){
   if(s2==null){
     return null;
   }

   ... lots of string manipulation involving s1, s2 and s3.

   return (s1+s2+s3);
}
like image 25
Brian Avatar answered Dec 03 '25 05:12

Brian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!