Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java while循环中的多个条件会产生不同的结果_Java_C++_Algorithm_Sorting_Insertion Sort - Fatal编程技术网

Java while循环中的多个条件会产生不同的结果

Java while循环中的多个条件会产生不同的结果,java,c++,algorithm,sorting,insertion-sort,Java,C++,Algorithm,Sorting,Insertion Sort,我在while循环中有两个条件: count = 0; while (j >= 0 && arr[j] > key) { count++; j = j-1; } 当我按如下方式打破这两个条件时,计数会发生变化: while (j >= 0) { if(arr[j] > key) { count++; } j = j-1; } 第一个程

我在while循环中有两个条件:

count = 0;
while (j >= 0 && arr[j] > key)
       {
           count++;
           j = j-1;
       }
当我按如下方式打破这两个条件时,计数会发生变化:

while (j >= 0)
{
    if(arr[j] > key)
    {
        count++;
    }
    j = j-1;
}
第一个程序的输出:456

第二个程序的输出:904


我认为这两段代码是相同的。为什么这两个程序的计数不同?

这两个版本在逻辑上并不相同。在第一个版本中,
j
仅当两个条件都为真时才递减。在第二个版本中,
j
仅当第一个条件为真时才递减<当第二个版本中的键比较失败时,code>j仍然会递减


因为
j
被用作计算循环条件的一部分,这直接影响循环执行的次数。

在第一个循环中
j
仅当
arr[j]>键
条件为
true
时才会减少。在第二个循环中,
j
每一步都会减少。假设
arr[0]
那么第一个循环将进行0次迭代,这个例子是这些循环差异的最好说明。

让我们检查两个循环:让
j=10
,让
key=3
arr[]={1,2,3,4,5,6,7…}

在第一个循环中,我们有以下模式:

count = 0;
while ( j >= 0 && arr[j] > key ) {
    count++; 
    j = j-1;
}
while (j >= 0) {
    if(arr[j] > key) {
        count++;
    }
    j = j-1;
}
输出每个步骤:

while ( /*j = */ 10 >= 0 && /*arr[10] = */ 11 > 3 ) { // TRUE
    count++; // count becomes 1
    j = j-1; // j becomes 9
}

while ( 9 >= 0 && 10 > 3 ) { // TRUE
    count++; // count becomes 2
    j = j-1; // j becomes 8
}

while ( 8 >= 0 && 9 > 3 ) {  // TRUE
    count++; // count becomes 3
    j = j-1; // j becomes 7
}

while ( 7 >= 0 && 8 > 3 ) { // TRUE
    count++; // count becomes 4
    j = j-1; // j becomes 6
}

while ( 6 >= 0 && 7  > 3 ) { // TRUE
    count++; // count becomes 5
    j = j-1; // j becomes 5
}

while ( 5 >= 0 && 6 > 3 ) { // TRUE
    count++; // count becomes 6
    j = j-1; // j becomes 4
}

while ( 4 >= 0 && 5 > 3 ) { // TRUE
    count++; // count becomes 7
    j = j-1; // j becomes 3
}

while ( 3 >= 0 && 4 > 3 ) { // TRUE
    count++; // count becomes 8
    j = j-1; // j becomes 2
}

while ( 2 >= 0 && 3 > 3 ) { // FALSE 
    // loop breaks: First Condition TRUE, Second Condition FALSE
}
简化输出:

// Initial count = 0, j = 10, constant key = 3, arr[] = {1,2,3,4,5,6,7...}

count = 0  j = 10  arr[10] = 11 compound condition = true
count = 1  j = 9   arr[9]  = 10 compound condition = true
count = 2  j = 8   arr[8]  = 9  compound condition = true
count = 3  j = 7   arr[7]  = 8  compound condition = true
count = 4  j = 6   arr[6]  = 7  compound condition = true
count = 5  j = 5   arr[5]  = 6  compound condition = true
count = 6  j = 4   arr[4]  = 5  compound condition = true
count = 7  j = 3   arr[3]  = 4  compound condition = true
count = 8  j = 2   arr[2]  = 3  compound condition = false

count = 8 // 8 times it looped successfully
// Initial count = 0, j = 10, constant key = 3, arr[] = {1,2,3,4,5,6,7...}

count = 0  j = 10  arr[10] = 11 while condition = true  | if condition = true
count = 1  j = 9   arr[9]  = 10 while condition = true  | if condition = true
count = 2  j = 8   arr[8]  = 9  while condition = true  | if condition = true
count = 3  j = 7   arr[7]  = 8  while condition = true  | if condition = true
count = 4  j = 6   arr[6]  = 7  while condition = true  | if condition = true
count = 5  j = 5   arr[5]  = 6  while condition = true  | if condition = true
count = 6  j = 4   arr[4]  = 5  while condition = true  | if condition = true
count = 7  j = 3   arr[3]  = 4  while condition = true  | if condition = true
count = 8  j = 2   arr[2]  = 3  while condition = true  | if condition = false
count = 8  j = 1   arr[1]  = 2  while condition = true  | if condition = false
count = 8  j = 0   arr[0]  = 1  while condition = true  | if condition = false
count = 8  j = -1  NO EXECUTION  while condition = false | NO EXECUTION

