Conditions if ... else

    image
    Good day!

    Today is a great day for me.
    In questions I wrote about a brief reference for beginners and professionals, I thought that I would quickly plunge into the language, do good to everyone and go on to learn my Perl .
    But as always, everything went wrong ...

    Taking up the study, I had a small notebook, a chair in the kitchen and a printed plate of ASCII codes. Each new step was for me, like a new record, a new height.

    Sometimes running on a computer BrainfuckIDEand experimented. The experiments were quite fruitful, and after 5-6 hours I studied it thoroughly. He remembered all the algorithms of addition, subtraction, movement, copying, division and multiplication. I looked at elementary addition, subtraction and movement on one of the sites, I thought up the rest myself.

    Then he surfed on the Internet and saw that it turned out to be possible to make conditions on (it was a surprise to me). The main question: How?

    Not going to study and print the algorithm into his brain, he sat down with a notebook in the corner and began to think.
    I thought to the effect of complete abstraction from this world. It took two notebooks in which I tried a bunch of different options, moved, copied, tried to somehow compare, vague ideas when they were implemented on a piece of paper, to the end led to another chaos in my head.

    Having set myself the goal of writing a program that will compare the entered number with a known one, and output the result (equal or not equal), for two days I almost without rest (just a dream, food, toilet and dota) sat and imagined how to implement it, on so confusing beautiful language.

    I went to bed late enough (10 am), I could not fall asleep when this happened in my head.

    Waking up in the late afternoon, he took an unfinished second notebook and began to write, frankly, I myself do not understand how I came to such an algorithm, but after 2 hours it seemed to be working on a piece of paper. He sat behind a netbook, and a new axis was set on a stationary one at that time.
    I started to rewrite from a piece of paper, it turned out I did not take into account a lot of questions and pitfalls, but the algorithm still seemed correct.
    At the computer, debugging took no more than an hour, when suddenly, before my eyes, the program woke up, working exactly the way I wanted it to.
    The feeling is indescribable, it feels like you discovered a planet. My first words were: " B * la Well, finally!". Having debugged a little, I removed a bunch of + and -, replacing them with cycles, everything worked like in a fairy tale.

    Do not throw tomatoes and rotten eggs too much, but I will try to take a chance and bring the code that I wrote. See:
    ,[>+>+<<-]>>[<<+>>-]                   # Копируем введённое значение в 1 ячейку
    +++++ +++++[<----->-]<---              # Уменьшаем её на ascii код числа с которым сравниваем
    >>+>>+<<<<                             # Счётчики для циклов
    [                                      # Цикл Если n != 5
        [-]+++++ +++++[>+++++<-]>+++.      # Выводим 5
        [-]<++++++++[>++++<-]>.            # Выводим пробел
        [-]<++++++++[>++++<-]>+.           # Выводим восклицательный знак
        [-]<++++++++++[>++++++<-]>+.       # Выводим знак равенства
        [-]<++++++++[>++++<-]>.<<.         # Выводим пробел и число n
    >>>>>-<<-]                             # Обнуляем счётчики, что бы второй цикл не выполнился
    >>[                                    # Цикл иначе, практически всё тоже самое
        [-]+++++ +++++[>+++++<-]>+++.
        [-]<++++++++[>++++<-]>.
        [-]<++++++++++[>++++++<-]>+.
        [-]<++++++++[>++++<-]>.<<<<.
    >>>>>-]
    


    As you can see, the main idea is that the cycle would be executed if and only if the remainder of the difference between the numbers n and ascii is the code of the number with which they are compared! = 0.

    I think this will be a fairly clear example for beginners, and from you Dear gurus and ninjas of this language, I ask for advice and possibly criticism.

    Still interested in the questions:
    • How would the rest solve this issue?
    • Did I choose the right algorithm, or can I do with a simpler one?
    • What else can be optimized here, and is it possible at all?

    Also popular now: