OK, first of all, I am not a programmer. (yes, I heard the “thank god”) Perhaps I could make the top example simpler.

But anyway, I kind of like goto too much. I find it more intuitive to just jump around and re-use parts rather than think about how to do loops without too much nesting.

In high school, we only did Python. I really wanted to do goto in Python as well, but all I found was this April fools’ goto module.

Now in college we’re starting with C, and I am starting with Bad HabitsTM.

Anyway, tagging every line was BASICally just for the joke, but it is useful to just jump to any random line.

  • kryptonianCodeMonkey@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    8 minutes ago

    But anyway, I kind of like goto too much. I find it more intuitive to just jump around and re-use parts rather than think about how to do loops without too much nesting.

    Might I introduce you to functions?

    Need to write a prompt to the console and get an input? That could be a function. Need to check if a character matches some option(s)? That’s could be a function. Need to run a specific conditional subroutine? That could be a function. And function names, done correctly, are their own documentation too.

    You main function loop could look almost like pseudo code if you do it right.

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <string.h>
    
    char* prompt_for_input(const char* prompt_message) {
        char temp[100];
        printf(prompt_message)
        fgets(temp, 100, stdin);
        temp[strlen(temp)-1] = '\0';
        char* input = malloc(strlen(temp));
        strcpy(input,temp);
        return input;
    }
    
    int string_to_int(char* input) {
        char *leftover_chars:
        return (int)strtol(input, &leftover_chars, 10);
    }
    
    int prompt_for_loop_count() {
        char *input = prompt_for_input("\I'll loop over this many times: ");
        int loop_count = string_to_int(input);
        free(input);
        return loop_count;
    }
    
    bool prompt_to_do_again(){
        char *input = prompt_for_input("Let's do that again!\Shallow we? (y/n): ");
        bool do_again = (strcmp(input, "y") == 0 || strcmp(input, "Y") == 0);
        free(input);
        return do_again;
    }
    
    int main() {
        bool need_to_get_loop_count = true;
        while(need_to_get_loop_count) {
            Int loops_remaining = prompt_for_loop_count();
            do {
                printf("Current = %d\n", loops_remaining):
                loops_remaining--;
            } while(loops_remaining > 0);
            need_to_get_loop_count = prompt_to_do_again();
        }
    }
  • crimsonpoodle@pawb.social
    link
    fedilink
    arrow-up
    4
    ·
    11 hours ago

    Gotos are cool, it’s the way assembly works, if your mind likes them then more power to you. But if you work on teams then it’s a nogoto. Most of the functionality can be replaced with things that most people find easier. Just enjoy your time with and don’t ever is Kotlin. Fucking let me fucking return. I see them now those snooty opinionated safety fanatics “ooooh it will be so elegant in the closures the last value will be the return” bs.

    • dejected_warp_core@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      9 hours ago

      the last value will be the return

      This is the one thing that I really dislike about Rust. All this talk about safety and the return value/expression reads like a typeo w/o a semicolon. It even has return anyway since there are lots of scenarios that’s needed for an optimal path.

  • Tynan@lemmy.ml
    link
    fedilink
    English
    arrow-up
    3
    ·
    13 hours ago

    I’m glad you included the April Fool’s Day link. Comefrom is the worst thing I’ve ever seen.

    • notabot@piefed.social
      link
      fedilink
      English
      arrow-up
      16
      ·
      1 day ago

      It’s always fun to get your offset slightly wrong, and jump into the middle of an instruction.

      • Quantenteilchen@discuss.tchncs.de
        link
        fedilink
        arrow-up
        1
        ·
        9 hours ago

        Oh but only on fully built arm cores which support both thumb and the other mode!

        That way all you instructions the debugger shows will be wrong as well since you will have swapped asm mode. (This should, however, fix some off by one jumps because the core should adjust the address. The nice change will continue wracking havoc so not much is lost…)