0

I was wondering if there is a command which would have the same function as exit.

So, for instance:

if (Average < 35) { MessageBox.Show("you failed"); **EXIT** } if (Average >= 75) { lblOutput.Text += Name +" " + Surname + ", " + "your average was: " + Average + ", you shall recieve a bursary!"; } else lblOutput.Text += Name +" " + Surname + ", " + "your average was: " + Average + ", you shall not revieve a bursary!"; 

Even if the average is lower than 35, the script will carry on going and the lblOutput will still say You shall not receive a bursary. while it should just show the MessageBox and not do anything with the label.

Could someone explain how to do this?

1
  • Are you trying to return from the function or terminate the process? Commented Mar 3, 2014 at 14:22

4 Answers 4

4

You can use return; to stop the execution path.

Sign up to request clarification or add additional context in comments.

3 Comments

that won't "Carry on" to the lblOutput code (as OP mentioned in question).
@BradChristie think you misread like I did initially, the OP says the code currently does that, but they don't want it to.
Ah, touche. Yea, must be a) not enough coffee, or b) a confusing question. (I'm leaning more toward the former)
1

I suppose that current code reside in a method with return type void, you can simply use return;

if (Average < 35) { MessageBox.Show("you failed"); return; } 

Comments

1

Alternatively to using return to exit early, you could write your if statements in such a way that it naturally follows that code path

if (Average >= 75) { lblOutput.Text += Name +" " + Surname + ", " + "your average was: " + Average + ", you shall recieve a bursary!"; } else if (Average >= 35) { lblOutput.Text += Name +" " + Surname + ", " + "your average was: " + Average + ", you shall not revieve a bursary!"; } else { MessageBox.Show("you failed"); } 

For me this is better from a readability point of view.

Comments

0

Instead or return just do

if() { } else { } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.