I made a game but didn't wanted to use do-while loops.
So instead I tried to use for loops. The problem is when I run with do-while
it works but when I run with for loops it doesn't work in the same way.
I think I changed do-while into for loops correctly. Can someone let me know what I am missing?
do {
crntShowBottle = rand() % 2 + 2;
} while (crntShowBottle == prevShowBottle);
prevShowBottle = crntShowBottle;
for (;;)
{
crntShowBottle = rand() % 2 + 2;
if (crntShowBottle == prevShowBottle)
break;
}
prevShowBottle = crntShowBottle;
First, try to understand the mechanism of loop and how they are different from do-while loop and for loop.
Based on your code, what is happening!
In the do-while section, you are giving instructions that "first do something" and then checking the condition. If the condition (checking) is true crntShowBottle == prevShowBottle then the loop must run again. Otherwise, if the condition is false crntShowBottle != prevShowBottle the loop must terminate.
In the for loop section, you used the infinite loop for(;;). Means loop will be running infinite times. But, inside the loop you wrote the break condition. So, when the condition is matching crntShowBottle == prevShowBottle your loop is shutting down. So, you must use crntShowBottle != prevShowBottle
You also need to understand, how break works!
Related
My novice understanding of the difference between for-loops and while-loops is that they differ in format and we use for-loops when we know the exact number of iterations to complete and we use while-loops when we know what condition must be met to stop. That said could someone explain the difference in outcome of the following code?
let countDown = 2;
while(countDown>1){
countDown--;
console.log(countDown)
}
1
for(let countDown = 2; countDown>1; countDown--){
console.log(countDown)
}
2
As U. Windl had commented, for is just "syntactic sugar" for a while loop. The output of your program varied for for and while loop because in while loop countDown is decremented first and then logged, in for loop countDown is logged first and then decremented. With the below change in while loop the output would be same for both and its equivalent to for loop.
let countDown = 2;
while(countDown>1){
console.log(countDown)
countDown--;
}
Lets see what for and while loop says JavaScript For Loop
The For Loop
The for loop has the following syntax:
for (statement 1; statement 2 (condition); statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
While Loop
while (condition) {
// code block to be executed
}
In your program in for loop Statement 3 (i.e countDown--) is executed after the code block (i.e console.log) has been executed, that is the reason the output was 2.
In while loop we have to take care when to execute Statement 3 (i.e countDown-- in this case) and in for loop Statement 3 is always executed after executing code block
for equivalent for while
for (; condition; ) {
// code block to be executed
}
while equivalent for for
Statement 1
while (Statement 2) {
// code block to be executed
Statement 3
}
for(i=1;i<=ntype;i++)
for(cnt=0,j=1;j<=nbeta[i];j++) {
cnt++;
pos[i][cnt]=j;
if (lll[i][j]==0) {
for(kk=1;kk<=kkbeta[i];kk++)
bfunc[i][cnt][kk]=bfunctmp[i][j][kk];
llltmp[i][cnt]=lll[i][j];
} // if
if (lll[i][j]>0) {
for(kk=1;kk<=kkbeta[i];kk++)
bfunc[i][cnt][kk]=0.5*(bfunctmp[i][j ][kk]+
bfunctmp[i][j+1][kk]);
llltmp[i][cnt]=lll[i][j];
j++;
} // if
} // j,i
!my translated fortran code is
do i =1,ntype
cnt =0
do j =1,nbeta(i)
cnt = cnt+1
pos(i,cnt) =j
if(lll(i,j) ==0) then
do kk =1, kkbeta(i)
bfunc(i,cnt,kk)= bfunctmp(i,j,kk)
end do !end kk
llltmp(i,cnt) =lll(i,j)
! write(*,*)i,j,lll(i,j)
! write(*,*)i,cnt,llltmp(i,cnt) ! '(5x,3I5)'
end if
! cnt1 =j
if(lll(i,j) > 0) then
do kk =1, kkbeta(i)
bfunc(i,cnt,kk)= 0.5 *(bfunctmp(i,j,kk) + bfunctmp(i,j+1,kk))
end do !end kk
llltmp(i,cnt) =lll(i,j)
j =j+1
end if
end do !end j
end do !end i
! A do-variable within a DO body shall not appear in a variable definition context. [j]
I am looking forward to solve this issue soon. Thank you!
As #TomKarzes remarked in comments, you may not assign to the control variable of a DO loop within the body of that loop. Sometimes you can resolve such issues by using a different variable, instead. In your code, however, the objective of the j=j+1 appears to be to skip a loop iteration, and you simply cannot do that in a DO loop.
You can, however, replace your DO loop with a DO WHILE loop:
j = 1
do while (j <= nbeta(i))
! ...
if (lll(i,j) > 0) then
! ...
j = j + 1
end if
j = j + 1
end do
That should work for your particular code, but you must take considerable care when making such a transformation. There are at least these considerations that must be accounted for:
In an iterated DO loop, the loop variable is automatically incremented after each execution of the loop body, even if execution of the body terminates early by execution of a CYCLE statement. In a DO WHILE loop, however, variables are modified only by statements that are executed, so a CYCLE will not, itself, cause any variable to be updated.
The number of iterations for an iterated DO loop to perform is computed before the first iteration is performed, based on data (such as the value of nbeta(i)) gathered when execution reaches the DO statement. That number of iterations can be changed only by prematurely discontinuing the loop via an EXIT, RETURN, GOTO, or STOP statement, or by executing a procedure that causes the program to terminate. On the other hand, the condition of a DO WHILE is evaluated before every iteration.
int i;
for(i=7;i<6;i--)//loop does not execute
{
printf("*");
}
The above code should print one * but it does nothing. Shouldn'tit run same as for(i=7;i<8;i++) ?
Is this a logical error ? Please help.
A for loop has 3 parts
for( init ; cond ; step )
When the execution reaches the loop,
init is executed.
cond is evaluated.
If false, break the loop
If true, proceed to the next step
Execute the body of the loop.
Do step(in many cases, this is increment/decrement)
Goto step 2
In your case , i is set to 7. Then the condition i<6 is checked. Obviously, 7<6 is false. So the loop never gets executed.
And No.
for(i=7;i<6;i--)
and
for(i=7;i<8;i++)
aren't the same.
Perhaps you wanted to write
for(i=7;i>6;i--) //'>' instead of '<'
in which the loop will execute once.
Let's look at the loop
for( i = 7 ; i < 6 ; i-- )
i is initialized 7, but you have the condition i < 6, but i is 7, and therefor, it does not satisfy the condition of the loop. So, the code does not even go through one iteration of the loop.
Maybe, you meant i > 6
In this loop
for(i=7;i<6;i--)
at first you set i to 7 and then check whether i is less than 6. As i is equal to 7 then it is not less than 6 and the loop iterate never.
If you want that the loop would iterate one time then you should write
for(i=7; i > 6;i--)
Though with these magic numbers the loop looks strange.:) It is not clear what is the intention of the programmer.
int i;
for(i=7;i<6;i--)//loop does not execute
{
printf("*");
}
In first execution of this loop value of "i" is 7
next we have to check the condition "i<6" it is false because 7<6 is false ,then it not give any output
for(i=7;i<8;i++) is different because first "i" value is 7
then it checks condition it is i<8 (7<8) it is true.then it executes once ,after that it increment from 1 (i++),then it checks condition .then it is false because "i" is 8
then it stops execution.
#include <stdio.h>
int main(){
int i = 0;
do {
i++;
printf("In while loop\n");
} while (i < 3);
}
output:
In while loop
In while loop
In while loop
Why the printf statement is executed three times?
As soon as the loop starts the value of i becomes 1, so the loop should run 2 times only but it is running 3 times, how?
Pseudo code:
i = 1
=> In while loop
i = 2
=> In while loop
i = 3
=> In while loop
exit from loop
The condition is checked only at the end, after printf.
The do-while loop tests the condition at the end, so the loop in your example will be executed 3 times with i = 1, 2, 3.
Your condition(i < 3) is checked at the end of the loop.
1st pass : i = 1 => "In while loop" printed => (i < 3) satisfied.increment i
2nd pass : i = 2 => "In while loop" printed => (i < 3) satisfied.increment i
3rd pass : i = 3 => "In while loop" printed => (i < 3) not true.exit from loop
Hope this helps!
The first time you hit the while after the first printf i is 1. The loop continues.
The second time you hit the while after the second printf i is 2. The loop continues.
The third time you hit the while after the third printf i is 3. The loop now ends.
You have hit printf three times.
The loop is starting with 'i=0' and then entering the loop with "i++" and becoming 'i=1' then the condition is checked over "i" so in the last loop when 'i=3' the loop is executed and then checked for "i<3".So the loop is repeated thrice.
The reason is u are using post increment.
{
int i = 0;
do {
i++; //first time - 0,second time - 1,third time - 2
printf("In while loop\n");
} while (i < 3);
}
Use pre increment or while instead of do-while to see the difference
Is it safe to use a break instruction for a while/for loop, which is inside a list_for_each_entry instruction (in the Linux kernel)?
By example:
list_for_each_entry(obj1, vars, list) {
...
while(j < k)
{
...
if(k >= MAX_ITERATIONS) break; // break the while
...
}
...
}
Will the while loop be broken as expected or will the list_for_each_entry be broken?
break always breaks out of only the innermost loop, so the outer loop is not affected. In fact, to break out of multiple levels of loops, you have to use goto.