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

0 Members and 1 Guest are viewing this topic.

Arnold

  • Hero Member
  • *****
  • Posts: 872
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

  • Hero Member
  • *****
  • Posts: 513
    • PluriBASIC
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

  • Hero Member
  • *****
  • Posts: 513
    • PluriBASIC
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

  • Hero Member
  • *****
  • Posts: 872
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

  • Admin Support Member
  • *****
  • Posts: 4205
    • Oxygen Basic
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

  • Hero Member
  • *****
  • Posts: 872
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

  • Admin Support Member
  • *****
  • Posts: 4205
    • Oxygen Basic
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

  • Hero Member
  • *****
  • Posts: 872
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

  • Hero Member
  • *****
  • Posts: 513
    • PluriBASIC
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

  • Admin Support Member
  • *****
  • Posts: 4205
    • Oxygen Basic
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 :)