This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1 @@
{{Control Structures}}This page lists the conditional structures offered by different programming languages. Common conditional structures are '''if-then-else''' and '''switch'''.

View file

@ -0,0 +1,2 @@
---
note: Control Structures

View file

@ -0,0 +1,2 @@
LDA #10
CMP #11

View file

@ -0,0 +1,20 @@
BNE ;Branch on Not Equal - branch when the zero flag is set
BEQ ;Branch on EQual - branch when the zero flag is set.
;The zero flag is set when the result of an operation is zero
BMI ;Branch on MInus
BPL ;Branch on PLus - branch when the sign flag is cleared/set.
;The sign flag is set when the result of an instruction is a negative number
;and cleared when the result is a positive number
BVS ;Branch on oVerflow Set
BVC ;Branch on oVerflow Cleared - branch when the overflow flag is cleared/set.
;The overflow flag is set when the result of an addition/subtraction would
;result in a number larger than 127 or smaller than -128
BCS ;Branch on Carry Set
BCC ;Branch on Carry Clear - branch when the carry flag is cleared/set.
;The carry flag is set when an addition produced a carry and when
;a subtraction produced a borrow and cleared if an addition/subtraction
;does not produce a carry/borrow. The carry flag also holds bits
;after shifts and rotates.

View file

@ -0,0 +1,6 @@
LDA #200
CMP Variable
BEQ #3 ;if equal, skip ahead 3 bytes...
CLC ;if unequal, continue executing instructions
ADC #1
STA OtherVariable ; ...to here.

View file

@ -0,0 +1,4 @@
LDX #100
Loop: ...do something
DEX
BNE Loop

View file

@ -0,0 +1 @@
if(i<0) i=0; else i=42

View file

@ -0,0 +1 @@
i=(i<0? 0: 42)

View file

@ -0,0 +1,10 @@
type Restricted is range 1..10;
My_Var : Restricted;
if My_Var = 5 then
-- do something
elsif My_Var > 5 then
-- do something
else
-- do something
end if;

View file

@ -0,0 +1,13 @@
type Days is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
Today : Days;
case Today is
when Saturday | Sunday =>
null;
when Monday =>
Compute_Starting_Balance;
when Friday =>
Compute_Ending_Balance;
when others =>
Accumulate_Sales;
end case;

View file

@ -0,0 +1,9 @@
case Today is
when Monday =>
Compute_Starting_Balance;
when Friday =>
Compute_Ending_Balance;
when Tuesday .. Thursday =>
Accumulate_Sales;
-- ignore Saturday and Sunday
end case;

View file

@ -0,0 +1,10 @@
case Today is
when Saturday | Sunday =>
null; -- don't do anything, if Today is Saturday or Sunday
when Monday =>
Compute_Starting_Balance;
when Friday =>
Compute_Ending_Balance;
when Tuesday .. Thursday =>
Accumulate_Sales;
end case;

View file

@ -0,0 +1,7 @@
select
accept first_entry;
-- do something
or accept second_entry;
-- do something
or terminate;
end select;

View file

@ -0,0 +1,5 @@
select
My_Task.Start;
or
delay Timeout_Period;
end select;

View file

@ -0,0 +1 @@
var x = loggedin ? sessionid : -1

View file

@ -0,0 +1,7 @@
if (value > 40) {
println ("OK")
} elif (value < 20) {
println ("FAILED")
} else {
println ("RETRY")
}

View file

@ -0,0 +1,25 @@
switch (arg) {
case "-d":
case "--debug":
debug = true
break
case "-f":
force = true
break
default:
throw "Unknown option " + arg
}
switch (value) {
case > 40:
println ("OK")
break
case < 20:
println ("FAILED")
break
case in 50..59:
println ("WIERD")
// fall through
default:
println ("RETRY")
}

View file

@ -0,0 +1,9 @@
if (c1) {
// first condition is true...
} elif (c2) {
// second condition is true...
} elif (c3) {
// third condition is true...
} else {
// none was true...
}

