Singular
https://www.singular.uni-kl.de/forum/

For loop with multiple conditions (boolean expr)
https://www.singular.uni-kl.de/forum/viewtopic.php?f=10&t=2968
Page 1 of 1

Author:  vinay [ Tue Jan 11, 2022 9:37 am ]
Post subject:  For loop with multiple conditions (boolean expr)

The following two similar codes are giving different outputs.

The following code:
Code:
for (i=1;i<=10;i++)
{
    if ((i%2)!=1)
    {
        i;
    }
}

returns the expected answer, whereas the code
Code:
for (i=1;i<=10  && (i%2)!=1;i++)
{
    i;
}

returns nothing.

Is this expected behaviour?

-- VInay

Author:  hannes [ Thu Jan 13, 2022 2:18 pm ]
Post subject:  Re: For loop with multiple conditions (boolean expr)

Yes, it is:
a for loop stops at that moment that the condition is not fulfilled:
this is here the case already for the start value of i: 1:
it is smaller than 10, not i%2 is not !=1: combined with &&: condition is false.
What you probably mean:
Code:
for(i=2;i<=10;i+=2)
{
  i;
}

Author:  vinay [ Sun Jan 16, 2022 12:05 pm ]
Post subject:  Re: For loop with multiple conditions (boolean expr)

Thank Hans, got it! Didnt think of the trick:
Code:
i+=2


-- VInay

Page 1 of 1 All times are UTC + 1 hour [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/