2013年10月11日星期五

About the for loop a small problem !

Under discussion : for (int i = 0; i <10; i + +) {
/ / print the value of i
}
for (int k = 0; k <10; + + k) {
/ / print the value of k
}
Question: Why is the 0-9 second case ?


------ Solution ------------------------------------ --------
increment , not involved in the expression of other operations, so the effect is the same.
------ Solution ---------------------------------------- ----
i + + and + + i the difference between these two expressions used only when their values ​​will be manifested , for example,
a = i + +;
b = + + k;
i == k time , a less than b 1 . However, for i and k , there is no difference , 1 is added .
in the for statement , and did not use the third value of the expression , i and k is used to change the values ​​of these two variables themselves only.
------ Solution ---------------------------------------- ----
both before adding or added , the end result is to 9 , i + + and + + k is content after the end of the cycle performed separately .
------ Solution ---------------------------------------- ----
i + + and + + i in the value of i is executed after adding a

but as an expression of their operation result is not the same

In the two cycles are used to judge the value of i is the same so that the output is to understand the for loop execution

hope useful for you
------ For reference only ------------------------------ ---------
one is the first increase after the implementation of a first implementation is added , so that made the difference
------ For reference only -------- -------------------------------
debug it, very good understanding of
------ For reference only ---------------------------------------
both raising and then perform or the first initial value is 0 after the execution plus limit is 9 so the end result is 0 1 2 3 4 5 6 7 8 9
- ----- For reference only ---------------------------------------
this also needs to discuss it ? You see ah , i and k ranges are 0-9 , the result is inevitable ah
------ For reference only ----------- ----------------------------

positive solution . . . + + K, i + + is a complete re- implementation cycle . .

So, on to the next : for (int i = 0; i <10; i + +) {
/ / print the value of i
}
for (int k = 0; k <10; + + k) {
/ / print the value of k
} wood are different. . .
------ For reference only -------------------------------------- -
public class Test
{
public static void main (String [] args)
{
for (int i = 0; i <10; i + +) {
System.out.println (i) ;/ / 0到9
}

for (int j = 0; j <= 10; j + +) {
System.out.println (j) ;/ / 0到10

}

for (int m = 1; m <= 10; m + +) {
System.out.println (m) ;/ / 1到10
}
}
}
------ For reference only --------------------------------- ------
doubt debug trace it again to understand
------ For reference only ------------------ ---------------------
executed after the first + + with the first run after the same ah
------ For reference only - --------------------------------------
both the same, said the reason LS

but experts say + + i will be better with a little
------ For reference only -------------------- -------------------
i + +, i value in adding a ;
+ + k, k before adding one , then the value ;
Whether before adding , or added , the operation is complete, the value of the variable is incremented by 1 ;
for loops are the first after this step , come again to judge the loop condition is satisfied.
So , the same results .
------ For reference only -------------------------------------- -


Thank you, understand.
------ For reference only -------------------------------------- -
I knew where I was wrong , and right for () the execution logic wrong. Is not so : for ( initial value ; condition ; stepper ) { loop body } , the first run , look at the initial value is the number, and then see if the condition is satisfied, satisfied run the loop body before executing the steps.

I put in operation or in the case of an assignment and for confusion , I had thought that way : look at the initial - > Conditions - > Then Stepper - > ; the loop body - > conditions - > Step - > loop of ( and i + + and + + k the situation is different ) , so I think it should be the second case 1-10 , uh ,
------ For reference only ------------------------------ ---------
+ + k;
and k + +; are the same
------ For reference only --------------------------- ------------
the for loop does not embody coming ! ! When in operation , or if the judge with i + + or + + i can reflect the difference between them !
------ For reference only -------------------------------------- -
above all positive solutions, had Cougerenao slightly
------ For reference only ----------------------- ----------------

+
------ For reference only -------------- -------------------------
below are thinking In Java 4th Edition original:
Any of the expressions initialization, Boolean-expression or step can be empty. The expression is tested before each iteration, and as soon as it evaluates to false, execution will continue at the line following the for statement. At the end of each loop, the step executes.
translation: initial , conditions and steps can be empty, each round of checks are performed before the loop conditional expression , a Boolean conditional expression is false then out of the loop , execute the statement following the for loop . At the end of each round of cycle , perform steps.
------ For reference only -------------------------------------- -
so to speak, whether it is i + + or + + k, are executed in the body of the program , and constraints unrelated
------ For reference only ------ ---------------------------------
floor positive Solutions
------ For reference only ---------------------------------------
top
--- --- For reference only ---------------------------------------
i + + and + + k, + + No. the only difference between before and after the variable in this statement can be used only when the value of the expression manifested
------ For reference only ------------ ---------------------------
independent operator does not matter about plus
------ For reference only ---------------------------------------
should be due to i + + and + + k is the for loop body content after the implementation , so it did not affect the implementation of the printed results myself , the landlord discovered the problem a good kind of how I never pay attention to
------ For reference only ---------------------------------------
circulation while transforming the above look out soon after
1. for (int i = 0; i <10; i + +) into while after

