collapse collapse

* Who's Online

  • Dot Guests: 68
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11164
  • stats Total Topics: 1700
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 1470

Author Topic: While loop not exiting  (Read 8620 times)

0 Members and 1 Guest are viewing this topic.

Offline spum789

  • Limited_C++_Learner
  • Jr. Member
  • *
  • Posts: 65
  • Reputation 97
  • Gender: Male
    • View Profile
While loop not exiting
« on: November 13, 2009, 06:59:44 pm »
hey i have a while loop that refueses to exit even though the condition is false

Code: C++
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int choice;
  5. bool cont = true;
  6. char cont1;  
  7.  
  8. void choices() {
  9.         cout << "Enter 1 or 2" << endl;
  10.         cin >> choice;
  11.         switch(choice)
  12.         {
  13.                 case 1:
  14.                         cout << "you have chosen 1" << endl;
  15.                         break;
  16.                 case 2:
  17.                         cout << "you have chosen 2" << endl;
  18.                         break;
  19.                 default:
  20.                         cout << "Error: You did not choose a 1 or a 2... douscebag" << endl;
  21.                         break;
  22.         }
  23.        
  24.        
  25. }
  26.  
  27. int main()
  28. {
  29.         do {
  30.                 choices();
  31.                 cout << "Would you like to enter another number? 0/1" << endl;
  32.                 cin >> cont1;
  33.                 if(cont1 == 'y') {
  34.                         cont = true;
  35.                 }
  36.                 else {
  37.                         cont = false;//here the condition is set to false if something other than y is entered
  38.                 }
  39.         }while(cont = true); //since the condition is false then the loop should exit but it doesn't
  40.         return 0;
  41. }
« Last Edit: November 13, 2009, 09:23:16 pm by Celestialkey »

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: While loop not exiting
« Reply #1 on: November 13, 2009, 09:23:06 pm »
USE [ CODE=CPP ]!!!!
------------------------------
On topic: Why are you using a exit condition like that? O_o You really only need those if you have to clean something up, otherwise, you can just do one of these.
Code: C++
  1. do {  
  2.       choices();  
  3.       cout << "Would you like to enter another number? 0/1" << endl;  
  4.       cin >> cont1;  
  5.       if(cont1 == 'y')  
  6.          break;
  7.    }while(cont = true);
  8.  
or
Code: C++
  1. do {  
  2.       choices();  
  3.       cout << "Would you like to enter another number? 0/1" << endl;  
  4.       cin >> cont1;  
  5.       if(cont1 == 'y')  
  6.          return 0;
  7.    }while(cont = true);
  8.  

You may also have a error somewhere in the Choices. It may be taking the 'enter' press as the second cin , to test this, add another
Code: C++
  1. cin >> cont1;
  2.  
after your current one.
« Last Edit: November 13, 2009, 09:27:41 pm by Celestialkey »
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline powerfear

  • - Secret Permissions Level 1 -
  • Jr. Member
  • **
  • Posts: 58
  • Reputation 64
  • Gender: Male
    • View Profile
Re: While loop not exiting
« Reply #2 on: November 13, 2009, 09:36:19 pm »
if you don't absolutely want to use Do/While you can do this

Code: PHP
  1. #include <iostream>
  2. using namespace std;
  3. int choice;
  4. bool cont = true;
  5. char cont1;  
  6. void choices() {
  7.    cout << "Enter 1 or 2" << endl;
  8.    cin >> choice;
  9.    switch(choice)
  10.    {
  11.       case 1:
  12.          cout << "you have chosen 1" << endl;
  13.          break;
  14.       case 2:
  15.          cout << "you have chosen 2" << endl;
  16.          break;
  17.       default:
  18.          cout << "Error: You did not choose a 1 or a 2... douscebag" << endl;
  19.          break;
  20.    }
  21. }
  22.  
  23. int main()
  24. {
  25. start:
  26.       choices();
  27.       cout << "Would you like to enter another number? y/n" << endl;
  28.       cin >> cont1;
  29.       if(cont1 == 'y')
  30.          goto start;
  31.    return 0;
  32. }
  33.  
tested and it work, otherwise don't have much time to figure out why its bugging a bit weird ;P

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: While loop not exiting
« Reply #3 on: November 13, 2009, 09:39:33 pm »
if you don't absolutely want to use Do/While you can do this

