曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
VB入门教程

[VB Super Simple Intro] 9. Program Execution Order-1

Preface The author has been quite busy lately, so this tutorial series has been updating rather slowly. I saw some readers urging me to update in the WeChat official account backend, so I squeezed out some time to write a post~ Some of you worry that I won't have time to keep updating. Let me reiterate: as long as people are reading, I'll make time to keep updating, so please rest assured. About Program Execution Order When we write programs, we write them line by line, and when the program runs, it also runs line by line. However, real-world requirements are very complex. If we could only run line by line, our programs couldn't be called "programs"—they'd only be cal

曦远 (DealiAxy)约 14 分钟
目录10 个章节

Preface

The author has been quite busy lately, so this tutorial series has been updating rather slowly. I saw some readers urging me to update in the WeChat official account backend, so I squeezed out some time to write a post~ Some of you worry that I won't have time to keep updating. Let me reiterate: as long as people are reading, I'll make time to keep updating, so please rest assured.

About Program Execution Order

When we write programs, we write them line by line, and when the program runs, it also runs line by line. However, real-world requirements are very complex. If we could only run line by line, our programs couldn't be called "programs"—they'd only be called "batch processing."

In VB, we have three ways to control the program's execution order: judgment, loops, and jumps. Below we'll learn each of them in turn.

Judgment

Judgment, as the name suggests, is judgment—for example, judging which of two numbers is larger, judging whether a username and password match, and so on. Judgment in turn has two types of statements: conditional statements and select statements.

Conditional Statements

Below is a simple conditional statement structure:

if 条件1 then
    dosth()
elseif 条件2 then
    dosth2()
else
    dosth3()
end if

For the if statement, it executes like this: if the 条件 expression evaluates to true, then the code block after then is executed; if it's false, the statements after else are executed.

Here's an example:

Comparing the Size of Two Numbers

Code function: compare the size of two numbers and output the larger one.

dim a as integer
a = 1
dim b as integer
b = 2

if a > b then
    print a
else
    print b
end if

Code execution result:

2

Checking the Username

Code function: check whether the username is an administrator

dim username as string
username = inputbox("enter your username")
if username = "admin" then
    print "you are super user"
else
    print "you are not super user"
end if

Run result:

Input: admin

Output: you are super user

Naming Months by Season

Code function: output the corresponding months based on the season entered by the user

dim season as string
season = inputbox("please enter season")
if season = "spring" then
    print "Jan, Feb, Mar"
elseif season = "summer" then
    print "Apr, May, June"
elseif season = "autumn" then
    print "July, Aug, Sept"
elseif season = "winter" then
    print "Oct, Nov, Dec"
end if

Run result:

Input: spring

Output: Jan, Feb, Mar

Select Statements

In the examples above, there's one that names months by season. There are only four seasons in a year, so it's not a big problem—you can write four if else if statements to check. But what if we reverse it and name the season by month? Would we really have to write 12 statements?

To solve the problem of overly verbose code, VB uses the select statement, which is similar to the switch statement in C, except VB's select statement is more powerful!

Syntax:

Value selection: that is, performing different operations based on different values of a variable.

select case 变量
    case 1
        call dosth1()
    case 2
        call dosth2()
    case 3
        call dosth3()
    case else
        call dosth4()
end select

How does the select case statement run? VB first evaluates the test expression, then compares the expression's value with the value of each Case. If they are equal, it executes the statement block associated with that Case. If more than one Case matches the test expression, only the first matching Case has its associated statement block executed; if none of the values in the expression list match the test expression, the statements in the Case Else clause are executed.

You can set multiple comparison values for each Case statement

' 飞机棋游戏
dim number as integer
number = int(rnd * 6) + 1
select case number
    case 1,6
        print "起飞"
    case 2,3,4,5
        print "不起飞"
end select

You can also set multiple comparison values by specifying a range

dim score as integer
score = inputbox("请输入考试分数")
select case score
    case 90 to 100
        print "优秀"
    case 80 to 90
        print "良好"
    case 60 to 80
        print "及格"
    case else
        print "不及格"
end select

Conditional selection, a unique feature of VB's select statement, and a very handy one. It's equivalent to a simplified version of the if else if structure.

dim score as integer
score = inputbox("请输入考试分数")
select case sorce
    case is < 60
        print "不及格"
        exit select
    case is > 60 and is < 80
        print "及格"
        exit select
    case is > 80 and is < 90
        print "良好"
        exit select
    case is > 90
        print "优秀"
end select

Due to time and space constraints, I won't give more examples. A more detailed introduction to the syntax will be expanded in the next update, so please stay tuned~

end

That's all for program execution order in this article. Loops and jumps will be covered in the following articles. Hope you'll keep following along~

About


For more interesting tricks, please follow my WeChat official account: DealiAxy Every article is also collected on my blog: blog.deali.cn

_EOF_

[VB Super Simple Intro] 9. Program Execution Order-1

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

【VB超简单入门】八、四种运算

前言 这几天好像看这个VB系列教程的同学很多啊,有个小同学说写得不错嘿嘿,所以我又有动力继续更新下去。说一下哈,在公众号和博客里同步更新的哟,并且博客因为可以用Markdown写,所以排版会更舒服一点,建议大家方便的话来博客看,博客地址:blog.deali.cn 算术运算 算术运算包括加(+) 、 减(-) 、乘 (*)、除(/)、整除(\)(如7\2等于 3)、取余(Mod)(用来取余数,如7

2022-02-10 · VB入门教程 · 约 1 分钟

【VB超简单入门】二、知识准备

在开始编程之前,需要先熟悉一下各种操作和术语,以后学习编程才能得心应手。 首先最重要的操作当然就是…电脑的开机关机啦~(开个玩笑哈哈),必须掌握软件的安装和卸载,还有能编写批处理程序对平时的使用也是很有帮助的。 关于各种操作列个表~~ (由于时间关系,先列出相关点,详细操作以后我会慢慢补充~) l 查看、编辑注册表:使用regedit.exe修改系统注册表,我们写程序有时需要将程序的相关配置保存到

2022-11-13 · VB入门教程 · 约 1 分钟

【VB超简单入门】一、写在前面

前言 每本书的前面总得写点什么,到我这里也自然不能免俗,前言这东西“存在即合理”,所以就随便写一点咯~ 首先这本书是给从未接触过编程的童鞋准备的,由于我学识疏浅,对VB也只是一知半解所以也只能讲一点点最基础的知识,入门之后便可以自由学习更高级的东西了~ 编程其实并不难,有很多人一看到屏幕上密密麻麻的代码就被吓呆了= =… 也有很多童鞋问我编程是不是得背很多代码= =…其实根本不用背,编程涉及的单词

2022-11-13 · VB入门教程 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…