跳至內容

GCSE 計算機/程式設計

來自華夏公益教科書

以下示例符合 GCSE 計算機 的結構。程式碼是用 VB(.net) 和 C#(.net) 編寫的。在 c# 中,// 和在 VB 中的 ' 是註釋,編譯器會忽略它們。它們是為了幫助你理解程式碼。

程式語言規則

[編輯 | 編輯原始碼]

每種程式語言都有自己的規則,這是它們之間的主要區別。許多語言有一些相同的規則。

必須在每行程式碼的末尾使用 (要將行繼續到下一行,不要使用 ;)

必須使用 {} 來包含程式碼

要將行繼續到下一行,請使用 _ 程式碼包含各種

宣告變數

[編輯 | 編輯原始碼]

//(型別,例如 int 或 string) (名稱,例如 i); int i; //名為 i 的整數 (與 VB 的可選 as 不同 ... C# 使用 type object 作為任何型別)

dim i as integer //名為 i 的整數 (as integer 可選)

IF 條件

[編輯 | 編輯原始碼]

if (條件,例如 1=1) {

 //Do something if true

}else{

 //Do something if false

}

if 條件,例如 1=1 then

 'Do something if true

else

 'Do something if false

end if

int i; //宣告 i For (i=0; i<10, i++) {

 //i is an integer variable and the for checks if i = 10 every loop. If not does the process within then adds 1 to i.

}

For i As Integer = 1 To 10

  'i is an integer variable and the for checks if i = 10 every loop. If not does the process within then adds 1 to i.

Next i

int i; While(i<10) //檢查 i 是否小於 10 {

 //Loops till i = 10
 i++; //Adds 1 to i

}

dim i While i<10 '檢查 i 是否小於 10

 'Loops till i = 10
 i+=1 'Adds 1 to i

End While

Repeat(loop)

[編輯 | 編輯原始碼]

While(true) {

 //Does this for as long as the program is open

}

Do

 'Does this for as long as the program is open

Loop

華夏公益教科書