on writing a while loop for rolling two dice

Avi Gross avigross at verizon.net
Mon Sep 6 19:38:17 EDT 2021


I actually like it if a language lets you spell out your intention, although adding many keywords is not a plus.

So, yes something like:

	loop
		...
	end loop;

Is appealing as it makes clear the decision on when to exit the loop must be within the loop (or till something kills ...)

 In languages like C/C++ there are people who make up macros like:

#define INDEFINITELY_LOOP while (true)

Or something like that and then allow the preprocessor to replace INDEFINITELY_LOOP with valid C code.

So, how to do something like that in python, is a challenge left to the user 😉

Bottom line I find is it is a great idea to write comments as even if you are the one reviewing the code much later, you may struggle to figure out what you did and why.

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On Behalf Of Dennis Lee Bieber
Sent: Sunday, September 5, 2021 12:50 PM
To: python-list at python.org
Subject: Re: on writing a while loop for rolling two dice

On Sat, 4 Sep 2021 12:27:55 -0500, "Michael F. Stemper"
<michael.stemper at gmail.com> declaimed the following:

>
>Kernighan and Ritchie agree(d) with you. Per _The C Programming
>Language__:
>   Experience shows that do-while is much less used that while
>   and for.
>

	And just for confusion, consider languages with "repeat / until"...

	"do / while" repeats so long as the condition evaluates to "true"; "repeat / until" /exits/ when the condition evaluates to "true".

	Then... there is Ada...

	While one is most likely to encounter constructs:

	for ix in start..end loop
		...
	end loop;

and

	while condition loop
		...
	end loop;

the core construct is just a bare

	loop
		...
	end loop;

which, with the "exit when condition" statement allows low-level emulation of any loop... (same as Python "if condition: break"

	loop	-- "while" loop
		exit when not condition;
		...
	end loop;

	loop	-- "repeat / until"
		...
		exit when condition;
	end loop;

	loop	-- split
		...
		exit when condition;
		...
	end loop;

{I'm not going to do the "for" loop, but one can easily manage initialization/increment/test statements}.


	To really get your mind blown, look at loops in REXX...

	do while condition
		...
	end

	do until condition
		/* note that the termination parameter is listed at top, */
		/* but takes effect at the bottom, so always one pass */
		...
	end

	do forever
		...
		if condition then
			leave
		...
	end

	do idx = start to end /* optional: by increment	*/
		...
	end

	do idx = 1 by increment for repetitions
		...
	end

AND worse! You can combine them...

	do idx = start for repetitions while condition1 until condition2
		...
	end

{I need to check if both while and until can be in the same statement}


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/

--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list