VBScript အခြေအနေအရာ

အကျိုးသတ္တု

If...then..else စကား
ဒီ အကြောင်းအရာကို ရိုးစကား if...then..else ကျသတ်ခြင်း ပြောဆိုထားသည်။
If...then..elseif စကား
ဒီ အကြောင်းအရာကို ရိုးစကား if...then...elseif... ကျသတ်ခြင်း ပြောဆိုထားသည်။
ဆိုင်းတုံးစကား
This example demonstrates how to write a select case statement.

Conditional statements

Frequently, when we write code, we need to perform different operations based on different judgments. We can use conditional statements to complete this work.

In VBScript, we can use three conditional statements:

if statement
If you want to execute a series of code when the condition is true, you can use this statement.
if...then...else statement
If you want to execute one of two sets of code, you can use this statement.
if...then...elseif statement
If you want to execute one of multiple sets of code, you can use this statement.
Select case statement
If you want to execute one of multiple sets of code, you can use this statement.

If....Then.....Else

In the following cases, you can use the If...Then...Else statement:

  • Execute a piece of code when the condition is true
  • Select two pieces of code to execute

true အခြေအနေသတ်မှတ်သော အခါ မည်သည့် ဖော်ပြချက်ကို လုပ်ဆောင်ရန် ရှိလျှင် အပြင်းအထန် အကယ်၍ ဖော်ပြချက် တစ်ကြိမ်တည်းမျှ ရေးထိုးရမည်

if i=10 Then msgbox "Hello"

အထက်အရာဝတ္တုများတွင် လည်းကောင်း သို့မဟုတ် .else.. ဖော်ပြချက် မရှိပေ။ ကျွန်တော်တို့ သည် အခြေအနေသတ်မှတ်သော အခါ တစ်ခုသာ လုပ်ဆောင်ခဲ့ကြသည် (i သည် 10 ကဲ့သို့)

true အခြေအနေသတ်မှတ်သော အခါ မည်သည့် ဖော်ပြချက်ကို လုပ်ဆောင်ရန် ရှိလျှင် အပြင်းအထန် အကယ်၍ တစ်ကြိမ်တည်းမျှ ဖော်ပြချက် တစ်ခုသာ ရေးထိုးရမည် ပြီး ထို့ပြီးနောက် "End If" သံသားကို အဆုံးဖြတ်ရမည်

if i=10 Then
   msgbox "Hello"
   i = i+1
end If

အထက်အရာဝတ္တုများတွင် လည်းကောင်း သို့မဟုတ် .else.. ဖော်ပြချက် မရှိပေ။ ကျွန်တော်တို့ သည် အခြေအနေသတ်မှတ်သော အခါ များစွာ လုပ်ဆောင်ခဲ့ကြသည်

true အခြေအနေသတ်မှတ်သော အခါ မည်သည့် ဖော်ပြချက်ကို လုပ်ဆောင်ရန် ရှိလျှင် အပြင်းအထန် အကယ်၍ "Else" သံသားကို ထည့်သွင်းရမည်

if i=10 then
   msgbox "Hello"
else
   msgbox "Goodbye"
end If

true အခြေအနေသတ်မှတ်သော အခါ ပထမပိုင်း ကို လုပ်ဆောင်ပါ၊ အခြေအနေသတ်မှတ်သော အခါ မမျှတသော အခါ ဒုတိယပိုင်း ကို လုပ်ဆောင်ပါ (i မပြည့် 10 ကဲ့သို့)

If....Then.....Elseif

ဤအပိုင်းကို မျိုးပေါင်းအပိုင်းတစ်ခုကို လုပ်ဆောင်ရန် ရှိလျှင် အပြင်းအထန် အကယ်၍ if...then...elseif ဖော်ပြချက်ကို အသုံးပြုနိုင်ပါသည်

if payment="Cash" then
   msgbox "You are going to pay cash!"
 elseif payment="Visa" then
   msgbox "You are going to pay with visa."
 elseif payment="AmEx" then
   msgbox "You are going to pay with American Express."
 else
   msgbox "Unknown method of payment."
end If

Select Case

假如你希望选择多套代码之一来执行,可以使用 SELECT 语句:

select case payment
 case "Cash"
   msgbox "You are going to pay cash"
 case "Visa"
   msgbox "You are going to pay with visa"
 case "AmEx"
   msgbox "You are going to pay with American Express"
 case Else
   msgbox "Unknown method of payment"
end select

以上代码的工作原理:首先,我们需要一个简单的表达式(常常是一个变量),并且这个表达式会被做一次求值运算。然后,表达式的值会与每个 case 中的值作比较,如果匹配,被匹配的 case 所对应的代码会被执行。