Code: PHP
  1. #include <iostream>
  2. using namespace std;
  3. int choice;
  4. bool cont = true;
  5. char cont1;  
  6. void choices() {
  7.    cout << "Enter 1 or 2" << endl;
  8.    cin >> choice;
  9.    switch(choice)
  10.    {
  11.       case 1:
  12.          cout << "you have chosen 1" << endl;
  13.          break;
  14.       case 2:
  15.          cout << "you have chosen 2" << endl;
  16.          break;
  17.       default:
  18.          cout << "Error: You did not choose a 1 or a 2... douscebag" << endl;
  19.          break;
  20.    }
  21. }
  22.  
  23. int main()
  24. {
  25. start: // NO
  26.       choices();
  27.       cout << "Would you like to enter another number? y/n" << endl;
  28.       cin >> cont1;
  29.       if(cont1 == 'y')
  30.          goto start; // NO
  31.    return 0;
  32. }
  33.  
tested and it work, otherwise don't have much time to figure out why its bugging a bit weird ;P

No, no, no, no, no, no, no, NO, NO, NO!


"Using GOTO commands in modern languages is a BIG faux pas in programming. It should be avoided, and more structured, well, structures should be used. Instead of GOTO, use a loop, conditional, or function call. You've got lots of options in C++: FOR, WHILE, DO, IF, SWITCH, TERNARY (?). Other times you'll want to use a function call in place of a GOTO jump."
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: While loop not exiting
« Reply #4 on: November 13, 2009, 09:42:48 pm »
If you want more information on why its bad, here is a link to a pretty famous article.
http://www.ifi.uzh.ch/req/courses/kvse/uebungen/Dijkstra_Goto.pdf
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline spum789

  • Limited_C++_Learner
  • Jr. Member
  • *
  • Posts: 65
  • Reputation 97
  • Gender: Male
    • View Profile
Re: While loop not exiting
« Reply #5 on: November 13, 2009, 11:24:19 pm »
firstly i didn't know about [ code=cpp].
second thx for your help i've been refreshing myself on wat i know, and i knew it was simpler than wat i was making it out to be.

i ending up doing this
Code: C++
  1. do {
  2.                 choices();
  3.                 cout << "Would you like to enter another number? 0/1" << endl;
  4.                 cin >> again;
  5.         }while(again != 1);
  6.  


USE [ CODE=CPP ]!!!!
------------------------------
On topic: Why are you using a exit condition like that? O_o You really only need those if you have to clean something up, otherwise, you can just do one of these.
Code: C++
  1. do {  
  2.       choices();  
  3.       cout << "Would you like to enter another number? 0/1" << endl;  
  4.       cin >> cont1;  
  5.       if(cont1 == 'y')  
  6.          break;
  7.    }while(cont = true);
  8.  
or
Code: C++
  1. do {  
  2.       choices();  
  3.       cout << "Would you like to enter another number? 0/1" << endl;  
  4.       cin >> cont1;  
  5.       if(cont1 == 'y')  
  6.          return 0;
  7.    }while(cont = true);
  8.  

You may also have a error somewhere in the Choices. It may be taking the 'enter' press as the second cin , to test this, add another
Code: C++
  1. cin >> cont1;
  2.  
after your current one.
« Last Edit: November 13, 2009, 11:26:21 pm by Celestialkey »

Offline jho

  • The flamboyant FLOSS fanatic
  • Sr. Member
  • *
  • Posts: 352
  • Reputation 1199
  • Gender: Male
    • View Profile
    • http://hajotus.net
Re: While loop not exiting
« Reply #6 on: November 14, 2009, 06:20:11 am »
The while-statement was assigning cont to true.

Use
Code: C++
  1. do { /* ... */ } while (cont == true);
instead of
Code: C++
  1. do { /* ... */ } while (cont = true);

The first one compares cont to true each iteration. The second one assigns cont to be true every iteration.
"I'm sorry, I only accept criticism in the form of sed expressions."

Offline jho

  • The flamboyant FLOSS fanatic
  • Sr. Member
  • *
  • Posts: 352
  • Reputation 1199
  • Gender: Male
    • View Profile
    • http://hajotus.net
Re: While loop not exiting
« Reply #7 on: November 14, 2009, 06:45:32 am »
No, no, no, no, no, no, no, NO, NO, NO!


