|
Post by jarychk on Jul 18, 2022 6:42:41 GMT
(Maybe post belongs in Novice board. Not sure.)
I just began designing a menu-driven program and the main code is all within a SELECT CASE structure. I made a menu program some years ago and as I best recall, a SELECT CASE structure was placed inside a WHILE loop. Currently I found no reason (yet) to surround the bulk of the code inside a WHILE/WEND loop. I wonder when or if such a menu-driven program should be created using a WHILE loop. The development is going well. I include a way for the user to leave the program. I just wonder if menu-driven (non-gui) should or not need to be done inside a WHILE loop.
The main menu to the user would look something this way:
and then user makes choice by the number, and program goes to the part of program for the choice.
|
|
|
Post by plus on Jul 19, 2022 2:29:57 GMT
Yes you could very well use Select Case of Menu Choice to decide which part of program to run next. Right, no While... Wend needed during select case.
eg Input "What is your choice ";choice Select Case choice case 1 : goto [part1] case 2 : goto [part2] ... end Select
|
|
|
Post by plus on Jul 19, 2022 2:53:55 GMT
You could use While... Wend to get the key choice for Menu 1 to 4 like this:
[getChoice] while k$ = "" k$ = input$(1) wend if val(k$)<1 or val(k$)>4 then goto [getChoice] print k$
|
|
|
Post by jarychk on Jul 19, 2022 5:07:40 GMT
You could use While... Wend to get the key choice for Menu 1 to 4 like this: [getChoice] while k$ = "" k$ = input$(1) wend if val(k$)<1 or val(k$)>4 then goto [getChoice] print k$
That begins to look interesting but is confusing. Some few moments of further thought give a feeling that the option for EXIT choice could be part of the WHILE loop condition. This kind of program can become very complicated, with or without a WHILE loop. So far, my little program in current design is still going well, and I have kept with the same started main menu coordinated with SELECT CASE structure. I put in a couple of GOTO [branchlabel] as a way of looping back within part of the program, but so far still do not see the need for a while-loop.
|
|
|
Post by Rod on Jul 19, 2022 10:01:53 GMT
while k$ = "" k$ = input$(1) wend
Is no different to
[getkey] k$=input$(1) if k$="" then goto [getkey]
The point about While Wend and other variants is that it encourages structured programming and reduces "jumps"
|
|
|
Post by jarychk on Jul 19, 2022 11:21:00 GMT
while k$ = "" k$ = input$(1) wend Is no different to [getkey] k$=input$(1) if k$="" then goto [getkey] The point about While Wend and other variants is that it encourages structured programming and reduces "jumps" Somewhere was a good bit of guidance about this a long time ago. That guidance may have been on one of the JB or LB conforums, or maybe in a wiki article no longer available. IF I find a reason or way to make my current in-development program go from the bottom of the SELECT CASE structure back to the top of the SELECT CASE structure, then maybe I would be able to apply a WHILE Loop.
|
|
|
Post by tsh73 on Jul 19, 2022 13:49:12 GMT
does program supposed to have more then one operation? If not, and it works like this
start program present a menu - select 1 operation do select case - execute that 1 operation quit looks like you don't need a loop
But if it supposed to work like this
start program while not quit ...present a menu - select next one operation ...do select case - execute that selected operation ... quit
then you surely have a place for a loop.
|
|
|
Post by jarychk on Jul 26, 2022 23:52:17 GMT
Upon another online web search, I believe I found just what I was asking - if I think about the article carefully. This online page helps: www.javatpoint.com/menu-driven-programs-in-pythonUnderstandable, Python is not BASIC. Much of the wording used for the code is different, but the structuring is mostly understandable. Still, trying to use the structure as seen there will require some care. While looking through the page's program code, I could not see the matching WEND for the main WHILE. What could be seen is that the main program starts immediately at the top of the WHILE, and between the main prog loop is what would be my choice to use SELECT CASE structure instead of those IF ELIF ESLE things.
|
|
|
Post by Rod on Jul 27, 2022 9:16:38 GMT
There will always be a WEND to a WHILE else the compiler will complain. That's one of the benefits of structured loops like WHILE WEND, the compiler knows to expect a WEND. If the program is a bunch of GOTO statements the compiler can't hope to know what is going on.
So it is really structured programming techniques you should research. I can sum it up in a word or two. "The complete elimination of GOTO."
|
|
|
Post by tsh73 on Jul 27, 2022 9:46:17 GMT
Python is not BASIC instead of
while 1 repeat stuff wend do things after loop Python relies on indenting keeped right so end of indented block corresponds to WEND, END IF, NEXT etc
while 1 repeat stuff
do things after loop
|
|
|
Post by jarychk on Jul 28, 2022 10:42:30 GMT
I think I know what to do with this now. Here is a general outline of a framework for WHILE loop for the start and end of a menu based text only program (It is not a runnable program as shown).
Remarks and program identification for the user
opt=1 WHILE opt<4 and opt>0
CLS PRINT "CHOOSE OPTION FOR WHAT YOU WANT TO DO. PRINT PRINT "1. Possible preparatory task" PRINT "2. One of the routine tasks" PRINT "3. Different routine task" PRINT "4. EXIT the program" PRINT INPUT "What is your choice? ";opt
IF opt<>int(opt) OR opt<1 OR opt>=4 THEN PRINT "NOT A MENU CHOICE OPTION" PRINT "PROGRAM ENDING" END END IF
SELECT CASE opt CASE 1 code for performing preparatory task CASE 2 code for performing a routine task code may include submenu CASE 3 code for different routine task code could include submenu CASE 4 other code remarks possible but code to exit the program END SELECT
WEND
END
[subroutinesAndFunctionsMayPlacedHere]
|
|
|
Post by Rod on Jul 28, 2022 20:26:28 GMT
One thing to practice is indentation. Indent the code using tab every time you start a piece of structured code.
|
|
|
Post by jarychk on Jul 29, 2022 0:07:43 GMT
One thing to practice is indentation. Indent the code using tab every time you start a piece of structured code. That's right. The outline I show does not have much or any indenting, but the program I make (and actually made) using something near to the format uses very much indenting under each section within the program framework.
|
|
|
Post by jarychk on Jul 29, 2022 1:52:20 GMT
Forum problem? Rod! I came back to the topic to edit my example framework to indent in another block of code, but forum will not accept any tab nor any space-key press; but puts the page back to just show the sequence of posts for this topic.
few minutes later, I believe I have how to. Very carefully, not quickly, I press the spacebar key. If I do too fast, forum bumps me out of the edit form.
|
|
|
Post by Rod on Jul 29, 2022 15:50:03 GMT
I would indent the while wend also. I would simplify the opt 4 code and put it as the while wend test. So you can only use 1-3 or exit with 4, all other input is ignored.
WHILE opt<>4
CLS PRINT "CHOOSE OPTION 1-4." PRINT "Last Choice ";opt PRINT "1. Possible preparatory task" PRINT "2. One of the routine tasks" PRINT "3. Different routine task" PRINT "4. EXIT the program" PRINT INPUT "What is your choice? ";opt
SELECT CASE opt CASE 1 'code for performing preparatory task CASE 2 'code for performing a routine task 'code may include submenu CASE 3 'code for different routine task 'code could include submenu CASE ELSE 'do nothing END SELECT WEND
PRINT "Goodbye" END
|
|