Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SUBS or GOSUB; for library projects, which do you guys prefer?
#9
(08-27-2022, 09:05 PM)TempodiBasic Wrote: @SmcNeill
I know that it is possible to use GOTO in C, but never seen before this time.

Yes, "goto" exists in C, and sometimes it can be useful too, for example to get out of a deeply nested loop. Sure, that can also be solved differently, but much more complicated - and therefore more error-prone.

Here's an example of "goto" in C, but really just an example. One can easily program it differently. Many gotos . . .  Tongue

Code: (Select All)
//Goto Benutzung - 28. Aug.2022
//Prüfen der Eingabe ob Zahl

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

int main(void)
{
    int zahl, check;    
    double pi = 3.141592653589;
    double kreisumfang, quadratflaeche;
    double zylindervolumen;
    
    wiederholen:
    printf("\nBitte waehlen Sie:\n");
    printf("10 = Kreisumfang\n");
    printf("15 = Flaeche eine Quadrats\n");
    printf("20 = Volumen eines Zylinders\n");
    printf("\n0 fuer Ende: ");
    check = scanf("%d", &zahl);
    
    //Prüft ob Eingabe eine Zahl war.
    //Liefert scanf eine 0 zurück, war es keine Zahl
    if(check == 0)
        goto keine_zahl;
    
    if(zahl == 0)
    {
        goto ende;
    }    
    else if(zahl == 10)
        goto kreis_umfang;
    else if(zahl == 15)
        goto flaeche_quadrat;
    else if(zahl == 20)
        goto zylinder_volumen;
    else goto wiederholen;
    
    kreis_umfang:
    kreisumfang = (double)10 * pi;
    printf("\nEin Kreis mit dem Durchmesser von 10cm hat einen Umfang von %4.2f cm", kreisumfang);
    exit(0);
    
    flaeche_quadrat:
    quadratflaeche = pow(15.0, 2.0);
    printf("\nBei einem Quadrat von 15cm Kantenlaenge betraegt der Flaecheninhalt: %4.2f qcm\n", quadratflaeche);
    exit(0);
    
    zylinder_volumen:
    zylindervolumen = (((20.0 * 20.0) * pi) * 30.0);
    printf("\nZylindervolumen bei einem Radius von 20cm und einer Hoehe von 30cm betraegt: %4.2f ccm\n", zylindervolumen);
    exit(0);
    
    keine_zahl:
    printf("\n\aEingabe war keine Zahl!\n");
    exit(0);
    
    ende:
    printf("\a\nUnd Tschuessi!\n");
    return(0);
}

Screenshot:
[Image: goto-BSP2022-08-29.jpg]
Reply


Messages In This Thread
RE: SUBS or GOSUB; for library projects, which do you guys prefer? - by Kernelpanic - 08-29-2022, 07:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Aloha from Maui guys. Cobalt 18 3,063 01-20-2025, 07:33 PM
Last Post: Pete
  What do you guys like to use for mouse mapping? Pete 32 4,539 01-07-2025, 03:35 PM
Last Post: OldMoses
  Hey guys, riddle me this... Pete 8 1,396 01-01-2025, 02:23 AM
Last Post: Pete
  Viewing SUBs in IDE TerryRitchie 0 477 05-27-2024, 06:24 PM
Last Post: TerryRitchie
  Coverting GOSUB to GOTO? James D Jarvis 15 2,604 12-31-2022, 07:19 PM
Last Post: SMcNeill

Forum Jump:


Users browsing this thread: 1 Guest(s)