int i = 0;
while (i <10) {
System.out.println (i);
i + + ;/ / ---------------------- a
}

2. for (int i = 0; i <10; + + i) into while after
int i = 0;
while (i <10) {
System.out.println (i);
+ + i ;/ / ---------------------- b
}
comparison, either case , are the first printing, and then i add 1 to the premises in a and b is the increase of a different way , one is to add a further argument , one is to add a value , but the results are the same, all i added a ,
So , like ah.
------ For reference only -------------------------------------- -


upstairs . This is not the same , + + k is the sum of the first run , and k + + is executed after the additive
------ For reference only ---------- -----------------------------
no difference between the two for loops , k values ​​are from 0-9 < br> ------ For reference only ---------------------------------------
this actually on Home ? ! ~ Mai the fight
------ For reference only --------- ------------------------------
+ + i and i + +, i values ​​are increment one , but not the same as the value of the entire expression , (+ + i) increase in value after taking i , (i + +) to increase the value before taking i . In addition , for loop executes the process: first obtain the value of the variable , and then determine the loop condition , condition is met , the loop body is executed once , after the implementation of the third expression. This cycle until the condition is no longer satisfied so far !
------ For reference only ---------------------------- -----------
still discussing this change results posted ah
------ For reference only ----------- ----------------------------
i + + and + + k the difference is not reflected here

------ For reference only ---------------------------------- -----
3 Floor positive Solutions
7 F Detailed
------ For reference only ------------------------------- --------
for (int i = 0; i <10; i + +) {
/ / print the value of i
}
for (int k = 0; k <10; + + k) {
/ / print the value of k
}
Question: Why is the 0-9 second case ?

on here is no difference between , on the for loop , if the call i, k will see the difference, for example, in which you add a
int sumi = 0;
int sumk = 0;
for (int i = 0; i <10;) {
sumi + = i + +;
}

for (int k = 0; k <10;) {
sumk + = + + k;
}
System.out.println ("" + sumi + sumk);
this result is not the same
------ For reference only ---------------------------- -----------
for (int i = 0; i <10; i + +) {
/ / print the value of i
}
for (int k = 0; k <10; + + k) {
/ / print the value of k
}
Question: Why is the 0-9 second case ?

on here is no difference between , on the for loop , if the call i, k will see the difference, for example, in which you add a
int sumi = 0;
int sumk = 0;
for (int i = 0; i <10;) {
sumi + = i + +;
}

for (int k = 0; k <10;) {
sumk + = + + k;
}
System.out.println ("" + sumi + sumk);
this result is not the same
------ For reference only ---------------------------- -----------
for (int i = 0; i <10; i + +) {
/ / print the value of i
}
for (int k = 0; k <10; + + k) {
/ / print the value of k
}
Question: Why is the 0-9 second case ?

on here is no difference between , on the for loop , if the call i, k will see the difference, for example, in which you add a
int sumi = 0;
int sumk = 0;
for (int i = 0; i <10;) {
sumi + = i + +;
}

for (int k = 0; k <10;) {
sumk + = + + k;
}
System.out.println ("" + sumi + sumk);
this result is not the same
------ For reference only ---------------------------- -----------
not limit your first add and added , from the overall point of view, in fact, no matter what the situation is the same as the number of executions are from zero to nine , so the result is the same !
------ For reference only -------------------------------------- -

grace . Quite right. This example and the first Jiahou Jia does not matter. And the range between the two.
If you declare a variable or variables that case print
talents and first Jiahou Jia relevant ( code province . )

