Page 51 - Computing Book 8
P. 51

Developing Applications – Visual Studio                                              Class 8


               If…Then…ElseIf Statement

               If there are more than two choices to display, using the If...Then...Else statement is not enough. To
               provide more choices, we can use the If...Then...ElseIf statement. In the following example, we have
               created a program that translates a student’s score to the corresponding grade.
               Follow the steps below to create the program using If…Then…Elself statement:
                   1.  Create a new form with three labels, one text box and a button.
                   2.  Edit the text for the first label to “Enter Your Score”.
                   3.  Edit the button text to Show Grade.
                   4.  Edit the text of the second label to “Your Grade:”
                   5.  Place the third label adjacent to the second label and we will use
                       this label to generate the grade in it, edit the label text to “Grade
                       not generated yet” and also change the name of this label to “lblGrade” from the properties
                       panel.
                   6.  Enter the code as shown in the example:



                         Declaration of Score Variable
                         Declaration of Grade Variable
                                                                         Declaration of score value which would
                                                                         be the TextBox1 which is user input

                      If…Then…Else  Statement  which
                      will take decision about the grade.
                      Data  will  be  taken  from  the  text
                      box and evaluated by the program
                      at every line and when the criteria                This is the 3  label which will display the
                                                                                 rd
                      matches  it  will  set  the  value  of             value  of  grade  variable  after  taking
                      grade to the relevant string value
                                                                         decision from if…then...else statement
                   •  The first condition score >= 90 is evaluated and if it is true, then the variable grade takes the
                       value A.
                   •  If a condition is found true, the following conditions are not evaluated and the instructions
                       under End If are executed.

                   •  If a condition is evaluated as false, then the next condition is evaluated and so on, until a
                       condition is true.
                   •  If none of the conditions are found to be true, then the code under Else is executed which
                       has a string value of “Need Improvement”.
               Conditional and Logical Operators

               Conditional and Logical operators can be used in making decision-based programs:
                        Conditional Operators                              Logical Operators
                   Operator          Meaning               Operator                  Meaning
                      =        Equal to                      And      Both sides must be true
                      >        More than                      Or      One side or other must be true
                      <        Less Than                      XOr     One side or other must be true but not both
                      >=       More than or equal to          Not     Negates truth
                      <=       Less than or equal to
                      <>       Not Equal to







               The City School /Academics/Computing Curriculum/Class 8/2020-2021                Page 50 of 75
   46   47   48   49   50   51   52   53   54   55   56