View file

@ -0,0 +1,5 @@
if: condition then: {
// condition is true...
} else: {
// condition is false...
}

View file

@ -0,0 +1 @@
condition.ifTrue: { /* condition is true... */ } ifFalse: { /* condition is false... */ }

View file

@ -0,0 +1,7 @@
IF condition
-> if condition is true...
ELSEIF condition2
-> else if condition2 is true...
ELSE
-> if all other conditions are not true...
ENDIF

View file

@ -0,0 +1 @@
IF condition THEN statement

View file

@ -0,0 +1,2 @@
DEF c
c := IF condition THEN 78 ELSE 19

View file

@ -0,0 +1,8 @@
SELECT var
CASE n1
-> code
CASE n2
-> code
DEFAULT
-> no one of the previous case...
ENDSELECT

View file

@ -0,0 +1,10 @@
SELECT max_possible_value OF var
CASE n1
-> code
CASE n2 TO n3, n4
-> more
CASE n5 TO n6, n7 TO n8
-> more...
DEFAULT
-> none of previous ones
ENDSELECT

View file

@ -0,0 +1,10 @@
if myVar is "ok" then return true
set i to 0
if i is 0 then
return "zero"
else if i mod 2 is 0 then
return "even"
else
return "odd"
end if

View file

@ -0,0 +1,19 @@
; if
x = 1
If x
MsgBox, x is %x%
Else If x > 1
MsgBox, x is %x%
Else
MsgBox, x is %x%
; ternary if
x = 2
y = 1
var := x > y ? 2 : 3
MsgBox, % var
; while
While (A_Index < 3) {
MsgBox, %A_Index% is less than 3
}

View file

@ -0,0 +1,7 @@
10 LET A%=1: REM A HAS A VALUE OF TRUE
20 IF A% THEN PRINT "A IS TRUE"
30 WE CAN OF COURSE USE EXPRESSIONS
40 IF A%<>0 THEN PRINT "A IS TRUE"
50 IF NOT(A%) THEN PRINT "A IS FALSE"
60 REM SOME VERSIONS OF BASIC PROVIDE AN ELSE KEYWORD
70 IF A% THEN PRINT "A IS TRUE" ELSE PRINT "A IS FALSE"

View file

@ -0,0 +1,2 @@
IF x = 0 THEN doSomething
IF x < 0 THEN doSomething ELSE doOtherThing

View file

@ -0,0 +1,7 @@
IF x > 0 AND x < 10 THEN
'do stuff
ELSE IF x = 0 THEN
'do other stuff
ELSE
'do more stuff
END IF

View file

@ -0,0 +1,5 @@
IF aNumber THEN
'the number is not 0
ELSE
'the number is 0
END IF

View file

@ -0,0 +1,12 @@
SELECT CASE expression
CASE 1
'do stuff
CASE 2, 3
'do other stuff
CASE 3.1 TO 9.9
'do this
CASE IS >= 10
'do that
CASE ELSE
'default case
END SELECT

View file

@ -0,0 +1,9 @@
10 INPUT "Enter 1,2 or 3: ";v
20 GOTO v * 100
99 STOP
100 PRINT "Apple"
110 STOP
200 PRINT "Banana"
210 STOP
300 PRINT "Cherry"
310 STOP

View file

@ -0,0 +1,12 @@
10 REM while loop
20 L=0
30 WHILE L<5
40 PRINT L
50 L=L+1
60 WEND
70 REM repeat loop
80 L=1
90 REPEAT
100 PRINT L
110 L=L+1
120 UNTIL L>5

View file

