home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (LoopBreakTest.groovy)

This example Groovy source code file (LoopBreakTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

groovytestcase, groovytestcase, loopbreaktest, loopbreaktest, should, should

The Groovy LoopBreakTest.groovy source code

package groovy

class LoopBreakTest extends GroovyTestCase {

    void testWhileWithBreak() {
        def x = 0
        while (true) {
            if (x == 5) {
                break
            }
            ++x

            assert x < 10 , "Should never get here"
        }
        
        println "worked: while completed with value ${x}"
    }
    
    /**

      We currently do not support do ... while in the JSR syntax

    void testDoWhileWithBreak() {
        def x = 0
        do {
            //println "in do-while loop and x = ${x}"

            if (x == 5) {
                break
            }
            ++x

            assert x < 10 , "Should never get here"
        }
        while (true)

        println "worked: do-while completed with value ${x}"
    }
    */

    void testForWithBreak() {
        def returnValue
        for (x in 0..20) {
            if (x == 5) {
                returnValue = x
                break
            }
            assert x < 10 , "Should never get here"
        }
        
        println "worked: for loop completed with value ${returnValue}"
    }
 }

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy LoopBreakTest.groovy source code file:

new blog posts

 

Copyright 1998-2014 Alvin Alexander, alvinalexander.com
All Rights Reserved.