LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2008, 04:34 PM   #1
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Rep: Reputation: 15
while () not exiting even when exit condition is met


Here's a bizzard one. I have an while(running) loop and during the loop execution when I certain condition is met I set running = 0; But the loop continues to infinity. Within the loop I tell it to give me constant feedback and I can watch the value of running change from 1 to 0... but the loop continues..... Any ideas?
 
Old 08-28-2008, 04:42 PM   #2
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
Quote:
Originally Posted by aatwell View Post
Here's a bizzard one. I have an while(running) loop and during the loop execution when I certain condition is met I set running = 0; But the loop continues to infinity. Within the loop I tell it to give me constant feedback and I can watch the value of running change from 1 to 0... but the loop continues..... Any ideas?
Why not do this:

Code:
while (running > 0) {
    //do stuff
    if (end_condition)
        running = 0;
}
To answer the question you've asked, that's heavily language-dependent. Some languages, such as Python, associate "0" with "false", so the above loop would end. Other languages, however, do not make that association, and a "while (variable)" would simply check that the variable *exists*, so regardless of the variable's value, the loop would continue infinitely.
 
Old 08-28-2008, 04:48 PM   #3
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Original Poster
Rep: Reputation: 15
I tried an if statement to change the variable. I have tried these.

Code:
while(running) {
  if (mycondition) running=0;
  };
}


while(running) {
  if (mycondition) return(EXIT_SUCCESS);
  };
}


while(running) {
  if (mycondition) exit(running);
  };
}


while(running) {
  if (mycondition) running=0;};
  if (running==0) {exit(running)};
  if (running==0) return(EXIT_SUCCESS);
}

none of them work.
 
Old 08-28-2008, 04:59 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
None of them are legal C, so I don't think you have really tried them - they will not compile (too many } characters).

Please post your actual code.
 
Old 08-28-2008, 05:14 PM   #5
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
Quote:
Originally Posted by aatwell View Post
none of them work.
You missed my point. while (running > 0) is what I was getting at
 
Old 08-28-2008, 06:22 PM   #6
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks so much....but I did get the point. I tried while(running > 0) and that didn't resolve the issue.

Cheers !
 
Old 08-28-2008, 06:40 PM   #7
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
This is an easy one - 'mycondition' is never true. Have a close look at your code (we can't help because you didn't post it).
 
Old 08-28-2008, 06:56 PM   #8
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
Quote:
Originally Posted by aatwell View Post
I tell it to give me constant feedback and I can watch the value of running change from 1 to 0
Quote:
Originally Posted by pinniped View Post
This is an easy one - 'mycondition' is never true. Have a close look at your code (we can't help because you didn't post it).
mycondition does become true at some point, or running wouldn't change from 1 to 0.
 
Old 08-28-2008, 06:57 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You never know, maybe it has a blocking system call in it.
ta0kira
 
Old 08-28-2008, 07:09 PM   #10
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Do you have nested loops and the condition is being reset?
Have you tried compiling with a different "optimization" level (check for compiler bug).
 
Old 08-28-2008, 07:34 PM   #11
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Maybe there is no code and OP is waiting for a program to exit that doesn't exist?
ta0kira
 
Old 08-28-2008, 07:36 PM   #12
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
Quote:
Originally Posted by pinniped View Post
Do you have nested loops and the condition is being reset?
Have you tried compiling with a different "optimization" level (check for compiler bug).
Also the OP could check to make sure he/she isn't shadowing variables. Declaring "running" inside the while loop and then setting running = 0 will not change the value of the "running" variable that is outside the loop.

This is the best I can do to reproduce the problem:

Code:
int main(){
    int running = 1;
    int count = 0;
    while (running) {
        int running = 1;
        if (count / 1000 > 0) {
            running = 0;
        }
        printf("running = %i", running);
        count++;
    }
    return 0;
}
But this is broken intentionally!

Last edited by rocket357; 08-28-2008 at 07:40 PM.
 
Old 08-28-2008, 08:36 PM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by aatwell View Post
I tried an if statement to change the variable. I have tried these.
Code:
while(running && !mycondition) {
  //if (mycondition) running=0;
  //}; ???
}
If that doesn't work then your condition is faulty.
ta0kira

Last edited by ta0kira; 08-28-2008 at 08:37 PM.
 
Old 08-29-2008, 01:12 PM   #14
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Original Poster
Rep: Reputation: 15
Code:
#include <stdio.h>
int main(){
    int running = 1;
    int count = 0;
    while (running) {
        int running = 1;
        if (count / 1000 > 0) {
            running = 0;
        }
        printf("running = %i", running);
        count++;
    }
    return 0;
}
I think you have something here. I am completely amazed when I compile and run this code that even when loop begins again, it doesn't check the value of running, find it false and exit the loop. I think you have found my issue. But now what's the solution? Can anyone explain this?

Thanks.
 
Old 08-29-2008, 02:00 PM   #15
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
In the code I posted, the variable "running" is being shadowed.

When you declare int running = 1, a *new* memory location is allocated. The outer "running" variable (above the loop) is NOT the same variable as the inner "running" variable (declared inside the loop). You can increment/decrement/assign values to/etc... the *inner* "running" variable and it won't change the *outer* "running" variable. Problem is, the while condition is checking the outer variable, whereas within the loop you're modifying the inner variable.

Check it out:

Code:
int main() {
   int running = 1;
   int count = 4;
   printf("The value of (outer) running is %i", running); // will print 1

   while (running++ < count) {
      int running = 0;
      printf("The value of (inner) running is %i", running); // will print 0
   }

   printf("The value of (outer) running is %i", running); // will print 5
}
[rocket357@FreeBSD ~]$ ./a.out
The value of (outer) running is 1
The value of (inner) running is 0
The value of (inner) running is 0
The value of (inner) running is 0
The value of (outer) running is 5
[rocket357@FreeBSD ~]$

The reason this prints values 1, 0, 0, 0, 5 is because within the loop, "running" is a different variable than "running" that the while condition checks (the outer "running" variable). (Note that the while condition increments the *outer* running variable as evidenced by the last printf).

Now without the shadowed variable:

Code:
int main() {
   int running = 1;
   int count = 4;
   printf("The value of (outer) running is %i", running);

   while (running++ < count) {
      printf("The value of (inner) running is %i", running);
   }

   printf("The value of (outer) running is %i", running);
}
[rocket357@FreeBSD ~]$ ./a.out
The value of (outer) running is 1
The value of (inner) running is 2
The value of (inner) running is 3
The value of (inner) running is 4
The value of (outer) running is 5
[rocket357@FreeBSD ~]$

Without the shadowed variable, the "outer" variable is the only one available, so it is used in the inner printf calls.

The fix is simple: check to make sure you aren't shadowing "running".

Last edited by rocket357; 08-29-2008 at 02:11 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning: at kernel/exit.c:814 do exit() (Tainted: P ) <- nVidia video problem? Hitboxx Fedora 2 10-06-2007 09:47 PM
Hello and well met. rickyh LinuxQuestions.org Member Intro 3 07-27-2007 08:59 AM
does exiting window managers exit your processes? DJOtaku Linux - General 2 08-16-2005 12:21 PM
RPM dependancies are met, but... bestmehr Linux - General 3 11-14-2003 03:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration