Question On If Statement

Hello,

My question is:

Can you have multiple if statements within an if statement?
Thanks in advance.


- Code Assassin
Your question isn't completely clear, if you mean testing if more than one condition is true then yes. Using the && (AND) operator, if all of the below conditions are true the statement as a whole will evaluate to true

1
2
if ( someCondition && someFunctionReturnsTrue() && ( someSmallNum < someLargeNum ) )
    doStuff();


The || (OR) operator evaluates to true if at least one of the conditions is true
Last edited on

Sorry, I'll try to elaborate a bit more:

I'm trying to allow users to select a location(or map) within an if statement. I don't think you can have multiple if statements in on if statement.

Like so:

1
2
3
4
5
cout<< " Please pick a location."<<endl;
cout<< " 1) Nacht der Untoten"<<endl;
cout<< " 2) Verruckt"<<endl;
cout<< " 3) Kino Der Toten"<<endl;
cin>>location;


The piece of code you see above is already contained by an if statement. What I'm asking is: can I add another if statement, within the one I have already.



Please let me know if this is a little more clearer.
Last edited on
1
2
3
4
5
6
7
8
9
10
if(stuff)
{
   if(other_stuff)
   {
        if(more_stuff)
        {
            do_stuff();
         }
    }
}


This is equivalent to
1
2
3
4
if(stuff && other_stuff && more_stuff)
{
    do_stuff();
}


The difference is only that depending on the situation one of these may be easier to read (here it's version 2, for more complex conditions it's usually version 1). Also, version 1 allows you to have an else case for each unmet condition. Which can be quite annoying if you have like 20 conditions and check each of these manually (thank C++ for exceptions).
Thanks for explaining, hanst99!

I agree. Thank C++ for exceptions! :)
If you want to pick something from a list, a switch statement may be more appropitiate. Google how to use them.
I will.

That's weird..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout<< " Please pick a location."<<endl;
cout<< " 1) Nacht der Untoten"<<endl;
cout<< " 2) Verruckt"<<endl;
cout<< " 3) Kino Der Toten"<<endl;
cin>>location;

 switch (location)
   { 
      case 1: cout<< "You slected 'Nacht der Untoten'"  ; 

      case 2:
         cout<< "You selected 'Verruckt'"<<endl;

      case 3: cout<< "You selected 'Kino Der Toten'"  ;}


When the user inputs a number it displays all cases instead of the case it's assigned to. For example, I tested my program and when I got to the map selection, I entered the number 1, and instead of it displaying case 1, it displayed all cases! :(

Still trying to solve the problem....


Nevermind...

I solved my problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cout<< " Please pick a location."<<endl;
cout<< " 1) Nacht der Untoten"<<endl;
cout<< " 2) Verruckt"<<endl;
cout<< " 3) Kino Der Toten"<<endl;
cin>>location;

 switch (location)
   { 
      case 1: cout<< "You slected 'Nacht der Untoten'" <<endl;
	  break ; 

      case 2:
         cout<< "You selected 'Verruckt'"<<endl;
		 break;

      case 3: cout<< "You selected 'Kino Der Toten'" <<endl;
	  break ;
	
      
}	


I was missing the break command.
What a stupid mistake :(
Last edited on
If you are using a switch statement you need to put a break; after each section where you don't want to continue to the next case.
Topic archived. No new replies allowed.