In any c++ compiler, is this allowed?

In a "nested if statement" is this ok?
Although I didn't put the cout statement inside the {} ?

1
2
3
4
5
if (m=='a') 
{
if ((n<=19)&&(n>=1))
cout<<"Your zodiac sign is Capricorn.";
}	
Yes, just as the following is allowed.
1
2
3
if (m=='a') 
   if ((n<=19)&&(n>=1))
      cout<<"Your zodiac sign is Capricorn.";


The braces are optional, but be careful since not using braces can lead to subtle hard to find bugs. For example look at the following:

1
2
3
4
if (m=='a')
   z = 34; 
   if ((n<=19)&&(n>=1))
      cout<<"Your zodiac sign is Capricorn.";


In this case only the "z = 34;" is part of that first if statement, because without the braces control statements contain only one line following the statement unless braces are used. Because of this, I recommend novice programmers always use braces with all control statements, even when not technically required.

Thank you very much.
Topic archived. No new replies allowed.