跳至內容

Ring/教程/控制結構

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

控制結構

[編輯 | 編輯原始碼]

在本節中,我們將學習 Ring 程式語言提供的控制結構。

  • If 語句

語法

	if Expression
		Block of statements
	but Expression
		Block of statements
	else
		Block of statements
	ok

示例

	see " 
		Main Menu
		---------
		(1) Say Hello
		(2) About
		(3) Exit

	    " give nOption

	if nOption = 1	see "Enter your name : " give name see "Hello " + name + nl
	but nOption = 2 see "Sample : using if statement" + nl
	but nOption = 3 bye
	else see "bad option..." + nl
	ok
  • Switch 語句

語法

	switch Expression
	on Expression
		Block of statements
	other
		Block of statements
	off

示例

	See " 
		Main Menu
		---------
		(1) Say Hello
		(2) About
		(3) Exit

	    " Give nOption

	Switch nOption
	On 1 See "Enter your name : " Give name See "Hello " + name + nl
	On 2 See "Sample : using switch statement" + nl
	On 3 Bye
	Other See "bad option..." + nl
	Off


  • While 迴圈

語法

	while Expression
		Block of statements
	end

示例

	While True

		See " 
			Main Menu
			---------
			(1) Say Hello
			(2) About
			(3) Exit

		    " Give nOption

		Switch nOption
		On 1 
			See "Enter your name : " 
			Give name 
			See "Hello " + name + nl
		On 2 
			See "Sample : using while loop" + nl
		On 3 
			Bye
		Other 
			See "bad option..." + nl
		Off
	End
  • For 迴圈

語法

	for identifier=expression to expression [step expression]
		Block of statements
	next

示例

	# print numbers from 1 to 10
	for x = 1 to 10	 see x + nl  next

示例

	# Dynamic loop
	See "Start : " give nStart       
	See "End   : " give nEnd
	See "Step  : " give nStep
	For x = nStart to nEnd Step nStep
		see x + nl
	Next

示例

	# print even numbers from 0 to 10
	for x = 0 to 10 step 2
		see x + nl
	next

示例

	# print even numbers from 10 to 0
	for x = 10 to 0 step -2
		see x + nl
	next
  • For in 迴圈

語法

	for identifier in List/String  [step expression]
		Block of statements
	next

示例

	aList = 1:10	# create list contains numbers from 1 to 10
	for x in aList  see x + nl  next  # print numbers from 1 to 10

在 For in 中使用 Step 選項

[編輯 | 編輯原始碼]

我們可以在 For in 中使用 Step 選項來跳過每次迭代中的多個項。

示例

	aList = 1:10	# create list contains numbers from 1 to 10
	# print odd items inside the list
	for x in aList step 2
		see x + nl  
	next

使用 For in 修改列表

[編輯 | 編輯原始碼]

當我們使用 (For in) 時,我們會按引用獲取專案。

這意味著我們可以在迴圈中讀取/編輯專案。

示例

	aList = 1:5	# create list contains numbers from 1 to 5
	# replace list numbers with strings
	for x in aList  
		switch x
		on 1  x = "one"
		on 2  x = "two"
		on 3  x = "three"
		on 4  x = "four"
		on 5  x = "five"
		off
	next
	see aList	# print the list items

Do Again 迴圈

[編輯 | 編輯原始碼]

語法

	
	do
		Block of statements
	again expression

示例

	x = 1 
	do 
		see x + nl 
		x++ 
	again x <= 10


Exit 命令

[編輯 | 編輯原始碼]

用於跳出迴圈。


語法

	exit [expression]	# inside loop


示例

	for x = 1 to 10
		see x + nl
		if x = 5 exit ok
	next

退出兩個迴圈

[編輯 | 編輯原始碼]

以下示例展示瞭如何使用 exit 命令從兩個迴圈中一步跳出。

示例

	for x = 1 to 10
		for y = 1 to 10
			see "x=" + x + " y=" + y + nl
			if x = 3 and y = 5
				exit 2	   # exit from 2 loops 
			ok
		next
	next
  • Loop 命令

用於跳到迴圈的下一輪迭代。

語法

	loop [expression]	# inside loop

示例

	for x = 1 to 10
		if x = 3
			see "Number Three" + nl
			loop
		ok
		see x + nl
	next

子函式中的 Exit/Loop

[編輯 | 編輯原始碼]

當我們在迴圈中時,我們可以呼叫一個函式,然後在該函式中使用 exit 和/或 loop 命令,該命令將作用於外部迴圈。

示例

	# print numbers from 1 to 10 except number 5.

	for x = 1 to 10
		ignore(x,5)
		see x + nl
	next

	func ignore x,y
		if x = y
			loop
		ok


短路求值

[編輯 | 編輯原始碼]

邏輯運算子 and/or 遵循`短路求值 <http://en.wikipedia.org/wiki/Short-circuit_evaluation>`_。

如果 AND 運算子的第一個引數為零,則無需計算第二個引數,結果將為零。

如果 OR 運算子的第一個引數為一,則無需計算第二個引數,結果將為一。

示例

	/* output
	** nice 
	** nice 
	** great	
	*/

	x = 0 y = 10

	if (x = 0 and nice()) and (y = 10 and nice())
		see "great" + nl
	ok

	func nice  see "nice" + nl   return 1

示例

	# No output

	x = 0 y = 10

	if (x = 1 and nice()) and (y = 10 and nice())
		see "great" + nl
	ok

	func nice  see "nice" + nl   return 1

示例

	/* output 
	** nice
	** great
	*/
 
	x = 0 y = 10

	if (x = 0 and nice()) or (y = 10 and nice())
		see "great" + nl
	ok

	func nice  see "nice" + nl  return 1

關於求值的註釋

[編輯 | 編輯原始碼]
  • True、False、nl & NULL 是語言定義的變數。
  • True = 1
  • False = 0
  • nl = 換行符
  • NULL = 空字串 = ""
  • 除了 0 (False) 之外,所有內容都求值為真。

示例

	# output = message from the if statement

	if 5 	# 5 evaluates to true because it's not zero (0).
		see "message from the if statement" + nl
	ok


華夏公益教科書