@ -0,0 +1,26 @@
REM Single-line IF ... THEN ... ELSE (ELSE clause is optional):
IF condition% THEN statements ELSE statements
REM Multi-line IF ... ENDIF (ELSE clause is optional):
IF condition% THEN
statements
ELSE
statements
ENDIF
REM CASE ... ENDCASE (OTHERWISE clause is optional):
CASE expression OF
WHEN value1: statements
WHEN value2: statements
...
OTHERWISE: statements
ENDCASE
REM ON ... GOTO (ELSE clause is optional):
ON expression% GOTO dest1, dest2 ... ELSE statements
REM ON ...GOSUB (ELSE clause is optional):
ON expression% GOSUB dest1, dest2 ... ELSE statements
REM ON ... PROC (ELSE clause is optional):
ON expression% PROCone, PROCtwo ... ELSE statements

View file

@ -0,0 +1,3 @@
v > "X",@ non-zero
> & |
> "0",@ zero

View file

@ -0,0 +1,2 @@
& #v_ "0",@ zero
> "X",@ non-zero

View file

@ -0,0 +1,6 @@
if (i == 0)
return "zero";
elif (i % 2)
return "odd";
else
return "even";

View file

@ -0,0 +1,5 @@
2+2:5
& put$"Strange, must check that Bracmat interpreter."
& 0
| put$"That's what I thought."
& Right

View file

@ -0,0 +1,8 @@
2+2
: ( (<3|>5)
& put$"Not quite, must check that Bracmat interpreter."
| (3|5)
& put$"Not far off, but must check that Bracmat interpreter some day."
| ?
& put$"That's what I thought."
)

View file

@ -0,0 +1 @@
[.]

View file

@ -0,0 +1 @@
+[.]

View file

@ -0,0 +1 @@
+[.-]

View file

@ -0,0 +1,2 @@
blsq ) 9 2.%{"Odd""Even"}ch
"Odd"

View file

@ -0,0 +1,4 @@
blsq ) 9^^2.%{+.}if
10
blsq ) 10^^2.%{+.}if
10

View file

@ -0,0 +1,4 @@
blsq ) 10^^2.%{-.}\/{+.}\/ie
11
blsq ) 9^^2.%{-.}\/{+.}\/ie
8

View file

@ -0,0 +1,4 @@
blsq ) {"Hate tomatos" "Like Bananas" "Hate Apples"}{"Tomato" "Banana" "Apple"}"Banana"Fi!!
"Like Bananas"
blsq ) {"Hate tomatos" "Like Bananas" "Hate Apples"}{"Tomato" "Banana" "Apple"}"Apple"Fi!!
"Hate Apples"

View file

@ -0,0 +1,17 @@
template<bool Condition, typename ThenType, typename Elsetype> struct ifthenelse;
template<typename ThenType, typename ElseType> struct ifthenelse<true, ThenType, ElseType>
{
typedef ThenType type;
};
template<typename ThenType, typename ElseType> struct ifthenelse<false, ThenType, ElseType>
{
typedef ElseType type;
};
// example usage: select type based on size
ifthenelse<INT_MAX == 32767, // 16 bit int?
long int, // in that case, we'll need a long int
int> // otherwise an int will do
::type myvar; // define variable myvar with that type

View file

@ -0,0 +1,10 @@
set(num 5)
if(num GREATER 100)
message("${num} is very large!")
elseif(num GREATER 10)
message("${num} is large.")
else()
message("${num} is small.")
message("We might want a bigger number.")
endif()

View file

@ -0,0 +1,19 @@
if condition-1
imperative-statement-1
else
imperative-statement-2
end-if
if condition-1
if condition-a
imperative-statement-1a
else
imperative-statement-1
end-if
else
if condition-a
imperative-statement-2a
else
imperative-statement-2
end-if
end-if

View file

@ -0,0 +1,33 @@
evaluate identifier-1
when 'good'
good-imperative-statement
when 'bad'
bad-imperative-statement
when 'ugly'
when 'awful'
ugly-or-awful-imperative-statement
when other
default-imperative-statement
end-evaluate
evaluate true
when condition-1
condition-1-imperative-statement
when condition-2
condition-2-imperative-statement
when condition-3
condition-3-imperative-statement
when other
default-condition-imperative-statement
end-evaluate
evaluate identifier-1 also identifier-2
when 10 also 20
one-is-10-and-two-is-20-imperative-statement
when 11 also 30
one-is-11-and-two-is-30-imperative-statement
when 20 also any
one-is-20-and-two-is-anything-imperative-statement
when other
default-imperative-statement
end-evaluate

