Thread: if and else if statements

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    if and else if statements

    Hey,

    I was just wandering if it is better to handle the else statements for all the if's.

    A lot of the times in my code I only use the if (true) do this, but there is no else statement.

    Is that inproper? Can it lead to problems, and what kind of problems?
    Everything is relative...

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Are you saying that in your code you actually write "if (true)"? Let's see an example of what you mean.

    There are no syntax problems with using if..else statements (it would be a really bad language if there were). Saying if(true) is useless, obviously, and an else corresponding to that 'if' would be meaningless.

    Try to explain a little better exactly what you're saying.

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    i think he means just if ( some expression that evaluates true )..

    i use if statements with out the else if statements i find it pointless.

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    no, no I don't use if(true). That's useless, unless true is defined as a variable that changes value. Anyway, for example I have this piece of code:
    Code:
    for (j=0; j< vars.NumScan; j++)
    	{
    		k=0;
    
    		for( i=0; i< FAD.vectDL.size(); i++)
    		{
    		
    			if ( strncmp(FAD.vectDL[i].Param, "ENDL", 4) != 0 && strncmp(FAD.vectDL[i].Param, "END", 3) != 0)
    			{
    				if(MakeWord(out, &FAD.vectDL[i],
    					mtxVicVals[j][k], TempWord) == 1)
    				{
    					//fprintf(out, "%08x ", TempWord);
    					AWords2d[j].push_back(TempWord);
    				}
    				k++;
    			}
    		}
    		//fprintf(out, "\n");
    	}
    I only define what happens if a conditin is true, I don't define what to do if it's not.
    Everything is relative...

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I wouldn't say it's a problem that you don't handle the else, unless it could yield undesired results when none of the expressions evaluate to true.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, there's several times when there's nothing to do in an else... an empty else is a valid statement, but I've never used it
    Code:
    if(something)
    {
    	doSomething();
    }
    else {} 	//I've never actually used this, but it's valid
    » unless true is defined as a variable that changes value

    true is a keyword in most languages - you can't name a variable after a keyword.
    Last edited by major_small; 07-18-2005 at 12:05 PM. Reason: trigger-happy syntax highlighter :-(
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed