Post a reply
Username:
Note:If not registered, provide any username. For more comfort, register here.
Subject:
Message body:
Enter your message here, it may contain no more than 60000 characters. 

Smilies
:D :) :( :o :shock: :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen:
Font size:
Font colour
Options:
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Disable BBCode
Disable smilies
Do not automatically parse URLs
Confirmation of post
To prevent automated posts the board requires you to enter a confirmation code. The code is displayed in the image you should see below. If you are visually impaired or cannot otherwise read this code please contact the %sBoard Administrator%s.
Confirmation code:
Enter the code exactly as it appears. All letters are case insensitive, there is no zero.
   

Topic review - For loop with multiple conditions (boolean expr)
Author Message
  Post subject:  Re: For loop with multiple conditions (boolean expr)  Reply with quote
Thank Hans, got it! Didnt think of the trick:
Code:
i+=2


-- VInay
Post Posted: Sun Jan 16, 2022 12:05 pm
  Post subject:  Re: For loop with multiple conditions (boolean expr)  Reply with quote
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;
}
Post Posted: Thu Jan 13, 2022 2:18 pm
  Post subject:  For loop with multiple conditions (boolean expr)  Reply with quote
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
Post Posted: Tue Jan 11, 2022 9:37 am


It is currently Fri May 13, 2022 10:54 am
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group