View file

@ -0,0 +1 @@
bool2int b = if b 1 0

View file

@ -0,0 +1,3 @@
case 6 * 7 of
42 -> "Correct"
_ -> "Wrong" // default, matches anything

View file

@ -0,0 +1,2 @@
answer 42 = True
answer _ = False

View file

@ -0,0 +1,8 @@
answer x
| x == 42 = True
| otherwise = False
case 6 * 7 of
n | n < 0 -> "Not even close"
42 -> "Correct"
// no default, could result in a run-time error

View file

@ -0,0 +1,5 @@
(if (= 1 1) :yes :no) ; returns :yes
(if (= 1 2) :yes :no) ; returns :no
(if (= 1 2) :yes) ; returns nil

View file

@ -0,0 +1,4 @@
(when x
(print "hello")
(println " world")
5) ; when x is logical true, prints "hello world" and returns 5; otherwise does nothing, returns nil

View file

@ -0,0 +1,6 @@
(cond
(= 1 2) :no) ; returns nil
(cond
(= 1 2) :no
(= 1 1) :yes) ; returns :yes

View file

@ -0,0 +1,3 @@
(cond
(= 1 2) :no
:else :yes) ; returns :yes

View file

@ -0,0 +1,5 @@
(condp < 3
4 :a ; cond equivalent would be (< 4 3) :a
3 :b
2 :c
1 :d) ; returns :c

View file

@ -0,0 +1,4 @@
(condp < 3
4 :a
3 :b
:no-match) ; returns :no-match

View file

@ -0,0 +1,4 @@
(case 2
0 (println "0")
1 (println "1")
2 (println "2")) ; prints 2.

View file

@ -0,0 +1,6 @@
if n == 1
console.log "one"
else if n == 2
console.log "two"
else
console.log "other"

View file

@ -0,0 +1,9 @@
n = 1
switch n
when 1
console.log "one"
when 2, 3
console.log "two or three"
else
console.log "other"

View file

@ -0,0 +1,7 @@
s = if condition then "yup" else "nope"
# alternate form
s = \
if condition
then "yup"
else "nope"

View file

@ -0,0 +1,7 @@
<cfif x eq 3>
do something
<cfelseif x eq 4>
do something else
<cfelse>
do something else
</cfif>

View file

@ -0,0 +1,11 @@
<cfswitch expression="#x#">
<cfcase value="1">
do something
</cfcase>
<cfcase value="2">
do something
</cfcase>
<cfdefaultcase>
do something
</cfdefaultcase>
</cfswitch>

View file

@ -0,0 +1,3 @@
(if (= val 42)
"That is the answer to life, the universe and everything"
"Try again") ; the else clause here is optional

View file

@ -0,0 +1,4 @@
(cond ((= val 1) (print "no"))
((and (> val 3) (< val 6)) (print "yes"))
((> val 99) (print "too far"))
(T (print "no way, man!")))

View file

@ -0,0 +1,17 @@
if (condition)
{
// Some Task
}
if (condition)
{
// Some Task
}
else if (condition2)
{
// Some Task
}
else
{
// Some Task
}

View file

@ -0,0 +1,2 @@
// if condition is true var will be set to 1, else false.
int var = condition ? 1 : 2;

View file

@ -0,0 +1 @@
const i = 5; static if (i == 7) { ... } else { ... }

View file

@ -0,0 +1,9 @@
auto i = 5;
// is(T: U) tests if T is implicitly castable to U.
// typeof(var) is the type of the variable.
// also: is(T==U) checks if T is U.
static if (is(typeof(i) : int)) {
...
} else {
...
}

View file

