Q
Q
Qwait2014-02-18 17:03:06
IT education
Qwait, 2014-02-18 17:03:06

How to programmatically implement a causal relationship?

Hello.
Let me know if I'm going in the right direction.
I argue on the example of the construction IF - THEN - ELSE.
IF event_occurs THEN command_1 ELSE command_0.
IF button_pressed THEN print_character ELSE just_wait.
Don't you think that on the example of a button, everything looks rather primitive.
If you also think so or you are sure, then let's figure out together how, what and where.
Namely, how to programmatically (competently) implement a causal relationship?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Fat Lorrie, 2014-02-18
@Free_ze

Well, callbacks are still being implemented. Slot signals. Events. But this is all, in fact, syntactic sugar around an ordinary IF.

Y
Yuri Lobanov, 2014-02-18
@iiil

The programmer always puts two glasses on the bedside table:
one full, in case he wants to drink, and the second empty, in case he does not want to.

T
tsarevfs, 2014-02-18
@tsarevfs

Let's consider the option with the button. Let's say we have some function that returns the status of a button.
Waiting in the simplest case is an infinite loop.

while true
{
   if (buttonPressed())
   {
      print('hello');
   }
}

This code loops endlessly and if the button is pressed it prints 'hello'. Note that there are some problems. Even though the code does nothing most of the time, it will be very CPU intensive. In addition, while the button is pressed, the inscription will be repeated at a tremendous speed.
while true
{
   if (buttonPressed())
   {
      print('hello'); 
   }
   sleep(300); 
}

In this form, the program will not eat up the processor, and the inscription will be repeated at a reasonable speed, as when pressing a button on the keyboard.
Move on. If the label should appear exactly once:
bool buttonReleased = true;
while true
{
   if (buttonPressed())
      if (buttonReleased)
      {
         print('hello'); 
         buttonReleased = false;
      }
   else
      buttonReleased = true;
   sleep(300); 
}

PS
What language do you learn to write in?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question