karagulamos's Profile User Rating: -----

Reputation: 20 Tradesman
Group:
Contributors
Active Posts:
17 (0.01 per day)
Joined:
24-November 10
Profile Views:
2,857
Last Active:
User is offline Dec 12 2013 05:53 AM
Currently:
Offline

Previous Fields

Country:
NG
OS Preference:
Windows
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
Playstation
Your Car:
Who Cares
Dream Kudos:
175

Latest Visitors

Icon   karagulamos Which is correct, "to be or not to be"?

Posts I've Made

  1. In Topic: do {} while(0);

    Posted 12 Aug 2012

    View Postsparkart, on 17 August 2011 - 11:14 PM, said:

    With the do while loop, you can break out of the code on failure:

    do
    {
      // Failure with return a null(0) value.
      p = CreateNewObject();
      if (p == 0) break;
    
      o = CreateDifferentObject();
      if (o == 0) break;
    
      //...
    } while(0);
    
    


    The above method can be avoided. Assuming that the code above is legally correct and the intention is to "do stuff" only when the creation of objects p && o succeeds, then I find the following examples more intuitive:

    Example 1
    =========


    //...
    p = CreatNewObject();
    o = CreateDifferentObject(); 
    
    if(p != 0 && o != 0){ // true if the creation of p && o succeeded
       //do stuff ...
    }
    
    // Perhaps some more code and/or cleanup ...
    
    


    Note that Example 1 incurs the extra cost of creating the object o regardless of whether or not the creation of p succeeded.

    Example 2
    =========


    bool created = false;
    
    //...
    p = CreateNewObject();
    
    if(p != 0){
       o = CreateDifferentObject();
       if(o != 0) created = true;
    }
    
    if(created){ // true if the creation of p && o succeeded
       //do stuff ...
    }
    
    // Perhaps some more code and/or cleanup ...
    
    


    Cheers!

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
July 19
Gender:
Location:
Nigeria
Interests:
Art, Dance, Drama, Friends, Music and Parapsychology.
Full Name:
Alexander Karagulamos
Years Programming:
4
Programming Languages:
Assembly, C, C++, Java, VB, Pascal, Python, HTML, CSS, JavaScript, PHP

Contact Information

E-mail:
Click here to e-mail me
Yahoo:
Yahoo  karagulamos
Facebook:
https://www.facebook.com/karagulamos

Friends

karagulamos hasn't added any friends yet.

Comments

karagulamos has no profile comments yet. Why not say hello?