| Statements |

## Lazy statements

The primitives and composite building blocks presented before are sufficiently powerful to construct quite elaborate structures and facilities. We have presented lazy-functions and lazy-operators. How about lazy-statements? First, an appetizer:

Print all odd-numbered contents of an STL container using std::for_each (sample4.cpp):

`

        for_each(c.begin(), c.end(),
            if_(arg1 % 2 == 1)
            [
                cout << arg1 << ' '
            ]
        );

`

Huh? Is that valid C++? Read on...

Yes, it is valid C++. The sample code above is as close as you can get to the syntax of C++. This stylized C++ syntax differs from actual C++ code. First, the if has a trailing underscore. Second, the block uses square brackets instead of the familiar curly braces {}.

Here are more examples with annotations. The code almost speaks for itself.

1) block statement:

`

        statement,
        statement,
        ....
        statement

`

Basically, these are comma separated statements. Take note that unlike the C/C++ semicolon, the comma is a separator put *in-between* statements. This is like Pascal's semicolon separator, rather than C/C++'s semicolon terminator. For example:

`

        statement,
        statement,
        statement,     //  ERROR!

`

Is an error. The last statement should not have a comma. Block statements can be grouped using the parentheses. Again, the last statement in a group should not have a trailing comma.

`

        statement,
        statement,
        (
            statement,
            statement
        ),
        statement

`

Outside the square brackets, block statements should be grouped. For example:

`

        for_each(c.begin(), c.end(),
            (
                do_this(arg1),
                do_that(arg1)
            )
        );

`

2) if_ statement:

We have seen the if_ statement. The syntax is:

`

        if_(conditional_expression)
        [
            sequenced_statements
        ]

`

3) if_ else_ statement:

The syntax is

`

        if_(conditional_expression)
        [
            sequenced_statements
        ]
        .else_
        [
            sequenced_statements
        ]

`

Take note that else has a prefix dot and a trailing underscore: .else_

Example: This code prints out all the elements and appends " > 5", " == 5" or " < 5" depending on the element's actual value:

`

        for_each(c.begin(), c.end(),
            if_(arg1 > 5)
            [
                cout << arg1 << " > 5\n"
            ]
            .else_
            [
                if_(arg1 == 5)
                [
                    cout << arg1 << " == 5\n"
                ]
                .else_
                [
                    cout << arg1 << " < 5\n"
                ]
            ]
        );

`

Notice how the if_ else_ statement is nested.

4) while_ statement:

The syntax is:

`

        while_(conditional_expression)
        [
            sequenced_statements
        ]

`

Example: This code decrements each element until it reaches zero and prints out the number at each step. A newline terminates the printout of each value.

`

        for_each(c.begin(), c.end(),
            (
                while_(arg1--)
                [
                    cout << arg1 << ", "
                ],
                cout << val("\n")
            )
        );

`

5) do_ while_ statement:

The syntax is:

`

        do_
        [
            sequenced_statements
        ]
        .while_(conditional_expression)

`

Again, take note that while has a prefix dot and a trailing underscore: .while_

Example: This code is almost the same as the previous example above with a slight twist in logic.

`

        for_each(c.begin(), c.end(),
            (
                do_
                [
                    cout << arg1 << ", "
                ]
                .while_(arg1--),
                cout << val("\n")
            )
        );

`

6) for_ statement:

The syntax is:

`

        for_(init_statement, conditional_expression, step_statement)
        [
            sequenced_statements
        ]

`

It is again almost similar to C++ for statement. Take note that the init_statement, conditional_expression and step_statement are separated by the comma instead of the semi- colon and each must be present (i.e. for_(,,) is invalid).

Example: This code prints each element N times where N is the element's value. A newline terminates the printout of each value.

`

        int iii;
        for_each(c.begin(), c.end(),
            (
                for_(var(iii) = 0, var(iii) < arg1, ++var(iii))
                [
                    cout << arg1 << ", "
                ],
                cout << val("\n")
            )
        );

`

As before, all these are lazily evaluated. The result of such statements are in fact composites that are passed on to STL's for_each function. In the viewpoint of for_each, what was passed is just a functor, no more, no less.

Unlike lazy functions and lazy operators, lazy statements always return void.

Copyright © 2001-2002 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)