@ -0,0 +1,8 @@
a = 3
if( a == 1 ){
io.writeln( 'a == 1' )
}elif( a== 3 ){
io.writeln( 'a == 3' )
}else{
io.writeln( 'a is neither 1 nor 3' )
}

View file

@ -0,0 +1,7 @@
a = 3
switch( a ){
case 0: io.writeln( 'case 0' )
case 1, 2: io.writeln( 'case 1,2' )
case 3 ... 5: io.writeln( 'case 3...5' )
default: io.writeln( 'default' )
}

View file

@ -0,0 +1,6 @@
if a:
pass
elseif b:
pass
else: # c, maybe?
pass

View file

@ -0,0 +1,7 @@
if (input.Field == "Hello World") {
sVar = "good";
} else if (input.Field == "Bye World") {
sVar = "bad";
} else {
sVar = "neutral";
}

View file

@ -0,0 +1,7 @@
if (okay) {
println("okay")
} else if (!okay) {
println("not okay")
} else {
println("not my day")
}

View file

@ -0,0 +1 @@
println(okay.pick("okay", "not okay"))

View file

@ -0,0 +1,5 @@
okay.pick(fn {
println("okay")
}, fn {
println("not okay")
})()

View file

@ -0,0 +1,7 @@
def expression := ["+", [1, 2]]
def value := switch (expression) {
match [`+`, [a, b]] { a + b }
match [`*`, [a, b]] { a * b }
match [op, _] { throw(`unknown operator: $op`) }
}

View file

@ -0,0 +1,60 @@
show_if_with_parenthesis = fn (Num) {
if (Num == 1) {
io.format("is one~n")
}
else if (Num === 2) {
io.format("is two~n")
}
else {
io.format("not one not two~n")
}
}
show_if_without_parenthesis = fn (Num) {
if Num == 1 {
io.format("is one~n")
}
else if Num === 2 {
io.format("is two~n")
}
else {
io.format("not one not two~n")
}
}
show_switch_with_parenthesis = fn (Num) {
switch (Num) {
case (1) {
io.format("one!~n")
}
case (2) {
io.format("two!~n")
}
else {
io.format("else~n")
}
}
}
show_switch_without_parenthesis = fn (Num) {
switch (Num) {
case 1 {
io.format("one!~n")
}
case 2 {
io.format("two!~n")
}
else {
io.format("else~n")
}
}
}
@public
run = fn () {
show_if_with_parenthesis(random.uniform(3))
show_if_without_parenthesis(random.uniform(3))
show_switch_with_parenthesis(random.uniform(3))
show_switch_without_parenthesis(random.uniform(3))
}

View file

@ -0,0 +1 @@
if x < 0 then 0 else x

View file

@ -0,0 +1,2 @@
getX x | x < 0 = 0
| else = x

View file

@ -0,0 +1,2 @@
force (x::xs) = x :: force xs
force [] = []

View file

@ -0,0 +1,3 @@
force lst = match lst with
x::xs = x :: force xs
[] = []

View file

@ -0,0 +1,5 @@
case X of
{N,M} when N > M -> M;
{N,M} when N < M -> N;
_ -> equal
end.

View file

@ -0,0 +1,6 @@
{N,M} = X,
if
N > M -> M;
N < M -> N;
true -> equal
end.

View file

@ -0,0 +1,3 @@
test({N,M}) when N > M -> M;
test({N,M}) when N < M -> N;
test(_) -> equal.

View file

@ -0,0 +1,2 @@
( condition ) IF ( true statements ) THEN
( condition ) IF ( true statements ) ELSE ( false statements ) THEN

View file

@ -0,0 +1 @@
10 < IF ." Less than 10" ELSE ." Greater than or equal to 10" THEN

View file

@ -0,0 +1,5 @@
( n -- ) CASE
( integer ) OF ( statements ) ENDOF
( integer ) OF ( statements ) ENDOF
( default instructions )
ENDCASE

View file