------ For reference only ---------------------------------- -----
i + +, + + k
are
for (;;)
{
/ / perform actions before running, so the result is the same
}
------ For reference only ------------------------------- --------
positive solution very much. . do not comment on them . .
------ For reference only -------------------------------------- -
whether it is performed after the first or the first increase after the implementation of the initial value is 0 plus limit value is 9 so the end result is the same
------ For reference only --- ------------------------------------
look to understand for conditional execution of implementation modalities
------ For reference only --------------------------------------- < br> Bangding Bangding
------ For reference only -------------------------------- -------
i + + and + + i this time is 0 ~ 9
------ For reference only --------------- ------------------------
 one such issue discussed 450 floor. . . Fights are very positive ah
------ For reference only ----------------------------- ----------
second when k becomes 9:00 or meet the conditions determined then execute +1 becomes 10 after the last judgment , but does not meet the conditions , so the output is over 9
------ For reference only -------------------------------------- -
understanding of the for loop execution order , we understand ,
------ For reference only ----------------------- ----------------
here and + + i and i + + independent main problem for the execution order .

first enter the for loop , the first implementation of the first part, that assignment . Then execute the second part perform boolean checks a boolean conditional expression is false then out of the loop , execute the statement following the for loop ; otherwise perform for the content of the end of each round of cycle , perform steps.
------ For reference only -------------------------------------- -
+ + i and i + +
you that cycle , the fact is the same
this you see the execution order for your to know
------ For reference only ---------------------- -----------------
LZ to figure out the order of execution for loop naturally understand
------ For reference only ----- ----------------------------------
7 floor speak better , it is recommended to lay the foundation
------ For reference only --------------------------------------- < br> whether i + +, or + + i, i values ​​are incremented by 1 , the former if it is n = i + +, n the value is unchanged , i.e. i + + overall value is not changed , which is the overall value of to add 1.
------ For reference only ---------------------------------- -----
under discussion : for (int i = 0; i <10;) {
/ / print the value of i
i + +;
}
for (int k = 0; k <10 ;) {
/ / print the value of k
+ + k;
}
------ For reference only --------------------------------- ------
is equivalent to:
int i = 0;
while (i <10)
{
/ / Print
i + +; / / + + i;
}
------ For reference only --------------------------------- ------
  This reply was moderator deleted at 2011-07-26 10:50:55

------ For reference only ---------------------------------- -----
this should still be very clear ~
------ For reference only ----------------------- ----------------
is equivalent to:
int i = 0;
while (i <10)
{
/ / Print
i + +; / / + + i;
}
------ For reference only --------------------------------- ------
in both cycles , are the first k +1
then compare . . .
him and this is not the same ( necessary to put together to form this effect )
for(int i = 0; ++i < 10;){// 1- 9(先加后比较)
System.out.println(i);
}

for(int i = 0; i++ < 10;){// 1- 10(先比较后加)
System.out.println(i);
}

------ For reference only ----------------------------------- ----
  This reply was moderator deleted at 2011-07-26 23:38:53

------ For reference only ---------------------------------- -----

positive solution, did not participate in third-party operations are executed in the execution of the loop , and then determine the value of i have no differences.
------ For reference only -------------------------------------- -
i + + and + + i did not participate in the operation expression in the words , the effect is much the same as drops go

example
for example, y = 5 × i + + and y = 5 × + + i this example ( of course, this expression as little as possible , or do not appear )
When i is the initial value of 3
operation result is: 15 and 20

But for statement i is a separate , self-energizing statement, so the effect is the same
------ For reference only -------------- -------------------------
for (int i = 0; i <10 ;)
{
System.print (i + +);
}

and for (int i = 0; i <10 ;)
{
System.print (+ + i);
}
so there will be a difference
------ For reference only ---------------------------- -----------
  This reply was moderator deleted at 2011-07-27 10:08:38

------ For reference only ---------------------------------- -----
both cases the same, only different replication when the only wrong , since the increase makes no difference.
------ For reference only -------------------------------------- -
circulation while the above transformation can be seen soon
1. for (int i = 0; i <10; i + +) into while after

int i = 0;
while (i <10) {
System.out.println (i);
i + + ;/ / ---------------------- a
}

2. for (int i = 0; i <10; + + i) into while after
int i = 0;
while (i <10) {
System.out.println (i);
+ + i ;/ / ---------------------- b
}
comparison, either case , are the first printing, and then i add 1 to the premises in a and b is the increase of a different way , one is to add a further argument , one is to add a value , but the results are the same, all i added a ,
So , like ah.
------ For reference only -------------------------------------- -
only involved in other operations , will reflect the i + + and + + i difference, huh
------ For reference only --------------- ------------------------
execution flow of the for loop a good look to see ~ !
------ For reference only -------------------------------------- -
for the execution order ,
------ For reference only ---------------------------- -----------

