Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 439 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
help a first timer learn java :D

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
Best 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 !
Answers
-
-
Building on the previous answer...
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!
-
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 !
-
THank! it worked but can i ask why should i put <= ? not ==
-
Take a look about Loops in Java
https://www.geeksforgeeks.org/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.