A good question about (if-statement)

73 views
Skip to first unread message

Mahmoud Fayed

unread,
Jul 22, 2024, 10:06:03 PM (5 days ago) Jul 22
to The Ring Programming Language
Hello

Try to solve this question
Rewrite the next C code to use one (if-statement) and avoid using the not (!) operator

        if ( nNum2 <= 0 ) {
if ( ( ! ( nNum3 >= nNum1 ) ) || ( nNum2 == 0 ) ) {
RING_VM_JUMP ;
}
}
else {
if ( ! ( nNum3 <= nNum1 ) ) {
RING_VM_JUMP ;
}
}

It looks easy, and it's easy but if you are not careful, you will do a mistake


Greetings,
Mahmoud

Mansour Ayouni

unread,
Jul 23, 2024, 1:34:30 AM (4 days ago) Jul 23
to Mahmoud Fayed, The Ring Programming Language
Hello Mahmoud,

The JUMP will happen in two conditions: when nNum2 = 0, whatever values the other variables have (which we induce from the red part of the painting), and when nNum1 < nNum2 (which we can induce from the green painting).

The same result can written: if nNum2 = 0 and nNum1 < nNum3

image.png

Now I'll look at your solution ;)

Best,
Mansour

--

---
You received this message because you are subscribed to the Google Groups "The Ring Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-lang+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ring-lang/3e094d5c-7692-4eff-b94b-7f9e53392585n%40googlegroups.com.

Mansour Ayouni

unread,
Jul 23, 2024, 1:52:12 AM (4 days ago) Jul 23
to Mahmoud Fayed, The Ring Programming Language
Hello Mahmoud,

Your solution can be further optimised to cope exactly with mine:

image.png

So, my take is that you can solve it by:  nNum2 = 0 OR nNum3 > nNum1 

See and tell me...

Best,
Mansour

Mahmoud Fayed

unread,
Jul 23, 2024, 1:54:31 AM (4 days ago) Jul 23
to The Ring Programming Language
Hello Mansour

>> " The JUMP will happen in two conditions: when nNum2 = 0, whatever values the other variables have (which we induce from the red part of the painting)"

This is part of rewriting the first condition, but the situation is more complex than that

Some information about nNum1, nNum2 and nNum3
 nNum2 = Step value that can be positive or negative
 nNum1 = Items Count 
 nNum3 = Index

These values determine the behaviour of the for loop 

Greetings,
Mahmoud

Mahmoud Fayed

unread,
Jul 23, 2024, 2:02:25 AM (4 days ago) Jul 23
to The Ring Programming Language
Hello Mansour

>> "So, my take is that you can solve it by:  nNum2 = 0 OR nNum3 > nNum1 "

I tried your solution as in the next screen shot
pwct_jump.png1

But the tests don't pass

Some of them will fail as in the next screen shot
tests1.png1

While others will go into infinite loop as in the next screen shot
tests2.png1

So, the solution is not correct.

Greetings,
Mahmoud

Mansour Ayouni

unread,
Jul 23, 2024, 2:26:27 AM (4 days ago) Jul 23
to Mahmoud Fayed, The Ring Programming Language

Hello Mahmoud,

I can understand that can be so complex!

I was deeling with the same situation these days in stzWalker when I tried to introduce the concept of changing direction and moving forward and backward.

At the end, I decided to go back to the old simpler design, leaving the new features to a future release.

Best,
Mansour

All the best,
Mansour


Mahmoud Fayed

unread,
Jul 23, 2024, 2:30:50 AM (4 days ago) Jul 23
to The Ring Programming Language
Hello Mansour

>> "I can understand that can be so complex!"

The question contains two traps, You discovered the first one about nNum2 which exist in (nNum2 <= 0) and also exist in  ( nNum2 == 0 ) 

