Conditional structures: Difference between revisions
Content added Content deleted
(Added XLISP) |
|||
Line 3,548: | Line 3,548: | ||
===Other short-circuiting operators=== |
===Other short-circuiting operators=== |
||
<tt>and</tt>, <tt>or</tt>, <tt>&&</tt>, <tt>||</tt> and <tt>//</tt> work as in Perl 5. |
<tt>and</tt>, <tt>or</tt>, <tt>&&</tt>, <tt>||</tt> and <tt>//</tt> work as in Perl 5. |
||
=={{header|Phix}}== |
|||
===if=== |
|||
<lang Phix>if name="Pete" then |
|||
-- do something |
|||
elsif age>50 then |
|||
-- do something |
|||
elsif age<20 then |
|||
-- do something |
|||
else |
|||
-- do something |
|||
end if</lang> |
|||
There is no limit to the number of elsif clauses, including 0. The else clause is also optional, |
|||
whereas the end if is always mandatory (which avoids any dangling else problems). All conditional |
|||
expressions are short-circuited. |
|||
===iff=== |
|||
<lang Phix>var = iff(flag?true_expr:false_expr)</lang> |
|||
In an iff() expression, only one of true_expr or false_expr will be evaluated, not both. |
|||
Phix has some rudimentary support of preprocessor idef statments, but their use is discouraged |
|||
(since they are quite unnecessary in Phix, I might add). |
|||
===switch=== |
|||
<lang Phix>switch v [with fallthrough] do |
|||
case 1,2: |
|||
-- do something |
|||
case 3 then |
|||
-- do something |
|||
fallthrough |
|||
case 4: |
|||
-- do something |
|||
break |
|||
default: |
|||
-- do something |
|||
end switch</lang> |
|||
By default there is no fallthough on switch clauses, however you can add a directive, and you can |
|||
override individual clauses with explicit fallthough or break statements. There is no need to have |
|||
break between cases when it is the default. You can use either : or then on case clauses. The else |
|||
keyword can be used instead of "default", and behaves identically. It can also be placed anywhere, |
|||
even first, or completely omitted. |
|||
The compiler will automatically construct either a jump table or daisy-chained cmp/jmp chains from |
|||
either if-constructs or switch-statements, leaving the programmer free to choose whichever shows |
|||
the intent clearest, without having to worry about performance implications. |
|||
===ilASM=== |
|||
Inline assembly, in the form of #ilASM{} constructs, should you be brave or desperate enough to |
|||
use them, also have some conditional guards for cross-platform support |
|||
<lang Phix>#ilASM{ |
|||
[32] |
|||
mov eax,[var] |
|||
[64] |
|||
mov rax,[var] |
|||
[PE32] |
|||
push eax -- uExitCode |
|||
call "kernel32.dll","ExitProcess" |
|||
[PE64] |
|||
mov rcx,rax -- uExitCode |
|||
call "kernel32.dll","ExitProcess" |
|||
[ELF32] |
|||
mov ebx,eax -- error_code (p1) |
|||
mov eax,1 -- sys_exit(ebx=int error_code) |
|||
int 0x80 |
|||
-- xor ebx,ebx -- (common requirement after int 0x80) |
|||
[ELF64] |
|||
mov rdi,rax -- error_code (p1) |
|||
mov rax,60 -- sys_exit(rdi=int error_code) |
|||
syscall |
|||
[] |
|||
}</lang> |
|||
=={{header|PHL}}== |
=={{header|PHL}}== |