while loop condition not fullfiled - c

double randomx,randomy,randomz,dia;
#define DROP_RAD 60
#define particleradi 3
for (i = 0; i<30; i++)
{
{
randomx=2*((float)rand()/RAND_MAX)-1;
randomy=2*((float)rand()/RAND_MAX)-1;
randomz=2*((float)rand()/RAND_MAX)-1;
randomx=randomx*(DROP_RAD -2*particleradi);
randomy=randomy*(DROP_RAD -2*particleradi);
randomz=randomz*(DROP_RAD -2*particleradi);
dia=randomx*randomx+randomy*randomy+randomz*randomz;
dia=sqrt(dia);
} while(dia>DROP_RAD);
printf(" id=%d, x=%lf, y=%lf, z=%lf , dia=%lf\n ",i,randomx,randomy,randomz,dia);
I'm getting values for dia greater than DROP_RAD even though the while condition is supposed to this. Kindly help.

I think you need a do-while-loop here. It will execute the block of code at least once, and then either repeatedly execute the block, or stop executing it, depending on the boolean condition at the end of the block.
do {
/* here goes your code */
} while (dia > DROP_RAD);
If you don't want the block to be executed at all when the condition is false, you have to move the condition to the beginning of the block.
while (dia > DROP_RAD) {
/* here goes your code */
}

Your code snippet is equivalent to the following
for (i = 0; i<30; i++)
{
{
randomx=2*((float)rand()/RAND_MAX)-1;
randomy=2*((float)rand()/RAND_MAX)-1;
randomz=2*((float)rand()/RAND_MAX)-1;
randomx=randomx*(DROP_RAD -2*particleradi);
randomy=randomy*(DROP_RAD -2*particleradi);
randomz=randomz*(DROP_RAD -2*particleradi);
dia=randomx*randomx+randomy*randomy+randomz*randomz;
dia=sqrt(dia);
}
while(dia>DROP_RAD);
printf(" id=%d, x=%lf, y=%lf, z=%lf , dia=%lf\n ",i,randomx,randomy,randomz,dia);
//...
That is you have a compound statement
{
randomx=2*((float)rand()/RAND_MAX)-1;
// ...
}
followed by a separate while statement
while(dia>DROP_RAD);
So either you have an infinite while loop
while(dia>DROP_RAD);
if dia is greater than DROP_RAD or the loop is interrupted at once if the condition is not satisfied.
It seems you mean a do-while loop like
do {
randomx=2*((float)rand()/RAND_MAX)-1;
randomy=2*((float)rand()/RAND_MAX)-1;
randomz=2*((float)rand()/RAND_MAX)-1;
randomx=randomx*(DROP_RAD -2*particleradi);
randomy=randomy*(DROP_RAD -2*particleradi);
randomz=randomz*(DROP_RAD -2*particleradi);
dia=randomx*randomx+randomy*randomy+randomz*randomz;
dia=sqrt(dia);
} while(dia>DROP_RAD);

Related

while loop replicating a for loop

Hi im trying to do a disk scheduling algorithm (C-SCAN).I have a for loop with an if statement that works, however im trying to change it to a while loop or another option to remove the break statement.
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
while ( initial>RQ[i] )
{
index =i;
i++;
}
The above for loop is what im trying to replicate as seen in my while loop. however it is not working the same way.
any help will be much appreciated
A for loop of the form
for (initialization; condition; repetition) {
// body code
}
is equivalent to:
initialization;
while (condition) {
// body code
repetition;
}
So your loop would be rewritten as:
i = 0;
while (i < n) {
if (initial < RQ[i]) {
index = i;
break;
}
i++;
}
One possibility of removing the break statement would be to introduce an additional flag variable which gets set when the loop should be terminated.
#include <stdbool.h>
[...]
bool terminate = false;
for( i=0; i<n && !terminate ; i++ )
{
if(initial<RQ[i])
{
index=i;
terminate = true;
}
}
However, I personally consider it better to use a break statement.

Changing the if statement into a while statement

I'm trying to change an if statement into a while statement.
For example
int A=1; if(A==1){};
is similar to int A=1 while(A!=1).
My If statement has no codes, I just need it to do nothing in order to avoid the else-if statements.
My If statement is inside 1 While statement and 1 If statement.
So I want the program to do the same thing in the while statement rather than going in 1 While statement and 1 If statement with an If statement with no code.
Original codes something like this
while(C1)
{
if(C2)
{
if( h->data[temp] < h->data[temp*2] && h->data[temp] < h->data[temp*2+1] )
{
break;
}
else if(C4)
{
DO();
}
}
}
I've changed it to
while( C1 && h->data[temp] > h->data[temp*2] && h->data[temp] > h->data[temp*2+1] )
{
if(C2)
{
if(C4)
{
DO();
}
}
}
and the result is different. The original code gives the correct result but the changed code gives an incorrect result. I've only changed the location to if to while and also changed the direction of the < operator to the > operator.
The total code seems like I've spent less effort in it, making down votes. So I'm posting the code somewhat like a pseudo code.
For example int A=1; if(A==1){}; is similar to int A=1 while(A!=1).
As already mentioned, your loop is breaking. The above statement is also incorrect.
int a = 1;
if(a == 1) {} //this is true, and will happen once
BUT
int a = 1;
while(a != 1) {} //false on entry, skip loop ( never entered )
You could also consider a switch / case if you can isolate the condition.
This might achieve your goal of removing the ifs, and make the code more readable.
e.g
int a = 1;
while(a)
{
switch(a)
{
case 1: ++a; continue;
case 2: do();
default: break; //end loop
}
}
In the original code, if (for example) h->data[temp]>h->data[temp*2] were false, DO() wouldn't be executed, but the loop would continue. In the new version, the loop would stop.
As others said, there is no reason to obfuscate the code with an extra while statement; the original is perfectly clear. There is also no need for the else in your original:
while(C1)
{
if(C2)
{
if( h->data[temp] < h->data[temp*2] && h->data[temp] < h->data[temp*2+1] )
{
break;
}
if(C4)
{
DO();
}
}
}

compiled program not executing, just has blinking cursor

trying to get this loop to run but it just has the cursor blinking. I am new to programing. Can you please help?
#include<stdio.h>
int main()
{
int i;
i = 0
while (i <= 5);
{
printf ( "i = %d" );
i += 1;
}
}
Notice how there was also a missing semicolon after i = 0, this will throw an error and prevent your program from continuing. Also, remove the semicolon after your while loop, it will not run the code inside with it present (And will just sit idle after skipping it). Your program should look like:
#include<stdio.h>
int main()
{
int i;
i = 0;
while (i <= 5)
{
printf ( "i = %d" );
i += 1;
}
}
adding to the answer of Cyral, a semicolon indicates the end of a statement. if you have
while (i <= 5);
{
printf ( "i = %d" );
i += 1;
}
the program will run statement by statement, so it will first run the while, doing nothing in every loop because it is a while with a blank instruction ;. then it will run the next statement, that is everything inside the { }.
the blinking cursor occurs because you increment i inside the statement that follows the while, so, the while, expecting an i thats grater than 5 to stop (an i that never increments inside the loop) enters in an infinite loop.

Stuck in a nested do { for { if loop

So with this bit of code the program hangs up and won't exit the loop, the printfs were just put in for debugging and they aren't integral to the program. I am pretty new to programming so I am not sure what I am missing the logic seems like it should work. Thank you very much for taking the time to look over this and your help.
do
{
intialcollide = 0;
for(i=0; i<11; i++)
{
if(i != currentObj)
{
if(object[currentObj].new_loctX == object[i].new_loctX && object[currentObj].new_loctY == object[i].new_loctY)
{
intialcollide = 1;
}
else
{
intialcollide = 0;
}
}
printf("%d\n", intialcollide);
}
}while(intialcollide != 1 || i != 10);
printf("Collide? %d", intialcollide);
When I run it I get infinite 1's and 0's. Thanks again for the help
At the end of your for loop, i will always equal 11. Maybe you thought it would equal 10? Anyways, there is no point comparing i to anything in the while condition because you know it is always the same value.
Add a "break;" whenever you set intialcollide to 1. Your for loop is resetting the value to 0 before it hits the while loop check.

How can I break out of two nested for loops in Objective-C?

I have two for loops nested like this:
for(...) {
for(...) {
}
}
I know that there is a break statement. But I am confused about if it breaks both loops or just the one in which it was called? I need to break both ones as soon as I see that it doesn't make sense to iterate more times over.
If using goto simplifies the code, then it would be appropriate.
for (;;)
{
for (;;)
{
break; /* breaks inner loop */
}
for (;;)
{
goto outer; /* breaks outer loop */
}
}
outer:;
break breaks out of one loop, but you can add a check to the outer loop which breaks when the inner breaks.
bool dobreak = false;
for ( ..; !dobreak && ..; .. ) {
for ( ... ) {
if (...) {
dobreak = true;
break;
}
}
}
The break statement only gets you out of the innermost loop. If you don't want the added overhead in code, memory and performance of a dedicated state variable, I recommend refactoring the code out into a function or method of its own, and using return to get out of all the loops:
void do_lots_of_work(void)
{
int i, j;
for(i=0; i<10 ; i++)
{
for(j=0;j< 10; j++)
{
..
..
if(disaster_struck())
return; /* Gets us out of the loops, and the function too. */
}
}
}
Other than the already mentioned flag variable or goto you could throw an Objective-C exception:
#try {
for() {
for() {
#throw ...
}
}
}
#catch{
...
}
Others have mentioned how you can set a flag or use a goto, but I'd recommend refactoring your code so that the inner loop is turned into a separate method. That method can then return some flag to indicate that the outer loop should break. If you name your methods appropriately, this is much more readable.
for (int i = 0; i < 10; i++) {
if (timeToStop(i)) break;
}
-(bool) timeToStop: (int) i {
for (int j = 0; j < 10; j++) {
if (somethingBadHappens) return true;
}
return false;
}
Pseudocode, not tested, but you get the idea.
The break statement will only break out of the loop in scope, which is the parent loop. If you want to break out of the second loop as well you could use a boolean variable which is in scope of both loops
bool isTerminated = false;
for (...)
{
if (!isTerminated)
{
for(...)
{
...
isTerminated = true;
break;
}
}
else
{
break;
}
}
Change top loop's counter before break
for(i=0; i<10 ; i++)
for(j=0;j< 10; j++){
..
..
i = 10;
break;
}
Probably the easiest way is to use a "flag" variable
for(i=0; i<10 && (done==false); i++)
for(j=0;j< 10; j++){
..
..
if(...){done=true; break;}
}
Another solution is to factor out the second loop in a function:
int i;
for(i=0; i<10 ; i++){
if !innerLoop(i) {
break;
}
}
bool innerLoop(int i)
int j;
for(j=0;j< 10; j++){
doSomthing(i,j);
if(endcondtion){
return false;
}
}
}
The break statement breaks out of the innermost loop. An additional test and break statement would be needed to break out of the outer loop.
If a break is executed from within a set of nested loops, only the innermost loop in which the break is executed is terminated. (Just like standard C)
Exactly like the last ones are, generally like this:
for(i=0;i<a;i++){
for(j=0;j<a;j++){
if(Something_goes_wrong){
i=a;
break;
}
}
}
Just for grins, how about changing this true/false check into a method and using return statements:
- (bool) checkTrueFalse: parameters{
for ( ...) {
for ( ... ) {
if (...) {
return true;
}
}
}
return false;
}

Resources