gotos

Paul Lew lew at gsg.UUCP
Fri Apr 22 01:57:55 AEST 1988


> How about: (pseudocode)
>    for (i = 0; i < max_in_table && key != name[i]; ++i) 
>       if (keypressed()) goto handle_key;
>    seek(helpfile, offset[i]);
>    if (keypressed()) goto handle_key;
>    linecount = 0;
>    do {
>       read_line_from_file();
>       ++linecount;
>       if (keypressed()) goto handle_key;
>       if (read_line[0] == special_flag_char) goto handle_key;
>       insert_spaces_and_color_codes();
>       if (keypressed()) goto handle_key;
>       display_text_line();
>       if (keypressed()) goto handle_key;
>       } while (linecount < 24);
>    handle_key:
>    c = wait_for_key_then_read_it();
>    switch (c) {
>       case 'A': ...
>        ...
>       }
> 
> I contend that if the reading and formatting and displaying are sufficiently
> slow, such behaviour can be desirable (i.e., on microcomputers).
> I would like to see a goto-less version of this that is easier to understand.

I think judicious use of goto is ok, but in this example, I think the
following code is easier to understand than your code. The block
stucture and ESPECIALLY indentation make it easier to understand, I
have to read your version line by line to figure out what it does.

    int kp = 0;		/* keypress indicator */
    for (i = 0; i < max_in_table && key != name[i]; ++i) 
       if (kp = keypressed()) break;
    if (!kp) {
       seek(helpfile, offset[i]);
       if (!keypressed()) {
          linecount = 0;
          do {
             read_line_from_file();
             ++linecount;
             if (keypressed() || read_line[0] == special_flag_char) break;
             insert_spaces_and_color_codes();
	     if (keypressed()) break;
             display_text_line();
             if (keypressed()) break;
             } while (linecount < 24);
          }
       }
    c = wait_for_key_then_read_it();
    switch (c) {
       case 'A': ...
        ...
-- 
Paul Lew			{oliveb,harvard,decvax}!gsg!lew	(UUCP)
General Systems Group, 5 Manor Parkway, Salem, NH 03079	(603) 893-1000



More information about the Comp.lang.c mailing list