Several Java topics

Source: Internet
Author: User

15. What are the results of the following program execution? B
int i = 100;
while (true)
{
if (i++ > 100)
{
Break
}
System.out.println (i);
}
A. 100
B. 101
C. 102
D. Error


Analysis: The trap of this problem is if (i++>100), we usually always if (i>100), but there is more than one + +, we first peel the parentheses, first, we see this is an infinite loop, the condition of the loop termination is when i++>100, The execution statement of the loop is the value of the print I,
Of course if (i++ > 100)
{
Break
}
is also part of the loop statement, which is the while loop embedding the IF statement, similar to the For loop embedding if statement we saw earlier, then we must really make it clear that break inside the IF statement is the real execution statement ...

So let's strip this code out of parentheses and analyze it.
int i = 100;
while (true)
{

if (i++ > 100)

Break The true execution statement, in addition to the loop condition, requires the IF condition to be satisfied to execute

System.out.println (i); The true execution statement executes when the loop condition is satisfied.
}

We have to analyze the execution flow of this statement, obviously the break statement precedes the print statement, then, meaning that if a break condition is encountered in a loop, the printed statement of this cycle will not execute
So, since this is an infinite loop, the number of cycles depends on when to jump out of the loop, so, if (i++>100) This statement inside, buried there is a trap, i++>100 this is a relational expression, is a relational operation, and because the suffix + + is the first to participate in the operation and then self-increment, so, Of course, first of all participate in relational operations, that is, whether I is greater than 100, then I self-increment 1, then 100>100, obviously not set up, so, although at this time, the loop has been started, or the loop condition has been satisfied, but the condition of the IF statement is not satisfied, so will not execute break, therefore, According to the order of execution from top to bottom of the statement, this time, you should execute the PRINT statement, printing is the value of I, then print out the value of I exactly how much?????? Is it 100 or 101???

Here, we have to define the real meaning of the increment after the first operation, the problem is to participate in relational operations (> Operations), and then self-increment, but the self-increment is immediately self-increment, that is, the process of self-increment 1 even occurs before the break, although break is the if condition judgment immediately after the execution of the statement, But the self-increment 1 action is also before the break statement execution, of course, also in System.out.println (i), before this statement, so System.out.println (i), when executed, I has completed the process of self-increment 1, so the printed value is 101 ...

The trap of this problem is i++, that is, I self-increment 1 of the process is occurring in the IF condition statement, so we do not think that i++ occurs in the IF condition statement will not take effect, in fact, is effective, this and for loop statement similar, because usually, we often see for (int i=1;i <=100;i++) {} Such statement, the i++ of such a statement is in the parentheses of the for, in fact, he is also in force, that is, each cycle, I self-increment 1, and the following while statement similar

int i=1;
Whie (i<=100) {i++;} At this time, i++ moved to the loop body statement inside, is the same effect

So, after printing out 101, the second loop is executed, the second, the loop condition is true, the loop satisfies, and we look at the IF condition, at which time the 101>100 is established, so execution break, jump out of the loop, because break before the print statement, so before the second printing, the loop has ended , so there can be no second print ... So the result 101

Therefore, when we do not know how many times the loop is, but need to loop at a certain point, you can use the infinite loop statement
while (true) {
if () {break;}
}

Whether it's a for loop, a while loop, or even a do While loop, we just remember that everything is a transformation of the three elements of the loop


Example: Look at the following procedure, say the value of a B c is how many??????


public class test{
public static void Main (string[] args) {
int a=1,b=2;
int c= (A+B>3?A++:++B);

System.out.println (a);
System.out.println (b);
System.out.println (c);
}
}

For this problem, the trap is the prefix + + and suffix + +, but the first to participate in the operation and then self-increment, or the first to participate in the operation, what is involved in what operation??????
int a=1,b=2;
int c= (A+B>3?A++:++B);
For this problem, we have to pay attention to a++ that a first to participate in the operation, after the self-increment 1,++b means B to increment 1, and then participate in the operation, then what is involved in what operation?????? In fact, a+b>3 this relationship expression, or relational operations, where A and b are coming from??? At first, I was also confused, must not think a++ and ++b to participate in the operation of A+b>3, it must be understood that the a++ and ++b behind is the result, why is the result, only in the a+b>3 to determine whether it is established after the decision is to return a++ or ++b, a++ and ++b to play its role, so say a+b>3 this inside of a and B is inherited from the above statement, so a=1,b=2 1+2>3 obviously not set up, so the return is ++b, just said, ++b said that the first self-increment 1, and then participate in the operation, so far, can understand, b to increase by 1, The value is 3 and then participates in the assignment operation because it returns a value, so assigning the value of B since increment to C is an assignment operation, but a++ because there is no return, or is masked, the value of a is still 1,c with a value of 3


This problem more pit father: And look I slowly analysis
Boolean b=true;
if (B=false)
{
System.out.println ("a");
}
else if (b)
{
System.out.println ("B");
}
else if (!b)
{
System.out.println ("C");
}
Else
{
System.out.println ("D");
}
For this problem, we first see that the Boolean variable B is assigned to true, the problem is the most important place is the if parentheses inside the content, I previously mistakenly known as if the parentheses can not be a variable, in fact, this understanding is completely wrong, it is very deep, we are now to gradually uncover the veil of this problem, First if (B=false), which b=false means to assign a constant false to B, then B is assigned to false, so does not print a, and if (b), because above in the IF (B=false) in parentheses, we have the value of B is assigned to false, So this is equivalent to if (false), so it will not print B, I said, in the if parentheses define the variable or the self-increment variable and the same effect outside the parentheses, that is, the front b=false; the change of the value assigned to B to false,b will affect the back, This place is also I have not noticed before, this is also a very subtle place, since will not print B, then we see, if (!b) actually!b this inside the B inheritance in front of false, so!b is true, so print C, this topic is extremely abnormal, should be given enough attention


is not proficient is the summation such as sun=sum+i; This means that I is an increment

There is a loop embedded if, to pay attention to
for () {if () {}};
while () {if () {}};


Common class public classes, in a source file (. java file), can have at most one (or none) of the publicly class

Several Java topics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.