already a positive solution
------ For reference only ---------------- -----------------------
i + + with +1 after
+ + k +1 before use
example

int i = 1;
int k = 1;
for (int l = 0; l <10; l + +)
{
int a = i + +;
int b = + + k;
System.out.println ("aaaaaaaaaaaa ===" + a + "," + b);

}
------ For reference only --------------------------------- ------
i + + + + k
variable for the for loop is no different , are 0-9 for printing, but if these two pairs of assignment to a variable , then the results will differ , i + + first assignment + + k first assignment after the operation
------ For reference only -------------------------------- -------
see the first two constraints , for (i = 0; i <10; ....) meaning that i ranges from 0 to less than 10. Last i + + or + + i represents the growth in the number of situations .
------ For reference only -------------------------------------- -
1 floor is a bright spot
------ For reference only ------------------------------ ---------


Positive Solutions
int i = 0;
i++;
System.out.println(i)

int k = 0;
++k;
System.out.println(k)


result is the same . . Because it is independent operations. . After the statement is executed from Canada , it has been increased by 1 , and whether pre- post
------ For reference only --------------------- ------------------
good read
------ For reference only --------------- ------------------------
because the two kinds of cases are alone , and did not participate in other operations which , in this case, so in fact in + + k and k + + is the same
------ For reference only ----------------------------- ----------
landlord estimated forget knot posted . . . . .
------ For reference only -------------------------------------- -
landlord , 13th floor said is correct
------ For reference only ------------------------- --------------
initial values ​​and ranges are limited , ah , i + + and + + k represents the amplitude increment is 1
------ For reference only - --------------------------------------
under discussion : for (int i = 0; i <10; i + +) {
/ / print the value of i
}
for (int k = 0; k <10; + + k) {
/ / print the value of k
}



When the execution is equivalent to the following section

for (int k = 0; k <10 ;) {
/ / print the value of k
+ + k;
}

So, the first output of the first and then execute + + k 0
But there is no other executive body so there's a k + + and + + k the result is the same
+ + k k is the first increase since the implementation of other programs , but there is no other body so the results are Like self plus one

------ For reference only -------------------------------- -------
i loop is executed the first time you are finished 0-9 k loop is outside the i loop course, both are the same cycle the name is not the same as that i k
------ For reference only -------------------- -------------------
look prefixes and suffixes to know
------ For reference only ---- -----------------------------------
what difference do
----- - For reference only ---------------------------------------
anyway execution to 9 point stop . . .
------ For reference only -------------------------------------- -
whether i + +, or + + i, the results are the same, because for a minute after the final surface behind no assignment , only one operator (i + + or + + i), the final result of this operator will pay to the for loop internal judgment. So i + + or + + i significance here is the same .
------ For reference only -------------------------------------- -
come to learn , but the stickers should be able to shut it
------ For reference only ---------------------- -----------------
i starts at 0 , constraints are i <10, with no termination cycle , natural output are 0 ~ 9 friends !
------ For reference only -------------------------------------- -
for (int i = 0; i <10; i + +) {
/ / print the value of i ; for statement is equivalent to int = 0;
while (i <10) {
/ / print the value of i
i + + ;/ / wood there with the same + + i
}
}
for (int k = 0; k <10; + + k) {
/ / print the value of k
}

------ For reference only ---------------------------------- -----
where either i + + or + + i will not affect the result , as for the first three conditions "incremental " after the end of the judgment can be executed, so it is i + + or + + i, after the implementation of this expression i will always increase one of the loop does not the same, so the first two methods is still 0-9.
------ For reference only -------- -------------------------------
long as you know can be executed sequentially , how to explain this ?
sequential execution : initialization part - > judge - > Iteration - > judge - > Iteration

------ For reference only ---------------------------------- -----
20 F, positive solution, judge finished directly execute the following statement , and then addition and subtraction operations, a simple example:
# include "stdio.h"
int main ()
{
int a [5], k;
for (k = 0; k <5; k + +)
scanf_s ("% d", & a [k]);
for (; k> -1; - k)
printf ("\ n% d \ n", a [k]);
return 0;
}

( Note : vs2012 is scanf_s)

input 12345 Enter, the first line appears -858,993,460 random numbers, behind is 54,321 , which is inside a for loop after the statement is executed 6 times

没有评论:

发表评论