Two If conditions and only one else condition possible?

I am trying to write a code in the following way

if (/* First If condition */)
      {

      }
      if (/*second if condition*/) 
      {
        
      }
      else
      {
        
      }

What I am confused about is, this "else" condition, which "if" statement will it be comparing itself to? The first or the second? I would like it to compare itself to the second.

To the second.
In ide press ctrl-t
That will indrnt your code (it is wrong now).
Your indrntation suggests it is a nested if, but the {} say it is not..

2 Likes

A @build_1971 said, the 2nd if is outside the braces of the first - so it stands alone.

Fixing the indentation:

if (/* First If condition */)
{

} // first 'if' ends here

if (/*second if condition*/)   // entirely new and unrelated statement
{
        
}
else // immediately follows the 2nd 'if' - so that's the one it "attaches" to
{
        
}
1 Like

The second
As you have written, your two conditions are generally independent and will be checked sequentially

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.