If Statement


Collapse Content

Code is normally executed in order from top to bottom, but you can make it skip around with control-flow statements which execute code only when a condition is true.

If

The most basic control-flow statement is the if statement. You can tell the computer to only run certain code when a condition is true. For example, you might want to give a bonus to a player who hits over 100 targets in a game:

if hits > 100 then
add 10 to score

In Java, an "if" statement has parentheses around the condition, followed by curly braces around the code:

if(condition){
  //then do stuff here
}

Or, in the above example:

//...code before

if(hits > 100){
   score = score + 10;
}

// continues with code after...

If then else

If you want some code to run when the condition is true, and other code to run when it is false, you can use an if-then-else statement by adding an else after the if.

if(hits > 100){
   score = score + 10;  //large bonus
}
else{
   score = score +1; //small bonus
}

This can also be seen in flowchart form:

If-then-else-flowchart

You can check many different conditions by combining if-else-then statements:

if(hits > 100){
  score = score + 10; 
}
else if( hits > 90){
  score = score + 6 ;
}
else if(hits > 80){
  score = score + 3;
}
else{
   score = score +1; 
}
//code continues

The computer will stop checking the remaining conditions once it 'meets' a true condition, since it only goes to else when the if condition is false. For example, if hits is 92, then score will increase by 6 and the code will then continue executing from //code continues.

Challenge

You are working on the Collatz Conjecture, an unsolved problem in math.

You will be given one number a as input. If a is even, return half of a (a / 2). If a is odd, return 3 * a + 1.

For example, when given 5 return 16, and when given 16, return 8.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • This is really hard :/

  • Meho the Wormcloack finds this easy

  • This is about the properties of int...

  • Cool problem, I like how you link it to a current problem that coding can be used to explore :D Keep up the good work guys! By the way, is / performing integer division?

  • @Stanley, if both numbers are integers, / will lose the decimal place from the answer.

  • Ah, I was a bit confused but now I realise that we only want integer outputs for the Collatz Conjecture anyway. Thanks for the help :D

  • public class Main {

    public static int doStuff(int a){
        int NrQift = a % 2;
        if(a == NrQift){
            return a/2;
        }
        else{
            return 3 * a + 1;
    
    cont...
  • I figured it out already after giving a bit more thought on the "Hint" xD Ty anyway whoever was gonna reply :)

  • Pretty sure this should work...

    if (a == a % 2) {
    return a / 2;
    }
    else {
    return (a * 3) + 1;
    }

    why not?

  • @ArtTric
    That won't work because a%2 should equal 0 (meaning that there is no remainder and therefore the number is even) and not equal to a.

  • So simple...
    int c=a%2; // take remainder of the input and save it
    //in variable c.

     if(c==0){
         return a/2;   //if the remainder == 0.
    
    cont...
  • I've done some work on the Collatz Conjecture before, so I kinda knew the answer. This is simpler than what has been posted already:

    if (a % 2 != 0) { // odd
    return (a * 3) + 1; // a times 3 plus 1
    } else { // even
    return a / 2; // a divided by 2
    }

  • @mina the operation needs to be on the right side. Its just a programming rule. example :

    if (x - 2 = 0) // error

    if (x = 2) // correct

    it wont work in this example but thats just the rule.

  • Why was I wrong?

    if (a % 2 == 0) {
    return a / 2;
    }
    else {
    return (a * 3) + 1;
    }

  • Took me a while but finally figured it out. One thing that kept tripping me up was =. I understand that the equal sign in Java is used to say that an expression is true right? When is it appropriate

    cont...
  • The solution is as straight-fwd as the problem, so apply simple maths and follow syntax.

  • I want to become a hacker, so i guess i have to learn all of this first :-)

  • needs more sauce

  • How we are using doStuff method in main class ?

  • what is wrong in below mentioned code
    package basicpgm;
    import java.util.Scanner;

    public class Main {

    public static int doStuff(int a){
        return 5*3+1;
        }
    
    public static void main(String[] args) {
            int result = doStuff(a);
            System.out.println(result);
        }
    }
    
  • if ( (x & 1) == 0 ) { even... } else { odd... }
    the low bit will always be set on an odd number.

  • This is so good I love this

  • import java.util.Scanner;

    public class Main {

    public static int doStuff(int a){
        if(a%2 != 0){
            return(3*a + 1);
        else{
            return (a/2);
    
    cont...
  •     if (a%2==0)
        {
            return a/2;
    
        }
        else
        {
            return 3*a+1;
    
        }
        i dont know what is wrrong in this
    
  • I have figured out:
    import java.util.Scanner;

    public class Main {

    public static int doStuff(int a){
        a=5;
        int s=a%2;
        int b=2;
        int c=4;
    
    cont...
  • this will do it
    // this if/else block will give result for any number a
    if (a%2>0){
    return 3*a+1
    }
    else{
    return a/2;

  • ya this is really confusing

All Node Comments
Contact Us
Sign in or email us at [email protected]