跳轉到內容

龍語言入門/課程/控制結構

來自華夏公益教科書,開放的書籍,開放的世界

控制結構

[編輯 | 編輯原始碼]

本章我們將學習 Dragon 程式語言提供的控制結構。

  • If 語句

語法

	if(Expression)
	{
		Block of statements
	}
	else if(Expression)
	{
		Block of statements
	}
	else {
		Block of statements
	}

示例

	select "types"
	select "graphic"
	nOption = int(prompt("1) Say Hello \n2) About")
	
	if nOption == 1	
	{
		name = prompt("Enter your name: ")
		showln "Hello " + name 
	}
	else if nOption == 2 
	{      
		showln "Sample : using if statement" 
	}
	else
	{ 
		showln "bad option..."
	}
  • While 迴圈

語法

	while(Expression)
	{
		Block of statements
	}


  • For 迴圈

語法

	for (identifier=initialize value, terminating expression, step expression)
	{
		Block of statements
	}

示例

	// print numbers from 1 to 10
	for (x = 1, x <= 10, x++)
	{
		showln x  
	}


  • Foreach 迴圈

語法

	for (identifier : List/String)
	{
		Block of statements
	}

示例

	aList = [1,2,3,4]  //create list contains numbers from 1 to 10
	for x : aList
	{
		showln x       // print numbers from 1 to 10
	}

Do While 迴圈

[編輯 | 編輯原始碼]

語法

	do {
		body
	} while (condition)

示例

	x = 1 
	do {
		showln x 
		x++ 
	}
	while (x <= 10)


華夏公益教科書