Page 50 - Computing Book 8
P. 50

Developing Applications – Visual Studio                                              Class 8


               Use decisions and repetitions:

               Programming is all about giving autonomy to computers as we know computers have limited
               intelligence and they can’t make decisions on their own unless we program them to take decisions.
               Therefore, decision making is an essential part of programming and that means we can write a
               program that will be able to respond to user input during its execution and give different results for
               different inputs. This process is possible with the use of conditional statements or If…Then
               statements.

               IF…Then Statement

               Follow the steps below to create a simple program using condition
               statements:
                   1.  Create a new form with two labels, one button and one text box
                       as shown in the example.
                   2.  Edit the first Label text to “Enter your lucky number”
                   3.  Change the name of the text box to “TextboxNumber”
                   4.  Edit the button text to “Try your luck” and change its name to
                       “btnCheck”
                   5.  Edit 2nd label text to “Result” and its name to “lblResult”
                   6.  Double click on the “Try your luck button” to open the script
                       window.
                   7.  Enter the code as shown.
                   8.  In the first line, we have declared a
                       variable “number” as an integer.
                   9.   In the second line, we have assigned
                       the value of number variable to
                       TextboxNumber which is a user input
                       text box.
                   10. In the third line, we set a conditional statement that if the user input number is greater or
                       equals to 7 then display the message “Congratulations you win!” in the “lblResult” Label
                       element.

               IF…Then…Else Statement

               A more complex version of the conditional statement is the If…Then…Else statement. This allows you
               to define instructions to be executed if the condition is false. For example, let’s say we want the
               previous program to display a relevant message to the user not only when the correct number is
               found but also when a wrong number is inserted.

               Make the following
               changes to your
               existing program’s
               code.









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