Java, Understanding - java

I have the following questions, about this Java Code:
public static void main(String[] args) {
int A = 12, B = 24;
int x = A, y = B;
while (x > 0) {
y = y + 1;
x = x - 1;
}
System.out.println("the solution is " + y);
}
What is beeing computed here?
My solution is, that it's (12-1)+(24+1) = 36. Please correct me if it's the wrong though.
For which A and B there will be an error?
Honestly, i though about A = 1 and smaller, but it didn't work....can someone help me out?
If there's an error, what is the readout?
I could not answer this, as trying out to get an error (for example setting A = -24) i just did not get an error but another solution.

Incrementing y, x times
There'll be no errors for any A and B. In the "worst" case, the loop won't be executed and the initial value of y will be printed
Irrelevant
I don't understand your title, there's nothing to do with String[] args here.
I'm unsure what's the purpose of this code, even for learning purposes..

Let's check this bit of your code:
while (x > 0) {
y = y + 1;
x = x - 1;
}
System.out.println("the solution is " + y);
This is a loop. A while loop, to be precise.
A loop will continue to iterate until the condition of the loop is false
In this case, you have x = 12 and y = 24. Since x value is positive, so it will enter the loop, and continue the calculation for each iterations.
Here are the results you'll get for each iteration:
x = 11, y = 25
x = 10, y = 26
x = 9, y = 27
x = 8, y = 28
x = 7, y = 29
x = 6, y = 30
x = 5, y = 31
x = 4, y = 32
x = 3, y = 33
x = 2, y = 34
x = 1, y = 35
x = 0, y = 36
When you get x = 0 and y = 36, the loop stops, because x = 0 and it violates the condition. So you get out of the loop. The last value of y is 36. That's what you're getting in you println(); it's not x + y, it's y alright.
But when x = -12, or x=-24 or any other negative number, the while condition is false. Your println() is outside the loop, so it will display the value of y which is outside the loop, i.e. 24.
You won't get an error for condition of while being false. Even if you println() x or y inside the loop, you won't get any result, but won't get any error either.

sum of A and B
when A<0 the sum is not calculated correctly
readout is B when there is error

Related

How can I erase the value of my variable inside of a for loop in Java

