VBS basic tutorial 3

Then last class , we will learn the structure of judgment.

Introduction

Before learning to judge the structure, we must first understand a new concept: the so-called Boolean value (Boolean), this kind of variable has only two possibilities: true (True) or false (Flase). This kind of variable is very in some cases Useful (such as "switch"). The way we define a Boolean variable is the same as other variables, and the assignment is the same, for example:

 dim a,b 
   a=true 
     b=false 

Okay, let’s get to the point. Let’s understand the simple judgment structure sentence. Its formula is: **if judgment type then sentence body**, referred to as if sentence. If here can be understood as "if", then temporarily understood as then. The formula means: if so, so be it. Let’s look at an example.

 dim a,b 
  a=29
  b=21 
  if a>b then msgbox "a大于b" 

Parsing

Let me explain it in vernacular: First, define a and b two variables, and assign them corresponding values. Then if b>a if b is greater than a (this is the judgment formula mentioned earlier) then msgbox "..." the output content b is greater than a (this is the execution statement body mentioned earlier). The judgment here will only return two results: either b>a, or a>b, that is, either true or false, if true, the output will be executed, and false will not be executed.
But if we want to execute the result that a is greater than b, else will come on stage. It can be understood as "otherwise", that is, what to execute if the result is false, let's look at an example:

dim a,b 
  a=29
    b=21
      if a>b then msgbox "a大于b" 
      else
      msgbox"b大于a"
      end if

Some friends may have questions: Why does an end if suddenly pop up? What we introduced before was a simplified if statement. Now this if statement uses end if to indicate the end of the if statement. In this way, we have perfected the formula: if judgment then execute statement else execute statement end if

Next, I will give you a question: please set a number less than ten, let others guess, if others guess right, output... if others guess wrong, output... (using if statement)

dim a
  a=inputbox("输入一个小于10的数") 
      a=int(a) 
          if a=8 then 
            msgbox "正确" 
              else 
                msgbox "错误" 
                  end if 

Haha, is it easy to make a "mini game"? (Although not very fun) Some students may ask: Why do I always write wrong? Remember what I said before? The result received by inputbox is a string by default, like 8≠"8", 8 is a number, and "8" is a string. They will never be equal. To make them equal, use int() to convert to an integer type. (= can also mean equal.)
Let's learn the logical operators and (and) and or (or). For example, if you want to determine that two conditions must be met at the same time, that is, both conditions are true, use and in the middle. If you only need to meet one of several conditions, use or. Here is an example of and.

dim a,b 
  a=inputbox("输入一个数 >10") 
  b=inputbox("输入另一个数 >10") 
  a=int(a) 
  b=int(b) 
  if a>10 and b>10 then 
  msgbox "正确" 
  else 
  msgbox "错误" 
  end if 

This program makes the two numbers that must be entered are greater than ten, then the output is correct, if one is not greater than ten, the output is wrong

dim a,b 
  a=inputbox("输入一个数 >10") 
  b=inputbox("输入另一个数 >10") 
  a=int(a) 
  b=int(b) 
  if a>10 or b>10 then 
  msgbox "正确" 
  else 
  msgbox "错误" 
  end if 

As long as one of the programs is greater than 10, the output is correct.

After reading the program, does everyone know at a glance?

operation

1) Use 3 bool values ​​to store whether your family is male (hint: sister1male=false)
    2) Given a number, greater than 10 and less than 20, output "correct", otherwise output "error"
      3) Input 12, Or 15, output "correct", otherwise output "error"
          5) Design a program at will, apply today's knowledge            The
          production process is relatively hurried, please correct me in the comment area if there is something wrong

Guess you like

Origin blog.csdn.net/CSDN_C2/article/details/105906329