Wiki

Changes between Initial Version and Version 1 of ConditionalExpr

Show
Ignore:
Timestamp:
12/22/09 13:38:18 (15 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ConditionalExpr

    v1 v1  
     1= Conditional Expressions = 
     2 
     3A conditional expression is an expression that produces a value that can be (conveniently) construed as a boolean 
     4true or false value. 
     5these are used directly in '''if''' and '''while''' flow control statements and the '''assert''' statement 
     6 
     7Any of following can be used as conditional expressions and produce a '''boolean true''' value. 
     8   * non false (bool Type - expression or variable ) 
     9   * numeric not 0 
     10   * non zero char 
     11   * not nil  
     12        * blank strings and empty collections are true (non nil) - must check .length for strings and .count for collections for checking non empty. 
     13 
     14nillable objects (e.g nillable boolean, nillable int) test true if both non nil and non nil object is 'true' (non false or non zero) 
     15 
     16== Examples == 
     17 
     18{{{ 
     19a = 100 
     20gt20 = a > 20 
     21 
     22if a          # numeric non zero 
     23    print 'a non zero' 
     24if a > 10     # boolean condition 
     25    print 'greater than 10' 
     26assert gt20   # boolean variable - succeeds true 
     27assert a > 20 # boolean condition same as above 
     28 
     29ch = c'x' 
     30if ch 
     31    print 'ch non zero' 
     32assert ch 
     33ch=c'\0' 
     34if not ch # negated condition 
     35    print 'ch is zero' 
     36 
     37s as String?  # nillable string unset value  
     38if s 
     39    print 's not nil' 
     40    if s.length 
     41        print 's not empty' 
     42else 
     43    print 's is nil' 
     44 
     45l = []         # empty list 
     46assert l       # succeeds not nil 
     47assert l.count # fails empty list 
     48 
     49ln as List?<of dynamic>  # nillable list - unset 
     50assert ln  # fails: ln not set so nil 
     51 
     52nb = a>10 to ? # bool expr cast to nillable bool 
     53assert nb      # succeeds non nil and true 
     54nb = a<5 
     55assert nb      # fails a is >5 so variable is false 
     56nb = nil 
     57assert nb      # also fails variable is nil 
     58  
     59}}} 
     60