## Forum Stats

• 3,826,035 Users
• 2,260,587 Discussions

Discussions

# help a first timer learn java :D

Member Posts: 2
edited Mar 19, 2018 3:28PM

HI i am new here actually this is the first forum group i've ever been in. i started learning my first language yesterday (java) and i had this assignment for "loops" ;

Let’s consider the folowing operations on a given integer:

• if the number is divisible by 3, 4 is added to it;

• if it is not, but is divisible by 4, it is divided by 2;

• if it is divisible neither by 3 nor by 4, 1 is substracted from it.

These operations are repeated successively until the result is zero. Concretely, if we start from an integer n0, the operations are applied to n0 which gives the number n1; then if n1 is different from zero, the operations are applied to n1 and son on, until we reach the number nk with value 0. For example if we start from 7, the following suite of number is generated: 6, 10, 9, 13, 12, 16, 8, 4, 2, 1 and0. The operations are repeated k = 11 times. If we start from 1, the value zero is immediately reached and the number of repetitions is then k = 1. You are asked to write a program which displays how many times the operations must be repeated until zero is reached. The number of repetitions must be displayed for any starting number comprised between two integers read from the keyboard. For example, for all starting numbers lying between 1 and 7, your program must produce the following output:

1 -> 1

2 -> 2

3 -> 12

4 -> 3

5 -> 4

6 -> 10

7 -> 11

--------------------------------------------------

i wrote this and i had no execution pls help, dont know what have done wrong;

public class suite {

public static void main(String[] args) {

int debut = 1 ;
int fin = 7 ;

for (int y = debut; y==fin ;y=y+1) {

int i =y;

int x = 0;

while (i!=0) {

if ((i % 3) == 0) { i = i+4; }

else if (((i % 3) != 0) && ((i % 4) == 0 )) {i = i/2;}

else{i=i-1;};

}

System.out.println(y+ "->" +x);
;
}

}

}

--------------------------

THANKS

• Member Posts: 214 Bronze Badge
edited Mar 19, 2018 2:57PM Answer ✓

Hi,

You want execute the for loop until the debut is less than or equal to fin ( y <= fin ).

Your code is for (int y = debut; y==fin ;y=y+1).  The conditional y==fin is not right.

Change the for loop

for (int y = debut; y <= fin; y = y + 1)

Happy coding !

• Member Posts: 214 Bronze Badge
edited Mar 19, 2018 1:51PM

Hi,

You can get quickly response at

Good luck !

• Member Posts: 584 Bronze Crown
edited Mar 19, 2018 2:35PM

In the ACTIONS menu on the right, you should be able to click on "Move", and then specify the correct space, so that the Java experts can see your question.

Good luck!

• Member Posts: 214 Bronze Badge
edited Mar 19, 2018 2:57PM Answer ✓

Hi,

You want execute the for loop until the debut is less than or equal to fin ( y <= fin ).

Your code is for (int y = debut; y==fin ;y=y+1).  The conditional y==fin is not right.

Change the for loop

for (int y = debut; y <= fin; y = y + 1)

Happy coding !

• Member Posts: 2
edited Mar 19, 2018 3:05PM

THank! it worked but can i ask why should i put <= ? not ==

• Member Posts: 214 Bronze Badge
edited Mar 19, 2018 3:28PM

Take a look about Loops in Java

`Syntax:`

`for (initialization condition; testing condition;                               increment/decrement){    statement(s)}1. Initialization condition: Here, we initialize the variable in use. It marks the start of a for loop. An already declared variable can be used or a variable can be declared, local to loop only.2. Testing Condition: It is used for testing the exit condition for a loop. It must return a boolean value. It is also an Entry Control Loop as the condition is checked prior to the execution of the loop statements.3. Statement execution: Once the condition is evaluated to true, the statements in the loop body are executed.4. Increment/ Decrement: It is used for updating the variable for next iteration.5. Loop termination:When the condition becomes false, the loop terminates marking the end of its life cycle.`

The loop is executed until a conditional expression is false. In your case, y==fin is always false and that's why never execute.

With less than or equals, the conditional expression y<=fin is true until y is greater than fin.

This discussion has been closed.