"Using GOTO commands in modern languages is a BIG faux pas in programming. It should be avoided, and more structured, well, structures should be used. Instead of GOTO, use a loop, conditional, or function call. You've got lots of options in C++: FOR, WHILE, DO, IF, SWITCH, TERNARY (?). Other times you'll want to use a function call in place of a GOTO jump."
Goto can also improve readablity in some cases. Just because some guy said 40 years ago that gotos are bad doesn't mean that you should go rampage every time someone uses a goto.

Let's consider these two snippets. Both do the same thing, one with gotos, the other with structured ifs.
Code: C++
  1. int foo(void)
  2. {
  3.     int x;
  4.     x = do_a();
  5.     if(error_a())
  6.         goto end_a;
  7.     x = do_b();
  8.     if(error_b())
  9.         goto end_b;
  10.     x = do_c();
  11.     if(error_c())
  12.         goto end_c;
  13.     x = do_d();
  14.     if(error_d())
  15.         goto end_d;
  16.     goto end;
  17.  
  18. end_d:
  19.     undo_d();
  20. end_c:
  21.     undo_c();
  22. end_b:
  23.     undo_b();
  24. end_a:
  25.     undo_a();
  26. end:
  27.     return x;
  28. }
  29.  

Code: C++
  1. int bar(void)
  2. {
  3.     int x;
  4.     x = do_a();
  5.     if(error_a())
  6.     {
  7.         undo_a();
  8.     }
  9.     else
  10.     {
  11.         x = do_b();
  12.         if(error_b())
  13.         {
  14.             undo_b();
  15.             undo_a();
  16.         }
  17.         else
  18.         {
  19.             x = do_c();
  20.             if(error_c())
  21.             {
  22.                 undo_c();
  23.                 undo_b();
  24.                 undo_a();
  25.             }
  26.             else
  27.             {
  28.                 x = do_d();
  29.                 if(error_d())
  30.                 {
  31.                     undo_d();
  32.                     undo_c();
  33.                     undo_b();
  34.                     undo_a();
  35.                 }
  36.             }
  37.         }
  38.     }
  39.     return x;
  40. }
  41.  
  42.  

I'd argue that the stack-like wind and unwind approach (the first snippet) is a lot clearer at first glance. And a lot shorter too.
« Last Edit: November 14, 2009, 06:49:02 am by jho »
"I'm sorry, I only accept criticism in the form of sed expressions."

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: While loop not exiting
« Reply #8 on: November 14, 2009, 08:50:19 am »
I don't hold my opinion based off that simple article. It was just something I remembered that helped explain the bad practices of using goto.
Also, in a case like that above, I would use a switch statement and just let it fall through. That defeats the entire purpose of your above statement.
Code: C++
  1. void Error(int x){
  2.     switch(x){
  3.         case ERROR_D: // This can be whatever value that was returned from the error
  4.             undo_d();
  5.         case ERROR_C:
  6.             undo_c();
  7.         case ERROR_B:
  8.             undo_b();
  9.         case ERROR_A:
  10.             undo_a();
  11.     }
  12. }
  13.  
  14. int foo(void)
  15. {
  16.     int x;
  17.     x = do_a();
  18.     if(error_a()){
  19.         Error(x);
  20.         return 1;
  21.     }
  22.     x = do_b();
  23.     if(error_b()){
  24.         Error(x);
  25.         return 2;
  26.     }
  27.     x = do_c();
  28.     if(error_c()){
  29.         Error(x);
  30.         return 3;
  31.     }
  32.     x = do_d();
  33.     if(error_d()){
  34.         Error(x);
  35.         return 4;
  36.     }
  37.  
  38.     return x;
  39. }

I'm sure there is a easier way, but without knowing what the functions do, I can't organize it any more.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline jho

  • The flamboyant FLOSS fanatic
  • Sr. Member
  • *
  • Posts: 352
  • Reputation 1199
  • Gender: Male
    • View Profile
    • http://hajotus.net
Re: While loop not exiting
« Reply #9 on: November 14, 2009, 09:18:35 am »
Very well then. I've just seen my share of brainwashed CS undergrads who tell everyone and their aunts how goto is bad just bekause Dijkstra said so. ;D