The second trap is related to 
else {
if ( ! ( nNum3 <= nNum1 ) ) {

When we look at an (else) condition we must assume that it will happens only if the previous conditions will not happen
i.e. the reality of this condition is   ! ( nNum2 <= 0 )  at first (inverse of the first condition) then  if ( ! ( nNum3 <= nNum1 ) ) {

Greetings,
Mahmoud

Mansour Ayouni

unread,
Jul 23, 2024, 4:29:07 AM (4 days ago) Jul 23
to Mahmoud Fayed, The Ring Programming Language
Hello Mahmoud,

Can you try this alternative?

if (nNum2 <= 0 and nNum3 < nNum1) or (nNum2 > 0 and nNum3 > nNum1)
      // do the jump
ok
  
Best,
Mansour


Mahmoud Fayed

unread,
Jul 23, 2024, 6:30:34 AM (4 days ago) Jul 23
to The Ring Programming Language
Hello Mansour

>> "Can you try this alternative?"

It's too much better but it misses one of the three conditions

It passes all of the tests which is not right
So, your update helped us to discover a missing test case reported by Ahmad Fayed and is added in this commit: Update Tests - language/tests/scripts/forinloop/test11.ring - Add ano… · ring-lang/ring@2891c39 (github.com)

Also, the code is revised and better names for variables are used

updatedcode.png1

Thank you very much my friend Mansour for your positive interaction and useful feedback, this helped in improving the tests and the code quality.

Greetings,
Mahmoud

Ilir Liburn

unread,
Jul 23, 2024, 6:31:27 AM (4 days ago) Jul 23
to The Ring Programming Language
Hello Mansour,

your statement is almost correct, lacking jump when num2 is (solely) zero. It should be like

if ( ( nNum2 > 0 && nNum3 > nNum1 ) || ( nNum2 < 0 && nNum3 < nNum1 ) || nNum2 == 0 ) {
RING_VM_JUMP ;
}

Greetings,
Ilir

Mansour Ayouni

unread,
Jul 23, 2024, 6:46:02 AM (4 days ago) Jul 23
to Ilir Liburn, The Ring Programming Language
Hello Mahmoud, Ilir, Ahmad,

This is energizing! Since this was the first thing I start my day with. Long life to Ring and all its team!

Best,
Mansour

Mahmoud Fayed

unread,
Jul 23, 2024, 6:48:30 AM (4 days ago) Jul 23
to The Ring Programming Language
Hello Mansour, Ilir

>> "This is energizing! Since this was the first thing, I start my day with. Long life to Ring and all its team!"

You are welcome :D

Greetings,
Mahmoud

Bert Mariani

unread,
Jul 23, 2024, 11:16:00 AM (4 days ago) Jul 23
to The Ring Programming Language
Hello Mahmoud et ALL

I think there is a bug in Ring.
Converted to C-Code Ring
Tested with different values for nNum2 and nNum3

nNum1 = 10
nNum2 = 5     //   5, 0
nNum3 = 8     // 15, 8

     ? "Start"

     if ( nNum2 <= 0 )
   
        if ( !( nNum3 >= nNum1 )  OR  

              ( nNum2  = 0 )  
           )    
           ? "RING_VM_JUMP_1" +" "+ nNum1 +" "+ nNum2 +" "+ nNum3
        ok

    else
        if ( !( nNum3 <= nNum1 ) )      
           ? "RING_VM_JUMP_2"  +" "+ nNum1 +" "+ nNum2 +" "+ nNum3
        ok
    ok
   
    ? "Finish"
   
//========================
/* RERULTS

Start
RING_VM_JUMP_2 10 5 30   // True   10 5 30 - good
Finish

Start
RING_VM_JUMP_1 10 0 30   // True   10 0 30 - good
Finish

Start                    // Failed 10 5 8 - bad
Finish
*/

Bert Mariani

unread,
Jul 23, 2024, 11:18:11 AM (4 days ago) Jul 23
to The Ring Programming Language
Sorry, nNum2 must be <= 0

Bert Mariani

unread,
Jul 23, 2024, 11:21:22 AM (4 days ago) Jul 23
to The Ring Programming Language

Start
RING_VM_JUMP_1 10 -4 8     // True  10 -4 8   good
Finish

Bert Mariani

unread,
Jul 23, 2024, 11:46:03 AM (4 days ago) Jul 23
to The Ring Programming Language
Hello Mahmoud et ALL

Here is my solution removing the "! Not"  using Ring code

//===============================
// ORIGINAL C-Code to Ring Code

nNum1 = 10
nNum2 = -4
nNum3 = 8


     ? "Start"
     if ( nNum2 <= 0 )
   
        if ( !( nNum3 >= nNum1 )  OR  
              ( nNum2  = 0 )  
           )    
           ? "RING_VM_JUMP_1" +" "+ nNum1 +" "+ nNum2 +" "+ nNum3
        ok
       
    else
        if ( !( nNum3 <= nNum1 ) )      
           ? "RING_VM_JUMP_2"  +" "+ nNum1 +" "+ nNum2 +" "+ nNum3
        ok
    ok
   
    ? "Finish"
   
//===================================
// ALTERNATIVE

     ? "Start2"

     if ( nNum2 <= 0 )
   
        if ( (( nNum3 < nNum1 )  OR  ( nNum2  = 0 ))  OR (nNum3 > nNum1) )
       
           ? "RING_VM_JUMP_1" +" "+ nNum1 +" "+ nNum2 +" "+ nNum3
        ok
       
    else    
           ? "RING_VM_JUMP_2"  +" "+ nNum1 +" "+ nNum2 +" "+ nNum3
    ok
   
    ? "Finish2"
  
//========================
/* RESULT


Start
RING_VM_JUMP_2 10 5 30    // True 10 5 30
Finish
Start2
RING_VM_JUMP_2 10 5 30    // ALT True 10 5 30
Finish2


Start
RING_VM_JUMP_1 10 0 30    // True 10 0 30
Finish
Start2
RING_VM_JUMP_1 10 0 30    // ALT True 10 0 30
Finish2

Start
Finish                    // True nNnb2 = 0 skip entire If-Else
Start2
RING_VM_JUMP_2 10 5 8     // ALT True  skip IF
Finish2


Start
RING_VM_JUMP_1 10 -4 8    // True 10 -4 8
Finish
Start2
RING_VM_JUMP_1 10 -4 8    // ALT True 10 -4 8
Finish2

*/

Mahmoud Fayed

unread,
Jul 23, 2024, 4:24:54 PM (4 days ago) Jul 23
to The Ring Programming Language
Hello Bert

>> "Here is my solution removing the "! Not"  using Ring code"

Thank you very much, Using Ring code to test/update the condition is a nice idea 

Attached the final C code as we have it in GitHub

We converted the code to one if-statement with one condition and without using Not too. 

updatedcode.png1

The code applies the next rules 
1 - If the Step value is Zero, the loop will not be executed (To avoid infinite loop)
2 - If the Step value is positive (increment) and the nStart is greater than nEnd, the loop will be terminated (To avoid infinite loop)
3 - If the step value is negative (decrement) and nStart is less than nEnd, the loop will be terminated (To avoid infinite loop)

i.e. the idea from this condition is providing a for-loop implementation that never go into infinite loop

Greetings,
Mahmoud

Bert Mariani

unread,
Jul 23, 2024, 6:27:09 PM (4 days ago) Jul 23
to The Ring Programming Language
Hi Mahmoud

In Ring code ... broke up the IF statement to show clarity / readability

      nStart =  5
for nStep =  -1 to 1
for nEnd  =   3 to 7

    See " "+ nStart +" "+ nStep +" "+ nEnd +" "
   
  if ( (( nStep < 0) && ( nStart < nEnd ) ) OR
       (( nStep > 0) && ( nStart > nEnd ) ) OR      
        ( nStep = 0)
     )

       See "RING_VM_JUMP:"
    ok
    See nl      
 next
    See nl
 next  
   
//========================
/*  OUTPUT

 5 -1 3
 5 -1 4
 5 -1 5
 5 -1 6 RING_VM_JUMP:
 5 -1 7 RING_VM_JUMP:

 5 0 3 RING_VM_JUMP:
 5 0 4 RING_VM_JUMP:
 5 0 5 RING_VM_JUMP:
 5 0 6 RING_VM_JUMP:
 5 0 7 RING_VM_JUMP:

 5 1 3
 5 1 4
 5 1 5
 5 1 6 RING_VM_JUMP:
 5 1 7 RING_VM_JUMP:
*/

Mahmoud Fayed

unread,
Jul 23, 2024, 6:32:00 PM (4 days ago) Jul 23
to The Ring Programming Language
Hello Bert

>> "In Ring code ... broke up the IF statement to show clarity / readability"

Thanks, The looks more nice and more readable

Just a little comment ---> Using (And) and (Or) together or using (&&) and (||) together will be more better than mixing (&&) and (OR)

Greetings,
Mahmoud
Reply all
Reply to author
Forward
0 new messages