for(i = 0; i<=timePassed; i++) {
y = r * x * (1 - x)
System.out.println(y)
So how do I reset the value of y every time the for loop loops so that i can assign a new value to it? I cannot wrap my head around it I have already tried to set y to 0 at the end of the loop right after printing y but that didnt seem to work. I also tried setting it to null but that gave me an error(?) I cannot understand why this is happening can someone please explain how to do this and what I did wrong?
Are you able to update your code block to include ALL code from your class.java file? It would help us see what could be wrong, because from this code snippet, y is not saved outside of the loop, therefore for every iteration (cycle) of the loop it would be reset and re-assigned.
As someone else has said, the y value does not change at any time in the loop, as all variables used (other than y) are declared outside of the loop. This loop simply prints the same y value "timePassed" amount of times.
Here's the code that you posted, including line numbers:
1. for(i = 0; i<=timePassed; i++) {
2. y = r * x * (1 - x)
3. System.out.println(y)
4.
Here are various comments + fixes:
Line 1: assume i is declared somewhere, like this: int i
Line 1: assume timePassed is declared somewhere and has a value set, like: int timePassed = 5;
Line 2: assume y is declared somewhere, like: int y;
Line 2: assume r and x are both declared somewhere, and have values set, like: int r = 1, x = 2;
Lines 2-3: need semicolons to terminate each line inside the loop: add ; at the end of each line
Line 4: add closing brace } to the "for" loop
Here is the code with those edits applied:
int timePassed = 5, y, r = 1, x = 2;
for (int i = 0; i <= timePassed; i++) {
y = r * x * (1 - x);
System.out.println(y);
}
Running that code produces this output:
-2
-2
-2
-2
-2
-2
The output doesn't change. Why? Because the calculation to determine each new value of "y" is the same each time: r * x * (1 - x). Despite being inside a loop, and with i changing each iteration through the loop, i is not used in the calculation at all.
Here's an edit to the code, removing the entire "for" loop (and i), and it produces the same -2 value:
int y, r = 1, x = 2;
y = r * x * (1 - x);
System.out.println(y);
-2
To demonstrate that y is being set each time through the loop, here's a variation of the code (including output) where i is included in the calculation for y.
int timePassed = 5, y, r = 1, x = 2;
for (int i = 0; i <= timePassed; i++) {
y = r * x * (1 - x) * i;
System.out.println(y);
}
0
-2
-4
-6
-8
-10

Using assignment operator inside of expression

Running this code will return 11 while I was expecting 20. Why is that so?
int x = 1;
int y = x + (x = 10);
System.out.println(y);
Evaluation is from left to right. So
int y = x + (x = 10);
is (with x initially 1):
int y = 1 + 10;
Putting the assignment in () doesn't make it come first. It just ensures that it's a valid expression, since y = x + x = 10 would be y = (x + x) = 10 which would require assigning to something (x + x) that wasn't a variable.
If you want 20, put the assignment first:
int y = (x = 10) + x;
Of course, the vast majority of the time, it's best to avoid these kinds of side-effects and assign x a value outside the expression, breaking the expression up if necessary. Assignment-within-expression can be useful sometimes (particularly while ((blah = getNextBlah()) != null) sort of things), but only in limited situations.

JAVA println gives unexpected output

I have problem with JAVA code, that is I have a code:
class test {
public static void main(String[] args){
int x = 0;
int y = 0;
while( x < 5 ){
y = y - x;
System.out.print(x + "" + y + " ");
x += 1;
}
}
}
and I compile it in Command Prompt and I get this:
00 1-1 2-3 3-6 4-10
Where I expected output to be:
00 11 23 36 210
I don't know where the problem is.
Thanks for any responses.
The - in y = y - x; means subtraction.
Most likely you intended
y = y + x;
or
y += x;
Also most likely you expected.
00 11 23 36 410
the hyphen you see is not a hyphen but rather minus sign as y eventually becomes negative (based on ur code)
one way of doing it is by changing printout to be a positive value:
System.out.print(x + "" + y + " ");
to:
System.out.print(x + "" + Math.abs(y) + " ");
Your y is producing negative values:
Your code:
int x = 0;
int y = 0;
while( x < 5 ){
y = y - x;
System.out.print(x + "" + y + " ");
x += 1;
}
Let's try to debug it:
first iteration y = 0-0 = 0
second iteration y = 0-1 = -1
third iteration y = -1-2 = -3
fourth iteration y = -3-3 = -6
fifth iteration y = -6-4 = -10
As you can see, you're basically going opposite to your expected result which is a good indication that you're using an opposite operator on some variable, in this case y-x should be y+x.
Change y = y-x to y = y+x

Head First Java program explanation

my program and question are below
public class test {
public static void main(String[] args) {
int x = 0;
int y = 0;
while ( x < 5 ) {
y = x - y;
System.out.println(x + "" + y);
x = x+1;
}
}
}
So output for this is 00 11 21 32 42. I understand what happens when x is even but what when x is odd? Let's move to step 2 and make x=1, then we get
y=1-y
2y=1
y=1/2
For me output should be like 11/2 or sth like that
So how the hell output for this is 11? Do we use approximation? Thanks for answer.
No. You understand it wrong. You are doing math keeping the fact aside that evaluation of Java expression.
y = x - y;
means
y= 1-0;
Which is
y = 1
There's nothing off with your code, the result is correct. When x is 1, we set y to be equal to x - y. That means x and y are both 1, as 1 - 0 is 1. Remember that a = b in programming is an assignment and does not imply equality.
Isn't it obvious? By "=" you assigning new value into variable.
x = 0, y = 0
y = 0 - 0 = 0
print x y -> 0 0
x = 1, y = 0
y = 1 - 0 = 1
print x y -> 1 1
x = 2, y = 1
y = 2 - 1 = 1
print x y -> 2 1
etc...

Understanding a for loop and if condition

Below is come code that I am having trouble understanding. The output is 13 15 x=6. I understand how we get the number 13 because when we go through the loop the value of x is 5 and its corresponding y value is 12. So if x > 4 we then increment y which gives u the value of 13. The next number printed out then should be 14 because the next value of x is 6 and its corresponding y value is 13 so when you increment that it will be 14. However when I run the code its gives a different answer. Can anyone please help? Thanks
public class Output {
public static void main(String[] args) {
Output o = new Output();
o.go();
}
void go() {
int y = 7;
for (int x = 1; x < 8; x++) {
y++;
if (x > 4) {
System.out.print(++y + " ");
}
if (y > 14) {
System.out.println(" x = " + x);
break;
}
}
}
} ​
The reason is because ++y increments y before printing it.
y++ would do what you are expecting
When entering the loop iteration where x is 6, y is 13, as you said. Then y is immediately incremented to 14. x is greater than 4, so System.out.print(++y + " "); is executed. ++y increments y to 15 before it is printed.

Categories

Resources