I can't also understand the argument how gotos automatically create ugly spaghetti code. Hell, break and continue are just gotos. While loops are just sugar-coated gotos. Do they create spaghetti code? IMO gotos are also the best way to handle exceptions without leaving the current function in C.
"I'm sorry, I only accept criticism in the form of sed expressions."

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: While loop not exiting
« Reply #10 on: November 14, 2009, 09:28:32 am »
I'm not saying there bad. I'm saying they are bad practice. If you have too many of them, the code just becomes terrible at following. Your only doing short jumps there, what if you have a application with 500+ lines and it jumps to the top to "loop" and inside there are other loops. I've seen my share of terrible goto applications that were hell to follow. And yes, GOTO is the best way to handle exceptions without leaving the c function, ill agree. But it's still complicated in anything larger then a calculator.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline jho

  • The flamboyant FLOSS fanatic
  • Sr. Member
  • *
  • Posts: 352
  • Reputation 1199
  • Gender: Male
    • View Profile
    • http://hajotus.net
Re: While loop not exiting
« Reply #11 on: November 14, 2009, 09:37:34 am »
I'm not saying there bad. I'm saying they are bad practice. If you have too many of them, the code just becomes terrible at following. Your only doing short jumps there, what if you have a application with 500+ lines and it jumps to the top to "loop" and inside there are other loops. I've seen my share of terrible goto applications that were hell to follow. And yes, GOTO is the best way to handle exceptions without leaving the c function, ill agree. But it's still complicated in anything larger then a calculator.
If you have a function of 500+ lines, you're doing wrong a lot more than just using gotos with it. And if you're talking about longjmp there.. yeah, that's a clusterfuck that should be avoided at all costs.

I've also seen my share of terrible applications that didn't use goto. It's not about the language, but the programmer. Granted, some languages make it easier to write bad code. This reminds me of:
Quote from: Bjarne Stroustrup
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg.

Linux kernel uses gotos extensively btw. Mainly for the wind-and-unwind style functions that I posted earlier. I've never had problems reading that code. And I think the Linux kernel is a little bit bigger than a simple calculator. ;)
"I'm sorry, I only accept criticism in the form of sed expressions."

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: While loop not exiting
« Reply #12 on: November 14, 2009, 10:05:36 am »
I stand corrected, but my opinion on GOTO won't change. ^^
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: While loop not exiting
« Reply #13 on: November 14, 2009, 10:55:44 am »
Putting to rest the goto myth I wrote a simple C++ program:
Code: C++
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.         int x = 5;
  7. a:
  8.         cout << x << endl;
  9.         goto a;
  10.         return 0;
  11. }
Here is the disassembly:
< = [
Code: C++
  1. 00BD13DC  rep stos    dword ptr es:[edi]
  2.         int x = 5;
  3. 00BD13DE  mov         dword ptr <x>,5
  4. a:
  5.         cout << x << endl;
  6. 00BD13E5  mov         esi,esp
  7. 00BD13E7  mov         eax,dword ptr [__imp_std::endl (0BD8298h)]
  8. 00BD13EC  push        eax  
  9. 00BD13ED  mov         edi,esp
  10. 00BD13EF  mov         ecx,dword ptr <x>
  11. 00BD13F2  push        ecx  
  12. 00BD13F3  mov         ecx,dword ptr [__imp_std::cout (0BD8290h)]
  13. 00BD13F9  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0BD8294h)]
  14. 00BD13FF  cmp         edi,esp
  15. 00BD1401  call        @ILT+325(__RTC_CheckEsp) (0BD114Ah)
  16. 00BD1406  mov         ecx,eax
  17. 00BD1408  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0BD829Ch)]
  18. 00BD140E  cmp         esi,esp
  19. 00BD1410  call        @ILT+325(__RTC_CheckEsp) (0BD114Ah)
  20.         goto a;
  21. 00BD1415  jmp         a (0BD13E5h)
  22.         return 0;
  23. 00BD1417  xor         eax,eax
As you can see it just translates it to a jmp instruction. Nothing harmful there. In fact I believe, a jmp instruction is faster than a function call. Anyone know differently?

In fact C++ will generate similar code for an infinite while loop:
Code: C++
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.         int x = 5;
  7. while(true)
  8. {
  9.         cout << x << endl;
  10. }
  11.         return 0;
  12. }

