+ 2
Book for Java learning of Herbert Shildt
Why in book for learning Java of Herbert Shildt some examples are not right, already second case when I start book read not right when I create code here or in IntelIiJ and result different with results from book examples? It's a little confuse me when I do all like in examples but have another results.
7 Antworten
+ 5
public class Program
{
public static void main(String[] args) {
//first loop
int x;
for(x=0;x<10;x=x+1)
System.out.println("Meaning x is :"+ x);
System.out.println ("\n");
//second loop
int y;
for(y=0;y<10;y=y+1);
System.out.println("Meaning y is :"+ y);
}
}
/*see in second loop a semicolon is there end of the for loop so the ;(semicolon) will execute after condition become false but understand one thin when condition became false , when the value of y is <less than 10 so if the value of y became 9 its true but when the value of y become 10 then its false but y is 10 now and print y value
}
}
+ 5
in first loop it print 10 times
Meanning of x is :0
Meanning of x is :1
Meanning of x is :2
Meanning of x is :3
Meanning of x is :4
Meanning of x is :5
Meanning of x is :6
Meanning of x is :7
Meanning of x is :8
Meanning of x is :9
but if u want to print the value of x like this
System.out.println(x);
then it will print 10
because the condition false when the value of x will 10.
but in second loop it is execute for ; and semecolon is a valid java statement so after ending the loop it print the value of y i.e 10
may be same thing will happen in your book ,
+ 5
then the write did it by mistake
0
It might be outdated.
Can you post the code and tell us the expected result?
0
Ok, today will send it here
0
For example this one code from book:
class ForTest {
public static void main(String [] args) {
int x;
for(x=0; x<10; x=x+1)
System.out.println("Meaning x: " + x);
}
}
And in book he write that it must be result:
Meaning x: 0
Meaning x: 1
Meaning x: 2
And so on
But in real practice when I do it it give me only one result:
Meaning x: 10
Why?
0
still don't understand why Herbert Shildt write code, I write it same but have another results then his:(