JAVA Learning Section ...... Thoughts and conditions in the loop

Source: Internet
Author: User
JAVA Learning Section ...... Thoughts and conditions in the loop-general Linux technology-Linux programming and kernel information. The following is a detailed description. All the friends who have used JAVA loops know that while (condition), do... while (condition)
And for (a; B; c. I just want to talk about it after I start learning.
Use the most commonly used While loop. See the following example:


Public class Loop
{
Public static void main (String args [])
{
Int num = 1;
While (num <10)
{

Num ++;
}
System. out. println (num );
}
}


At the beginning, I always felt "Faint", And I ++, ++, and so on, so we should
What should we do. I think it is best to better understand it through JDK debugging.
In the small example above, when the while loop ends, num must be 10,
In other cases, we should not consider how many times num ++ has been used.
A few times. (I have done this before, maybe you will laugh.) Let's give up
The content of the Ring Body is ignored first. In this way, it is much simpler. When num is 10,
That is, when num ++ is 10 (in the circular body, this 1.1 must be clear ),
Num ++ is 10, and num is actually only 9, that is, the number of times the loop body runs. In this way, is it true?
Although the output num in system. out. println is 10, the actual
The while LOOP only runs nine times. Okay, so that you can understand do... while (condition) Very well)
There is something in for (a; B; c.
Understanding the loop is to understand the conditional num of its variables <10 and the variable variation num ++,
If you can see clearly the cause and effect relationship, then how to 'transf' won't be 'faint', do you say yes?

As an important statement judgment, conditional statements play a self-evident role.
It also reflects the 'humanity' thinking. However, for our 'cainiao ', java
Hi, that's great. I can't understand it. Hey, don't worry, don't worry. Now I will
Three 'togs' (^; |; &) that are hard to understand in java's condition judgment;
First, we need to clearly understand their definitions.

A ^ B: in java, ^ is called an exception or judgment. It means that when only one of a and B is true, the operation
The result is true. The meaning is that the result is true only when the two sides are true.
Both conditions a and B must be judged to know whether they are true or not. See the following example:


Public class x
{
Public static void main (String args [])
{
Int I = 5;
Int j = 4;
If (I ++)> j ^ (j --)> I)
System. out. println ("I, j" + "\ n" + "I =" + I + "\ n" + "j =" + j );
Else

System. out. println ("I, j" + "\ n" + "I =" + I + "\ n" + "j =" + j) in else );
}
}


Output: I, j running in if
I = 6
J = 3

A & B: conciseness and in java: it means that when both a and B are true, the condition is true; its meaning
If Condition a is false, then the judgment of Condition B is not needed, and the statement after if is run directly.
I don't even think about whether Condition B is true or false. This is very similar to our general thinking;
Please refer to the following example to prove:


Public class x
{
Public static void main (String args [])
{
Int I = 5;
Int j = 5;
If (I ++)> j & (j --)> I)
System. out. println ("I, j" + "\ n" + "I =" + I + "\ n" + "j =" + j );
Else

System. out. println ("I, j" + "\ n" + "I =" + I + "\ n" + "j =" + j) Running in else );
}
}


Output: I, j
I = 5
J = 5
We can find that j is still 5 and j -- operation is not performed, which is the role of & switch;
It should also be noted that the order of judgment operations here is first ++ I, and then comparison of <;>;==
Finally, perform I -- operation. That is, compare I> j in (I ++) first, and then perform the I ++ operation.
Although I can see that there is a () number, java understands it in this way. If you don't believe it, try jdk;
In the following example, only I ++ is changed to ++ I, and j -- is changed to ++ j. The result is very different;


Public class x
{
Public static void main (String args [])
{
Int I = 5;
Int j = 5;
If (++ I)> j & (++ j)> -- I)
System. out. println ("I, j" + "\ n" + "I =" + I + "\ n" + "j =" + j );
Else

System. out. println ("I, j" + "\ n" + "I =" + I + "\ n" + "j =" + j) Running in else );
}
}


Result: I, j
I = 6
J = 4

A | B: It is concise in java or: it means that when both a and B are false, the condition is false; its meaning
If Condition a is true, then you do not need to make B's judgment and directly run the judgment statement in if.
Please refer to the following


Public class x
{
Public static void main (String args [])
{
Int I = 5;
Int j = 5;
If (++ I)> j | (++ j)> -- I)
System. out. println ("I, j" + "\ n" + "I =" + I + "\ n" + "j =" + j );
Else

System. out. println ("I, j" + "\ n" + "I =" + I + "\ n" + "j =" + j) Running in else );
}
}


Result:
I = 6
J = 5
Its running rules are the same as &, so we will not repeat them here.
Finally, thank you for reading my java Learning Section ...... Hope to make progress together with you;
Related Article

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.