Author Topic: Need help with for loop  (Read 1976 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
Need help with for loop
« on: June 08, 2019, 04:17:58 AM »
Hello,

can I get some help with this problem? I learned that in C/C++ a for loop can be converted to while like this:

for (expr1 ; expr2 ; expr3) {
   statements;
}

expr1;
while (expr2) {
   statements;
   expr3;
}

This should be easy to implement, nevertheless it is hard for me to understand this code snippet:

Code: [Select]
...
for (j = 0; j < 252; j = COLS * (j / COLS + 1)) {
for (; board[++j];) {
if (j % COLS == 10) {
lines++;

for (; j % COLS; board[j--] = 0)
   ;
update();

for (; --j; board[j + COLS] = board[j])
   ;
update();
}
}
}
...


How could this be represented in Oxygenbasic? Alternatively how could I use the C printf statement to display the values? I suppose I will never get used to C.

Roland

Brian Alvarez

  • Guest
Re: Need help with for loop
« Reply #1 on: June 08, 2019, 04:39:07 AM »
First one is the initial conditions, this is normally used to declare the iterator. Second one is the condition to continue the iteration. Third one is the action taken when the condition to continue the iteration evaluates as true. For example:

Code: [Select]
int j = 0

while j < 252

    ' some actions here.

    j = COLS * (j / COLS + 1)
end while

 Careful, because when using contine in the block above, the iterator modifier will not be executed unless used explicitly.
But of course, Oxygen supports this style of for/next blocks, so, you can simply....

Code: [Select]
int j = 0

for (j = 0; j < 252; j = COLS * (j / COLS + 1)) {

 ' do stuff as normal.

}

Brian Alvarez

  • Guest
Re: Need help with for loop
« Reply #2 on: June 08, 2019, 04:48:59 AM »
 About the other block:

Code: [Select]
for (; board[++j];) {

}

This can be interpreted as:

Code: [Select]
j++
while board[j]


  j++
end while

A ++ before a variable means something like: "increment before consulting its value".
A ++ after a variable means something like: "increment after consulting its value".
« Last Edit: June 11, 2019, 08:10:46 AM by Brian Alvarez »

Arnold

  • Guest
Re: Need help with for loop
« Reply #3 on: June 09, 2019, 12:48:10 AM »
Thank you Brian. Comparing with your code, I noticed some careless mistakes in my approach. Moreover using board[++j] and board[j++] seems to make a difference in some cases. These extended for loops are always a challenge for me.

Charles Pegge

  • Guest
Re: Need help with for loop
« Reply #4 on: June 09, 2019, 02:04:06 AM »
This code should carry a health warning   

It is both inefficient, and near impossible to read. So I would try to find better constructed source.



Arnold

  • Guest
Re: Need help with for loop
« Reply #5 on: June 09, 2019, 02:36:22 AM »
Yes, somehow I am insane. I try to understand some obfuscated code, which is already a nightmare of its own.

Charles Pegge

  • Guest
Re: Need help with for loop
« Reply #6 on: June 11, 2019, 07:17:02 AM »
The constructs are quite interesting from an academic point of view.

I'm making a few alterations for o2 to interpret them correctly

Here are some test cases:

Code: [Select]

'/*
'08:53 11/06/2019
'c style iteration / omitted expressions
uses console
int i,j
'for i=1;i<=4;i++ {printl i}
'for i=1;j<=4;i++ {j=i : printl i}
'for i=1;j<=4;i=i+1 {j=i : printl i}
'for i=1;j<=4;i+=1 {j=i : printl i}
'for ;i<=4;i++ {printl i}
'for ;i<=4; {printl i++}
'for ;; {printl i++ : exit for}
'for ;; {printl i++ }
'for (;++i and 7;) {printl i }
waitkey
'*/

Arnold

  • Guest
Re: Need help with for loop
« Reply #7 on: June 12, 2019, 12:59:38 AM »
Hi Charles,

you are always good for a surprise. The first two examples already work with Oxygen version B 0.1 and B 0.2. This is amazing. I always assumed that ";" is only used as a comment as specified in the help file. Using ";" with for loops seems to be an exception. Are there still more cases?

Roland

Brian Alvarez

  • Guest
Re: Need help with for loop
« Reply #8 on: June 15, 2019, 09:34:53 AM »
This code should carry a health warning   

It is both inefficient, and near impossible to read. So I would try to find better constructed source.

 Unfortunately, the programming world today is a savage jungle in which you have to adapt to survive.

If you cant beat them you have to join them, even if some of the aproaches make no sense and the original
creators later realize that it was jus not a good idea to add a feature simply because they would and deprecate
it.

Charles Pegge

  • Guest
Re: Need help with for loop
« Reply #9 on: June 15, 2019, 02:04:23 PM »
I looked very closely at this devilish code. It revealed very precisely how for loops are implemented in C, so o2 will be able to emulate it :)

Arnold

  • Guest
Re: Need help with for loop
« Reply #10 on: December 22, 2019, 12:14:38 PM »
While I was researching miniscm.c and o2scm.o2bas I found many for loops with multiple initialization or iteration expressions which are nicely transformed into while loops. Perhaps it is of interest to see the basic procedure:

Code: OxygenBasic
  1. uses console
  2.  
  3. /*
  4.    for (opt init(s); opt condition; opt iteration(s))
  5.          statement(s);
  6. --------------------------
  7.    opt init(s)
  8.    while (condition)
  9.       opt statement(s)
  10.       opt iteration(s)
  11.    wend  
  12. */
  13.  
  14. /*
  15.     int i, a    
  16.     for (i=0, a=10; i<5; i++, a--)
  17.     {
  18.        cout << i << "-" << a << endl;
  19.     }
  20. */
  21.     int i, a
  22.    
  23.     i=0 : a=10             'inits
  24.    While i<5              'condition
  25.       printl  i " - " a   'statement
  26.       i++ : a--           'iterations
  27.    wend
  28.     printl
  29.  
  30. /*
  31.     int i, j;
  32.     for ( i = 5, j = 10; i + j < 20; i++, j++ ) {
  33.         cout << "i + j = " << (i + j) << '\n';
  34.    }
  35. */
  36.     int i,j
  37.     i = 5 : j = 10                  'inits
  38.    while i + j < 20                'condition
  39.       printl "i + j = " (i + j)    'statement
  40.      i++ : j++                     'iterations
  41.    wend
  42.     printl
  43.        
  44. /*
  45.     int max = 5
  46.     int i = 0;
  47.     for(;;)
  48.     {
  49.         i++;
  50.         // use break to escape the loop
  51.         if(i > max)
  52.             break;
  53.         printf("i = %d\n",i);
  54.     }
  55. */    
  56.    int max=5, i=0
  57.    do
  58.       i++
  59.       if i > max then exit do
  60.       printl "i = " i
  61.    loop    
  62.  
  63.  
  64. printl "Enter ..."    
  65. waitkey
  66.  

In o2scm.o2bas there are also nested for loops with multiple initializations / iterations which have been converted into while loops. That makes things even more interesting.

Charles Pegge

  • Guest
Re: Need help with for loop
« Reply #11 on: December 24, 2019, 03:29:55 AM »
Yes. If there is any complexity in the iteration, do loops are much clearer and more maintainable than for loops.