user1659510
user1659510

Reputation: 283

How to stop execution of if-statement

Hi I want to stop the execution of if-loop ,I have tried with 'return' statement but its exits from the function ,So how can I exit from the single if Statement.I have tried with following code...

Here I want to stop execution of if(CheckHorizontalSide(SourceMember)) and by stopping this I want to move towards the if(CheckTop(SourceMember))

void A()
{


      if (CheckHorizontalSide(SourceMember))
        {

            if (lblHorizontalMember.Text == DestinationMember)
            {

                lsRelationPath.Add(lblHorizontalMember.Text);
                lblRelationPath.Text = String.Join("-", lsRelationPath);
                lblRelationPath.Visible = true;
                return;
            }

            bool WhetherContains = lsRelationPath.Contains(SourceMember);
            if (WhetherContains)
            {
                return;
            }

            //This below code is not related to the above 'WhetherContains '
            lsMemberID1.Clear();
            lsRelationPath.Add(lblHorizontalMember.Text);


            Find_Route(lblHorizontalMember.Text, DestinationMember);
        }


    if(CheckTop(SourceMember))
    {
    //code here....
    }
}

Upvotes: 0

Views: 11813

Answers (1)

Tom Blodget
Tom Blodget

Reputation: 20782

You put the rest of the block in a sub-block with { } and put else in front of that.

You can nest as deeply as you want but you might try factoring out blocks to helper functions to reduce the complexity and give statements a name.

 if (WhetherContains)
 {
     // this is actually empty
 }
 else
 {
     lsMemberID1.Clear();
     lsRelationPath.Add(lblHorizontalMember.Text);    
 }

Or,

 if (!WhetherContains)
 {
     lsMemberID1.Clear();
     lsRelationPath.Add(lblHorizontalMember.Text);    
 }

Upvotes: 1

Related Questions