samedi 27 février 2021

C# (or any other code because the only needed is the logic) - Condition to permanently disable a previous condition

I need to know a simplified way to have a condition that permanent "disables" the event from a previous condition already met.

Maybe I'm not using the correct terms but I will explain what I need with an example.

Having the condition:

if (a > 10)
{
   print ("Correct");
}

Let's suppose the condition is met and printing the message Correct as should, but later the next new condition is met (important, after the 1st condition is met):

if (a <= 10)
{
   print ("Incorrect");
}

What I need is when the second condition is met then the first message Correct doesn't be displayed anymore even if the a <= 10 again, so I need that Correct be permanent "skipped", permanent "disabled" or the correct way it calls, and now in substitution of Correct, then the new and permanent displayed message be Incorrect. So as you can see, in the situation I describe Incorrect has more weight than Correct because once a <= 10 then the permanent message to be displayed is Incorrect ("forever").

For a while I have been using some different combinations of if + else, and if + else if, and if + and extra separated if, but no positive results for now, because yes, I get the Incorrect message displayed once the 2nd condition is met but for a strange reason what the code does is once the 2nd condition is met it displays both message on screen Correct and Incorrect, both at the same time and I don't understand why it could be happening because both conditions are mutually exclusive, so in theory, when once one of them is met what I think is the other is automatically "disabled". I don't know if the fact about both conditions are into a loop makes any special case because this is happening.

...
// Here is an example of what I'm using. I tried to put the next in 
// around 10 different ways,either in the same part of the code or in 
// different parts and always the same result, it displays as 
// should `Correct` but when the `a <= 10` is met it displays both messages
// `Correct` and `Incorrect`, 
// and what I need is when this happens, only be displayed `Incorrect` permantly.

if (a <= 10)
{
   print ("Incorrect");
}
else if (a > 10)
{
   print ("Correct");
}
...

Note: I think no extra code or context is needed because here we are talking about a very simple logic, for a simple code. By the way I'm using C# but I put the examples with generic code because the main I need is the logic.

Aucun commentaire:

Enregistrer un commentaire