Link to home
Start Free TrialLog in
Avatar of learn
learn

asked on

check this code

Hi experts

Can you improve following code for me. At least it should not do double check int(c1)==10. Don't tell me using "break".

   ifstream fin(filename);
   char c1;
   while (int(c1) != 10)
   {  c1 = fin.get();
      if (int(c1) == 10)
       ..........do something;
      else
       ..........do something else;
   }
Avatar of kellyjj
kellyjj

You could use a goto statement, but other than that I don't see any other way.  
Avatar of learn

ASKER

I think even "break" is better than "goto".
Avatar of ozo
c1 seems to be uninitialized the first time through the loop.

as the Qs is locked giving as comment
I suppose ur problem is that u shouldnot do c1=10 condition testing twice
so
use try catch logic

        ifstream fin("temp.txt");
                char c1;
      
         try
         {
               while (true)
               {  c1=fin.get();
                    if (int(c1) == 10)                                 
                          throw(anything);
                    else
                          //..........do something else for c1!=10;
               }
            }catch(that anything){//..........do something; for c1==10}
   bool flag = true;
    while (flag)
    {
        c1 = fin.get();
        if (int(c1) == 10)
        {
            // do something...
            flag = false;
        }
        else
            // do something else...
    }

If you like this solution better than the previous ones, reopen the question for it.

Never use exceptions for flow control!
Speaking as an assembly languange programer, the most efficient way is with the "break".  I find that "break" is good to use in code and does not make it hard to understand.  "Goto" is efficient too, but does potentially make it hard to understand.

Using a throw and catch is a bad idea.  That is very inefficient.  (several orders of magnitude slowe, you could do the test a 100 times and sill be faster.)

Alexo's answer appears to be what you want, but isn't.  If the test to be performed is complex and slow or cannot be repeated that is a good way (the best way) to do it.  However, in this case, where the text to be performed is fast, there is no advantage.  It takes just as long to test the bool for true as it does to test the character for 10.   Thus he introduced a new test with no advantage over the old test.

I would use a break or switch to a do loop (that tests at the bottom.).
Avatar of learn

ASKER

To nietod:

1. A book doesn't like "break", I should ask arther :-)
2. I think it still need doing double check even using do loop.

Cheers.
Yes, you will have to do the check twice with the do loop, the difference is that you don't have to initialize c1.  With a while loop, that is, with the check at the top, you need to initialize c1 with a "dummy" value, in order to get into the while loop.  If you don't c1 could be randomly initalized and could sometimes start out with a value of 10.  In that case, the while loop would never run.  With the do loop, the loop will always be entered so the code will run at least once.  Now this is not a speed efficiency thing.  with the while loop you will have an extra initialization and one extra test.  That is maybe 30 clock cycles..  On a 300Mhz processor that is 1 millionth of a second.  Do it doesn't save enough time to worry about, but it does save space as well and it makes the code a little safer (If someone were to later see that the initial value wasn't needed (but it is) and remove it....)

There is no way to avoid the double check unless you use do the break.  Alex's code apparently avoids it, but not really.  (Alex's mind was on his coming vacation.)  But in this case, the cost fo the double check is trivial.  As klittle as 2 clocks, but at most say 15 clock cycles.  Again a millionth of a second.  Even if you had to do the loop 1000 times it would be trivial.  Now if you had a test that took a long time to do, you could use Alex's approach then.  In that case the slow test would be performed only one time per loop.
>> Alex's mind was on his coming vacation.
Alex's mind was on the "requirement" not to use break.

My "solution" is a kludge.  By all means use break.

>> A book doesn't like "break"
Then the author of the book has a poor understanding of C/C++.  Get another book.
My personal recommendations are:
  For C: "The C programming language, 2nd edition" by Kernighan & Ritchie.
  For C++ (beginner): "C++ Primer" by Lippman.
  For C++ (advanced): "The C++ programming language, 3rd edition" by Stroustrup.
Avatar of learn

ASKER

To nietod:

I believe your last comment is the best one I can expected. I would like to pass the points to you.
Can I just reopen my question and you put something as the answer? Or just copy the last comment. In case somebody visit this question and get answer.

Cheers.
Avatar of learn

ASKER

To Alexo:

Thank you for your comments and you are very kind to suggest some books.

Cheers.
Let me add two more books, ones that you are probably not ready for, but will be before too long.  they are "Effective C++" and "More Effective C++"  They deal with some of the issues that have come up in the other question.  I don't think you would understand them yet, but start withthe C++ primer and then try them.  I believe they are the 2 most essential books in C++.  

If you want me to answer, you have to reject the current answer.  (Although as Alex pointed out, I broke the no break rule.)  
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
break may be the most reasonable way to do it.
Although, depending on how c1 is initialized, it may still be possible to rewrite it, perhaps as something like:

while( (c1 = fin.get()) != 10 ){
    ..........do something else;
}
........do something;

But we'd have to see how c1 was originally initialized to know whether that was equivalent,
or whether we'd have to rewite it some other way.
Avatar of learn

ASKER

To ozo and all experts who help me in this question:

Thank you very much. I didn't expected so many suggestions which let me know a lot. especialy, ozo suggested a very good alternative way, though too late :-)

Cheers to you all.