Example: if else statement

0
Now, here you are with an example of an if else  statement.

Start an new project, and add some controls to you windows form.

  • Four radio buttons
  • Three text boxes
  • Two Labels
  • And two buttons

Set up the design and properties of your program as shown in the figure below.

Let us discus the coding part of this form.

  •  First you double click on the result button.

For understanding the if else if statement visit previous lesson.

What you will do when the condition is pass, whether which radio button is selected. If addition radio button is selected the following code will get executed

Three variables are used here :

  • if(rbadd.checked  == true) will check whether the radio button is checked or not.
  • Assign val1 value through txtval1.text
  • Assign val2 value through txtval2.text
  • And the result variable will get the addition of val1 + val2
  • The result will be shown in messagebox.

. else If subtraction radio button is selected the following code will get executed

The same process will take place in else if condition.

Three variables are used here :

  • if(rbsub.checked  == true) will check whether the radio button is checked or not.
  • Assign val1 value through txtval1.text
  • Assign val2 value through txtval2.text
  • And the result variable will get the subtraction of val1 – val2
  • The result will be shown in messagebox.

. else If multiplication radio button is selected the following code will get executed

Three variables are used here :

  • if(rbmult.checked  == true) will check whether the radio button is checked or not.
  • Assign val1 value through txtval1.text
  • Assign val2 value through txtval2.text
  • And the result variable will get the multiplication of val1 * val2
  • The result will be shown in messagebox.

. You will find the same process occurring within division radio button.

At last you will find the else part. What it will do, it will execute only if non of the above if conditions are true.

Now, get back to the form design and double click the clear button.

What .checked = false do? It simply un-check the radio buttons, if any of the radio button is checked. And what string.empty will do? It clears the text boxes if there is any value in text box.

Download the code file here if else statement 

Thank you.

In the next lesson you will learn some concepts about scope of the variable.

If you enjoyed this article, Get email updates (It’s Free)

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

2 Responses

  1. Ahmad
    Ahmad at | | Reply

    yar muj say nhi howa
    Mai nay try ki hai hud
    iss pay jo varibale k kaya name rakay hai wo bataya he nhi
    Or
    Rabadd ya kaya hai
    Bhi ya b explain karo Na

    0

Leave a Reply