Wiki

root/cobra/trunk/Tests/100-basics/110-while.cobra

Revision 2636, 0.9 KB (checked in by Charles.Esterbrook, 7 weeks ago)

JVM back-end progress.
credit:hopscc

  • Property svn:eol-style set to native
Line 
1#.require. clr
2@platform clr
3namespace Test
4
5    class Test
6
7        def main
8
9            # Note: You wouldn't actually use while loops for counting,
10            # but counting makes a good basic test.
11
12            i as int = 0
13            while i < 100
14                i += 1
15            assert i == 100
16
17            i = 0
18            while i > 0
19                i = -1
20            assert i == 0
21
22            i = 0
23            post while i > 0
24                i = -1
25            assert i == -1
26
27            i = 100
28            while i
29                i -= 1
30            assert i == 0
31
32            # 2006-12-16: there was a bug where post while generates both a while loop and a do-while loop
33            count = 0
34            i = 10
35            post while i > 0
36                i -= 1
37                count += 1
38            assert count == 10
39
40            random = Random()
41            a = 1
42            while a <= 3
43                post while .check(a, b)  # using local var `b` which is first assigned below
44                    b = random.next(3) + 1
45                a += 1
46   
47        def check(a as int, b as int) as bool
48            assert b <> 0  # verifies that the while condition is not checked immediately
49            return a <> b
Note: See TracBrowser for help on using the browser.