Multiple lines of code in if statement without curly braces I have used multiple lines of code in one if condition without using curly braces, which is executing properly. So what is the main necessity of using curly braces in if statements for multiple lines?

 if (ggcCompareGrid.TopLevelTable != null)
            foreach (GridRow row in ggcCompareGrid.TopLevelTable.Rows)
            {
                if (row.DisplayElement.ParentGroup != null)
                    if (row.DisplayElement.ParentGroup.Category != null)
                        if ((int)row.DisplayElement.ParentGroup.Category == key &amp;&amp; row.Record == null)
                        {
                            row.DisplayElement.ParentGroup.IsExpanded = false;
                            break;
                        }
            }
The problem with massively nested if statements without the { } is that it is a fragile structure. It is easy to come in and add a new statement and not realize whether it is or is not in one of the ifs or not. It also makes else statements harder to follow as not everyone is completely familiar with the precedence rules for else statements.

It works, it's entirely valid code - but it is bad style.

In this particular case you don't even need the nested ifs:

if (ggcCompareGrid.TopLevelTable != null)
{
        foreach (GridRow row in ggcCompareGrid.TopLevelTable.Rows)
        {
            if (row.DisplayElement.ParentGroup != null &amp;&amp; 
                  row.DisplayElement.ParentGroup.Category != null &amp;&amp; 
                  (int)row.DisplayElement.ParentGroup.Category == key &amp;&amp; 
                   row.Record == null)
            {
                  row.DisplayElement.ParentGroup.IsExpanded = false;
                  break;
            }
        }
  }


You could also consider in this case having a private boolean function that does the comparison:

 private boolean checkParentGroup(ParentGroup group, int key, Record record) {
      return ((group != null) &amp;&amp; (group.Category != null) &amp;&amp; 
                  ((int)group.Category == key) &amp;&amp; 
                   (record == null))
 }

if (ggcCompareGrid.TopLevelTable != null)
{
        foreach (GridRow row in ggcCompareGrid.TopLevelTable.Rows)
        {
            if (checkParentGroup(row.DisplayElement.ParentGroup, key, row.Record)
            {
                  row.DisplayElement.ParentGroup.IsExpanded = false;
                  break;
            }
        }
  }


This makes every part of the code simpler and clearer.

Obviously the checkParentGroup method should have a better name though, describing what it is actually checking.