Changes between Version 1 and Version 2 of IfStatement
- Timestamp:
- 12/20/10 02:40:49 (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
IfStatement
v1 v2 1 The '''while''' statement is used to repeatedly execute code while a condition is true.1 The if statement is used to conditionally execute code. 2 2 === Grammar === 3 3 {{{ 4 while-statement =5 [post] while<expression>4 if-statement = 5 if <expression> 6 6 <statements> 7 (else if <expression> 8 <statements>)* 9 [else 10 <statements>] 7 11 }}} 8 12 9 Without the '''post''', the expression is checked first and while true, the statements are repeatedly executed. If the expression is false the first time, the statements will not execute. With the '''post''', the condition is checked after the statements execute, so the statements will execute at least once. 10 11 Inside the statements, a '''break''' statement will exit the loop and a '''continue''' statement will skip the remaining statements and evaluate the expression again. In either case, an '''if''' statement is used to control when the loop is broken or continued (otherwise it would be pointless to have further statements in the loop). 13 Each expression is evaluated in order until one evaluates as true. Then the statements belonging to that expression are executed. If no expressions evaluate to true and the else portion is present then its statements are executed. Only one set of statements is ever executed and without an '''else''' it is possible that no statements are ever executed. 12 14 13 15 {{{ 14 16 #!cobra 15 17 # Example 1 16 while obj.someCondition 17 obj.process18 if name 19 print 'Hello, [name].' 18 20 19 21 # Example 2 20 while node 21 print node 22 node = node.next 22 if name 23 print 'Hello, [name].' 24 else 25 print "I don't know your name." 23 26 24 27 # Example 3 25 while node 26 if not node.isActive 27 print 'skipping inactive node: [node]' 28 continue 29 print node 30 node.process 31 node = node.next 32 33 # Example 4 34 while true 35 input = Console.readLine 36 if input is nil or input.trim.toLower == 'exit' 37 break 38 .processInput(input) 39 40 # Example 5 41 post while line and line.trim == '' 42 line = stream.readLine 28 if x < y 29 print 'x is smaller' 30 else if x > y 31 print 'x is larger' 32 else 33 print 'x and y are the same' 43 34 }}}