跳转到内容

龙语言入门/课程/控制结构

来自维基教科书,开放的书籍,开放的世界

控制结构

[编辑 | 编辑源代码]

本章我们将学习 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)


华夏公益教科书