@ -0,0 +1,6 @@
: test-case ( n -- )
CASE
0 OF ." Zero!" ENDOF
1 OF ." One!" ENDOF
." Some other number!"
ENDCASE ;

View file

@ -0,0 +1,19 @@
: switch
CREATE ( default-xt [count-xts] count -- ) DUP , 0 DO , LOOP ,
DOES> ( u -- ) TUCK @ MIN 1+ CELLS + @ EXECUTE ;
:NONAME ." Out of range!" ;
:NONAME ." nine" ;
:NONAME ." eight" ;
:NONAME ." seven" ;
:NONAME ." six" ;
:NONAME ." five" ;
:NONAME ." four" ;
:NONAME ." three" ;
:NONAME ." two" ;
:NONAME ." one" ;
:NONAME ." zero" ;
10 switch digit
8 digit \ eight
34 digit \ Out of range!

View file

@ -0,0 +1,7 @@
if ( a .gt. 20.0 ) then
q = q + a**2
else if ( a .ge. 0.0 ) then
q = q + 2*a**3
else
q = q - a
end if

View file

@ -0,0 +1,8 @@
select case (i)
case (21:) ! matches all integers greater than 20
q = q + i**2
case (0:20) ! matches all integers between 0 and 20 (inclusive)
q = q + 2*i**3
case default ! matches all other integers (negative in this particular case)
q = q - I
end select

View file

@ -0,0 +1,31 @@
! diffusion grid time step
where (edge_type(1:n,1:m) == center)
anew(1:n,1:m) = (a(1:n,1:m) + a(0:n-1,1:m) + a(2:n+1,1:m) + a(1:n,0:m-1) + a(1:n,2:m+1)) / 5
elsewhere (edge_type(1:n,1:m) == left)
anew(1:n,1:m) = (a(1:n,1:m) + 2*a(2:n+1,1:m) + a(1:n,0:m-1) + a(1:n,2:m+1)) / 5
elsewhere (edge_type(1:n,1:m) == right)
anew(1:n,1:m) = (a(1:n,1:m) + 2*a(0:n-1,1:m) + a(1:n,0:m-1) + a(1:n,2:m+1)) / 5
elsewhere (edge_type(1:n,1:m) == top)
anew(1:n,1:m) = (a(1:n,1:m) + a(0:n-1,1:m) + a(2:n+1,1:m) + 2*a(1:n,2:m+1)) / 5
elsewhere (edge_type(1:n,1:m) == bottom)
anew(1:n,1:m) = (a(1:n,1:m) + a(0:n-1,1:m) + a(2:n+1,1:m) + 2*a(1:n,0:m-1)) / 5
elsewhere (edge_type(1:n,1:m) == left_top)
anew(1:n,1:m) = (a(1:n,1:m) + 2*a(2:n+1,1:m) + 2*a(1:n,2:m+1)) / 5
elsewhere (edge_type(1:n,1:m) == right_top)
anew(1:n,1:m) = (a(1:n,1:m) + 2*a(0:n-1,1:m) + 2*a(1:n,2:m+1)) / 5
elsewhere (edge_type(1:n,1:m) == left_bottom)
anew(1:n,1:m) = (a(1:n,1:m) + 2*a(2:n+1,1:m) + 2*a(1:n,0:m-1)) / 5
elsewhere (edge_type(1:n,1:m) == right_bottom)
anew(1:n,1:m) = (a(1:n,1:m) + 2*a(0:n-1,1:m) + 2*a(1:n,0:m-1)) / 5
elsewhere ! sink/source, does not change
anew(1:n,1:m) = a(1:n,1:m)
end where

View file

@ -0,0 +1,3 @@
if booleanExpression {
statements
}

View file

@ -0,0 +1,7 @@
treadmill: for {
switch {
case true:
break treadmill
}
}
fmt.Println("I'm off!")

View file

@ -0,0 +1,6 @@
if booleanExpression {
statements
} else {
other
statements
}

Some files were not shown because too many files have changed in this diff Show more