Here is the disassembly:
Code: C++
  1. while(true)
  2. 00FE13E5  mov         eax,1
  3. 00FE13EA  test        eax,eax
  4. 00FE13EC  je          main+60h (0FE1420h)
  5. {
  6.         cout << x << endl;
  7. 00FE13EE  mov         esi,esp
  8. 00FE13F0  mov         eax,dword ptr [__imp_std::endl (0FE8298h)]
  9. 00FE13F5  push        eax  
  10. 00FE13F6  mov         edi,esp
  11. 00FE13F8  mov         ecx,dword ptr <x>
  12. 00FE13FB  push        ecx  
  13. 00FE13FC  mov         ecx,dword ptr [__imp_std::cout (0FE8290h)]
  14. 00FE1402  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0FE8294h)]
  15. 00FE1408  cmp         edi,esp
  16. 00FE140A  call        @ILT+325(__RTC_CheckEsp) (0FE114Ah)
  17. 00FE140F  mov         ecx,eax
  18. 00FE1411  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0FE829Ch)]
  19. 00FE1417  cmp         esi,esp
  20. 00FE1419  call        @ILT+325(__RTC_CheckEsp) (0FE114Ah)
  21. }
  22. 00FE141E  jmp         main+25h (0FE13E5h)

You can see it jumps to main+25h which is FE13E5 at the time of execution.

[edited name=Nathan Adams date=1258217843][/edited]
« Last Edit: November 14, 2009, 11:02:07 am by Nathan Adams »
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: While loop not exiting
« Reply #14 on: November 14, 2009, 11:03:45 am »
Nice to see your gun-ho here Nathan, but I believe it was more or less a argument of if its good or bad practice. No one questioned the performance.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: While loop not exiting
« Reply #15 on: November 14, 2009, 11:13:55 am »
Nice to see your gun-ho here Nathan, but I believe it was more or less a argument of if its good or bad practice. No one questioned the performance.
That is true, but the paper made it seem like Dijkstra wants to avoid goto statements like the plague,
"The go to statement as it stands is just too primitive;" (1)
And although general rule of thumb is to not use them, you should just discourage the use of them. Every project has different needs and obviously the Linux kernel has more lines of code than what we would ever write in a lifetime. And they really don't hurt anything by putting them in there, as the complier will just put in the same instruction(s).

Just sayin my two cents of course.
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline jho

  • The flamboyant FLOSS fanatic
  • Sr. Member
  • *
  • Posts: 352
  • Reputation 1199
  • Gender: Male
    • View Profile
    • http://hajotus.net
Re: While loop not exiting
« Reply #16 on: November 14, 2009, 12:46:14 pm »
Putting to rest the goto myth I wrote a simple C++ program:
Umm, what did you except?
Code: C
  1. while(1) { /* code */ }
  2. return 0;
is equivalent to
Code: C
  1. start:
  2.     if(!1)
  3.         goto end;
  4.     /* code */;
  5.     goto start;
  6. end:
  7.     return 0;
  8.  
and the compiler is usually smart enough to optimize out the if statement that always equals true.

Here's two C programs that do exactly the same thing:
Code: C
  1. int main(void)
  2. {
  3. start:
  4.     puts("test");
  5.     goto start;
  6.     return 0;
  7. }
  8.  
Code: C
  1. int main(void)
  2. {
  3.     while(1)
  4.         puts("test");
  5.     return 0;
  6. }
  7.  

And the output from gcc -S is identical for both (stripped of unneeded .ident parts etc to make it more clear. Still compiles fine without warnings on gas):
Code: [Select]
.LC0:
.string "test"
.text
.globl main
.type main, @function
main:
.LFB2:
pushq %rbp
.LCFI0:
movq %rsp, %rbp
.LCFI1:
.L2:
movl $.LC0, %edi
movl $0, %eax
call puts
jmp .L2
« Last Edit: November 15, 2009, 08:04:31 am by jho »
"I'm sorry, I only accept criticism in the form of sed expressions."

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: While loop not exiting
« Reply #17 on: November 14, 2009, 12:55:38 pm »
Usually when people say not to do things, I usually think its non-optimized and/or just not intelligent way of doing something.
Know what I mean?
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

 

Donate


* Search


* Recent Posts

I miss the chatbox by Shishka
[August 27, 2019, 11:52:39 pm]


Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]