Sub cal end sub Function f 1i Dim
• Sub cal( ) • . • end sub • Function f 1(i) • Dim i as integer • f 1= 2*i+5 • End function
複習. Visual Basic 語言簡介 • 三 變數: 需宣告 (例如以下例) • Dim i as integer • Dim x as float • Dim y as double • Dim dd as date • Dim s 1 as string • Dim s 2 as string*80 • Dim A(10, 15) as Integer • const pi = 3. 141593 • Variant 不限型態
參. Visual Basic 語言簡介 • • • (3) Nested If If ( ) Then. Else. If. Else. End If
複習. Visual Basic 語言簡介 • (4) Select Dim id as integer Select case id case 1: . case else. End Select
複習. Visual Basic 語言簡介 • 五 迴圈 Loop • (i) For …. . Next • For I=1 To 10 • Print I • Next I • (ii) Do loop • I=1 • Do while I <=10 • Print I • I=I+1 • Loop
參. Visual Basic 語言簡介 • 五 迴圈 Loop • (iii) While-Wend迴圈 • I=1 • While I <=10 • Print I • I=I+1 • Wend
例一: function displaymessage() { alert("Hello World!"); } <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form>
例二: function product(a, b) { return a*b; } <script type="text/javascript"> document. write(product(4, 3)); </script>
• 三 變數: 需宣告 (例如以下例) var x; var carname; 例 var x=5; var carname="Volvo";
算符 + * / % ++ -- 敘述 例 結果 Addition x=y+2 x=7 y=5 Subtraction x=y-2 x=3 y=5 Multiplication x=y*2 x=10 y=5 Division x=y/2 x=2. 5 y=5 Modulus x=y%2 x=1 y=5 Increment x=++y x=6 y=6 x=y++ x=5 y=6 Decrement x=--y x=4 y=4 x=y-- x=5 y=4
例 txt 1="What a very"; txt 2="nice day"; txt 3=txt 1+" "+txt 2; 結果 " What a very nice day"
• 四 控制敘述 • 有條件執行的IF • (1) if (condition) { code to be executed if condition is true } • 例 • var d=new Date(); var time=d. get. Hours(); if (time<10) { document. write("<b>Good morning</b>"); }
(2) If …. . Else • if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true } • 例 • if (time < 10) { document. write("Good morning!"); } else { document. write("Good day!"); } </script>
• • (3) Nested If if (condition 1) { code to be executed if condition 1 is true } else if (condition 2) { code to be executed if condition 2 is true } else { code to be executed if neither condition 1 nor condition 2 is true }
(4) switch(n) { case 1: execute code block 1 break; case 2: execute code block 2 break; default: code to be executed if n is different from case 1 and 2 }
例: var d=new Date(); var the. Day=d. get. Day(); switch (the. Day) { case 5: document. write("Finally Friday"); break; case 6: document. write("Super Saturday"); break; case 0: document. write("Sleepy Sunday"); break; default: document. write("I'm looking forward to this weekend!"); }
• 五 迴圈 Loop • for (variable=startvalue; variable<=endvalue; vari able=variable+increment) { code to be executed } • 例一 • var i=0; for (i=0; i<=5; i++) { document. write("The number is " + i); document. write(" "); }
• while (variable<=endvalue) { code to be executed } • 例二 • var i=0; while (i<=5) { document. write("The number is " + i); document. write(" "); i++; }
• do { code to be executed } while (variable<=endvalue); • 例三 • var i=0; do { document. write("The number is " + i); document. write(" "); i++; } while (i<=5);
- Slides: 35