Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"do while": is it a good solution?

159 views
Skip to first unread message

paskali

unread,
Jun 11, 2013, 4:07:13 PM6/11/13
to
Hi, as well as we know, in tcl there is not the c loop "do while":

int main(int argc, char *argv[]) {

int c = 0;

do {
c++;
printf("\n%d", c);
} while(c <= 10);

exit();

}

Can it to be replace in tcl with for?

proc main {} {

set c 0;

for {} {$c <= 10} {
incr c 1;
puts "$c";
} {}

}

main;

It is not a "beautifull" solution but i think is much working.

--
http://www.webuse.net/pm.php?u=2970

Posted using www.webuse.net

Robert Heller

unread,
Jun 11, 2013, 8:00:40 PM6/11/13
to
At Tue, 11 Jun 2013 20:07:13 GMT "paskali" <2970i...@webuse.net> wrote:

>
> Hi, as well as we know, in tcl there is not the c loop "do while":
>
> int main(int argc, char *argv[]) {
>
> int c = 0;
>
> do {
> c++;
> printf("\n%d", c);
> } while(c <= 10);
>
> exit();
>
> }
>
> Can it to be replace in tcl with for?
>
> proc main {} {
>
> set c 0;
>
> for {} {$c <= 10} {
> incr c 1;
> puts "$c";
> } {}
>
> }
>
> main;
>
> It is not a "beautifull" solution but i think is much working.

It is in fact possible define do ... while in Tcl itself:

## Off the top of my head:

proc do {body "while" condition} {
while {yes} {
uplevel 1 $body
if {![uplevel 1 {expr $condition}]} {break}
}
}

This defines a procedure named do that takes three arguments, the first is the
loop body, the second is the constant string while, and the third is the test
condition:

set c 0
do {
incr c
puts "[format %d $c]"
} while {$c <= 10}

Tcl is extendable!





>

--
Robert Heller -- 978-544-6933 / hel...@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments



gustafn

unread,
Jun 12, 2013, 2:45:44 AM6/12/13
to
the straight-forward for-loop based approach is:

for {set c 0} {$c <= 10} {} {

Arjen Markus

unread,
Jun 12, 2013, 3:50:58 AM6/12/13
to
Op dinsdag 11 juni 2013 22:07:13 UTC+2 schreef paskali het volgende:
> Hi, as well as we know, in tcl there is not the c loop "do while":

>
> Can it to be replace in tcl with for?
>

Your do-while loop could be written like this, indeed:

set c 0
for {} {$c <=10} {} {
incr c
puts $c
}

(Note that for wants the loop body as the fourth argument)

As a side note, I find the do-while construction much less useful than this one:

while {1} {
... do some preparation ...

if { ! [some condition] } {
break
}

... do the further work ...
}

Regards,

Arjen

paskali

unread,
Jun 12, 2013, 4:08:25 AM6/12/13
to
Robert Heller <hel...@deepsoft.com> wrote:

> It is in fact possible define do ... while in Tcl itself:
>
> ## Off the top of my head:
>
> proc do {body "while" condition} {
> while {yes} {
> uplevel 1 $body
> if {![uplevel 1 {expr $condition}]} {break}
> }
> }
>
> This defines a procedure named do that takes three arguments, the first
is the
> loop body, the second is the constant string while, and the third is the
test
> condition:
>
> set c 0
> do {
> incr c
> puts "[format %d $c]"
> } while {$c <= 10}
>
> Tcl is extendable!
>
>
>
>
>
> >
>

My solution is ungly but works, your solution is beautifull and works as
well! I prefer the yours, of course.
Thanks!

Ralf Fassel

unread,
Jun 12, 2013, 4:21:06 AM6/12/13
to
* "paskali" <2970i...@webuse.net>
| do {
| c++;
| printf("\n%d", c);
| } while(c <= 10);
--<snip-snip>--
| for {} {$c <= 10} {
| incr c 1;
| puts "$c";
| } {}
>
| It is not a "beautifull" solution but i think is much working.

There is a difference if the check condition is false on entry.
"do-while" will at least run once, whereas the "for"-loop does not run.
So while your your code works for this example, it is not a general
replacement for do-while. See Arjens or Roberts post for alternatives.

R'

Emiliano

unread,
Jun 14, 2013, 4:46:51 PM6/14/13
to
On 11 jun, 17:07, "paskali" <2970inva...@webuse.net> wrote:
> Hi, as well as we know, in tcl there is not the c loop "do while":
>

package require control ;# part of tcllib
set c 0
control::do {
incr c
puts $c
} while {$c <=10}


regards
Emiliano

paskali

unread,
Jun 16, 2013, 4:18:17 PM6/16/13
to
Yes, that is.

--
Pasquale Frega

tcltk-d

unread,
Jun 17, 2013, 3:17:37 AM6/17/13
to
I (used to) think that:
"Do" and/or "While" is a non-beautifull "procedure".
(These are not data-oriented.)
Most beautifull (objective) expression of Tcl may be:

foreach x some-list {
describe something only on x
}

How about?

paskali

unread,
Jun 17, 2013, 5:08:05 AM6/17/13
to
I am from C where foreach there is not.
However, i think, it is a issue of habit.
Of course there is the case in which a solution is better then an other.

Donal K. Fellows

unread,
Jun 17, 2013, 10:07:52 AM6/17/13
to
On 17/06/2013 11:08, paskali wrote:
> I am from C where foreach there is not.
> However, i think, it is a issue of habit.
> Of course there is the case in which a solution is better then an other.

If you're wanting to go over the elements of a list *without* changing
them, [foreach] is wonderful. If you're adding, removing or changing
elements as you go and you want to see those modifications (really
useful in some algorithms!) then you need to use the more primitive
[for] or [while].

Also, don't forget that there's also [dict for] for going over
dictionaries (and [lmap] and [dict map] for remapping in 8.6). These are
all things not found in C; Tcl isn't C. :-)

Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.

paskali

unread,
Jun 17, 2013, 1:30:38 PM6/17/13
to
Thanks for suggests. Of course C is tedious with strings (and not only) as well
as its users on comp.lang.c............

--
Pasquale Frega
Tiny C compiler and Freewrap user

paskali

unread,
Jun 19, 2013, 12:05:30 PM6/19/13
to
paskali <2970i...@webuse.net> wrote:

> Hi, as well as we know, in tcl there is not the c loop "do while":
>
> int main(int argc, char *argv[]) {
>
> int c = 0;
>
> do {
> c++;
> printf("\n%d", c);
> } while(c <= 10);
>
> exit();
>
> }
>
> Can it to be replace in tcl with for?
>
> proc main {} {
>
> set c 0;
>
> for {} {$c <= 10} {
> incr c 1;
> puts "$c";
> } {}
>
> }
>
> main;
>
> It is not a "beautifull" solution but i think is much working.
>

After some try, i see more confortable this form:

while {1} {

....

if {condition true} {
continue;
}

break;
}

It permits a better flessibility on the managment of the loop, when the
condition is true.

--
Pasquale Frega
Tiny C compiler and Freewrap user
I have a Commodore 64, too
0 new messages