count = 8; // 11 times it looped successfully.

在第二个循环中,我们有以下模式:

count = 0;
while ( j >= 0 && arr[j] > key ) {
    count++; 
    j = j-1;
}
while (j >= 0) {
    if(arr[j] > key) {
        count++;
    }
    j = j-1;
}
使用上面第一个循环中相同的初始条件,其中
j=10
key=3
arr[]={1,2,3,4,5,6,7…}

让我们输出每个步骤:

while ( /*j = */ 10 >= 0 ) { // TRUE
    if( /*arr[10] = */ 11  > 3 ) {  // TRUE
        count++; // count becomes 1
    }
    j = j-1; // j becomes 9
}

while ( 9 >= 0 ) { // TRUE
    if ( 10 > 3 ) { // TRUE
        count++; // count becomes 2
    }
    j = j-1; // j becomes 8
}

while ( 8 >= 0 ) { // TRUE
    if ( 9 > 3 ) { // TRUE
        count++; // count becomes 3
    }
    j = j-1; // j becomes 7
}

while ( 7 >= 0 ) { // TRUE
    if ( 8 > 3 ) { // TRUE
        count++; // count becomes 4
    }
    j = j-1; // j becomes 6
}

while ( 6 >= 0 ) { // TRUE
    if ( 7 > 3 ) { // TRUE
        count++; // count becomes 5
    }
    j = j-1; // j becomes 5
}

while ( 5 >= 0 ) { // TRUE
    if ( 6 > 3 ) { // TRUE
        count++; // count becomes 6
    }
    j = j-1; // j becomes 4
}

while ( 4 >= 0 ) { // TRUE
    if ( 5 > 3 ) { // TRUE
        count++; // count becomes 7
    }
    j = j-1; // j becomes 3
}

while ( 3 >= 0 ) { // TRUE
    if ( 4 > 3 ) { // TRUE
        count++; // count becomes 8
    }
    j = j-1; // j becomes 2
}

while ( 2 >= 0 ) { // TRUE
    if ( 3 > 3 ) { // FALSE
        count++; // count DOES NOT INCREMENT
    }
    j = j-1; // j becomes 1
}

while ( 1 >= 0 ) { // TRUE
    if ( 2 > 3 ) { // FALSE
         count++; // count DOES NOT INCREMENT
    }
    j = j-1; // j becomes 0
}

while ( 0 >= 0 ) { // TRUE
    if ( 1 > 3 ) { // FALSE
        count++; // count DOES NOT INCREMENT
    }
    j = j-1; // j becomes -1
}

while( -1 >= 0 ) { // FALSE
    // Loop breaks since its only condition if false.
}
简化输出:

// Initial count = 0, j = 10, constant key = 3, arr[] = {1,2,3,4,5,6,7...}

count = 0  j = 10  arr[10] = 11 compound condition = true
count = 1  j = 9   arr[9]  = 10 compound condition = true
count = 2  j = 8   arr[8]  = 9  compound condition = true
count = 3  j = 7   arr[7]  = 8  compound condition = true
count = 4  j = 6   arr[6]  = 7  compound condition = true
count = 5  j = 5   arr[5]  = 6  compound condition = true
count = 6  j = 4   arr[4]  = 5  compound condition = true
count = 7  j = 3   arr[3]  = 4  compound condition = true
count = 8  j = 2   arr[2]  = 3  compound condition = false

count = 8 // 8 times it looped successfully
// Initial count = 0, j = 10, constant key = 3, arr[] = {1,2,3,4,5,6,7...}

count = 0  j = 10  arr[10] = 11 while condition = true  | if condition = true
count = 1  j = 9   arr[9]  = 10 while condition = true  | if condition = true
count = 2  j = 8   arr[8]  = 9  while condition = true  | if condition = true
count = 3  j = 7   arr[7]  = 8  while condition = true  | if condition = true
count = 4  j = 6   arr[6]  = 7  while condition = true  | if condition = true
count = 5  j = 5   arr[5]  = 6  while condition = true  | if condition = true
count = 6  j = 4   arr[4]  = 5  while condition = true  | if condition = true
count = 7  j = 3   arr[3]  = 4  while condition = true  | if condition = true
count = 8  j = 2   arr[2]  = 3  while condition = true  | if condition = false
count = 8  j = 1   arr[1]  = 2  while condition = true  | if condition = false
count = 8  j = 0   arr[0]  = 1  while condition = true  | if condition = false
count = 8  j = -1  NO EXECUTION  while condition = false | NO EXECUTION

count = 8; // 11 times it looped successfully.

这两个循环不相同,行为也不相同。第二个while循环将比第一个while循环执行更多次。

对于第一种情况,您的语句

计数++

j=j-1

将在两个条件都满足时执行 j>=0arr[j]>键为真

但对于第二种情况,语句j=j-1将仅在j>=0为真时执行,语句count++将在两个条件同时满足时执行
j>=0arr[j]>键为真。:)

这两个循环不完全相同。第一个循环仅在两个条件都为真时执行。使用调试器逐步执行,看看行为的不同之处。什么会使您认为它们是相同的?标签垃圾是怎么回事?