2 Replies - 313 Views - Last Post: 21 August 2009 - 10:19 PM Rate Topic: -----

#1 plaforte  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-August 09

Basic question on IF structure

Post icon  Posted 21 August 2009 - 09:15 PM

Hello, I am used to VB and just getting familiar with C++

If I have more than one condition to validate before doing an action, how do I do that ? do I simply put the IF one after one like this

if (condition1)
if (condition2)
if (condition3)
{
statement;
}

I have researched in eBooks but somehow I cannot get it working

Thank you
Is This A Good Question/Topic? 0
  • +

Replies To: Basic question on IF structure

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Basic question on IF structure

Posted 21 August 2009 - 09:28 PM

View Postplaforte, on 21 Aug, 2009 - 11:15 PM, said:

If I have more than one condition to validate before doing an action, how do I do that ? do I simply put the IF one after one like this

if (condition1)
if (condition2)
if (condition3)
{
statement;
}


Yes, that looks right. I would indent the code and include brackets, though!

if(condition){       
    if(condition){
        if(condition){
            //do stuff, as long as all 3 conditions evaluate to true;
        }
    }
}


Or you could just use AND (&&) or OR (||) depending on what you need to check

        if(condition1 && condition2 && condition3){
            //do stuff, as long as all 3 conditions evaluate to true;
        }
       


        if(condition1 || condition2 || condition3){
            //do stuff, as long as at least one of the 3 conditions evaluates to true;
        }
       

This post has been edited by OliveOyl3471: 21 August 2009 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5395
  • View blog
  • Posts: 27,389
  • Joined: 10-May 07

Re: Basic question on IF structure

Posted 21 August 2009 - 10:19 PM

Whever I have more than three ifs, I prefer to use a switch case.
	  switch ( c ) {
		 case 'a':
			break;
		 case 'b':
			break;
		 case 'c':
			break;
		 default:
			..
	  }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1