Talk:Control Structures: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
Created page with 'Using '''else''' we can have a code block which executes when the if statement evaluates to true, and another code block which executes when the if statement evaluates to false. …'
 
imported>Yoyo
m question about static
 
(14 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Using '''else''' we can have a code block which executes when the if statement evaluates to true, and another code block which executes when the if statement evaluates to false.
Ok, old version no longer needed. A couple cleanup requests:
<code>
* add return
    if(false)
* change looping to flow control OR add a separate section for continue, break & return
      {
* the above 3 should each have a lv3 heading
      print("this line never gets printed");
--[[User:StDoodle|StDoodle (#1059825)]] 18:57, 18 April 2010 (UTC)
      }
      else
      {
      print("this line gets printed");
      }
</code>
<Br/>
<Br/>
We also need to understand nesting if statements.


<code>
Never mind; the stuff, I fixed it. --[[User:StDoodle|StDoodle (#1059825)]] 19:21, 18 April 2010 (UTC) (Though further info for break, continue & return might be nice).
  if(true)
 
    {
I was not aware of that use of switch statements, thanks! --[[User:StDoodle|StDoodle (#1059825)]] 13:55, 3 May 2010 (UTC)
    if(true)
 
      {
*That use of switch statements was one of the things zarqon and I pushed for when we were cajoling veracity to add it as a mafia feature. It is so much more readable to use a single switch statement than 6 "else if" statements. --[[User:Bale|Bale]] 23:33, 3 May 2010 (UTC)
      print("this line gets printed");
 
      }
Er, that is to say, I knew how to use them with a top-level variable, but not that you could omit that and use a sep. expression each time... and I could swear I tried to use expressions that way (apparently you can't do both at once!). :P --[[User:StDoodle|StDoodle (#1059825)]] 02:29, 4 May 2010 (UTC)
      else
 
      {
I'm wondering if a more explicit example is necessary. What I'm talking about is:
      print("this line never gets printed");  
 
      }    
{{
    print("this line gets printed also");
CodeSample|
    }
code=
  if(false)
<syntaxhighlight>
    {
switch {
    if(true)
case x < 0 && x == x % 2:
      {
  print(x+" is negative and even");
      print("this line never gets printed");
  break;
      //though inside an if(true) statement,  
case x < 0:
      //the outer if(false) stops the code from ever getting here.
  print(x+" is negative and odd");
      }
   break;
      else
case x == 0:
      {
  print("x is zero");
      print("this line never gets printed");
  break;
      }
case x == x % 2 && x < 10:
    print("this line never gets printed");
  print(x+" is a positive one digit even number");
    }
  break;
</code>
case x != x % 2
<Br/>
  print(x+" is an odd number");
<Br/>
  break;
Now you only need to put it all together as needed for your situation.
default:
  print(x+" is greater than 9");
}
</syntaxhighlight>}}
Do you think an example like that would be helpful? --[[User:Bale|Bale]] 03:19, 4 May 2010 (UTC)
 
That looks good; I'd add in one line with a different variable though, just to make the differentiation in use clear. Such as an initial check for y > 9000 or something. Then again, I pretty much figured it out from what's on the page, so I'm not sure it's necessary. Perhaps three samples are in order: a simple switch with a generic variable to test, your example, and a "This won't work" example that tries to mix the two. --[[User:StDoodle|StDoodle (#1059825)]] 13:51, 4 May 2010 (UTC)
 
Regarding foreach and sorting: since it sorts by index, not by value, that should be mentioned somewhere in that blurb. --[[User:Heeheehee|Heeheehee]] 12:10, 22 June 2010 (UTC)
* foreach DOES NOT SORT - it uses whatever ordering is provided by the aggregate you're iterating over.  For maps and arrays, the objects themselves maintain the keys in sorted order.  For plural typed constants, the ordering is however the elements were originally listed, which is completely arbitrary (and can even include duplicate keys). -- [[User:Jasonharper]] 16:26, 22 June 2010 (UTC) (signature added later)
 
::I know, but I felt it would take too much pain to write a full-length guide on maps and aggregates that includes that information. Hopefully, someone would in the future. --[[User:PhilmASTErpLus|PhilmASTErpLus]] 04:08, 23 June 2010 (UTC)
 
 
== repeat until ==
 
Does it loop as long as the boolean expression is true, or false? I assume false, somewhat different from how [http://en.wikipedia.org/wiki/Do_while_loop do while loop]s work. --[[User:PhilmASTErpLus|PhilmASTErpLus]] 04:19, 23 June 2010 (UTC)
 
:The loop ends when the expression is false. --[[User:Bale|Bale]] 04:32, 23 June 2010 (UTC)
 
== static ==
 
When using static in a function to declare/initialize variables do the variables retain those values from call to call, or will this cause an error when trying to run? Thanks --[[User:Yoyo|Yoyo]] ([[User talk:Yoyo|talk]]) 22:02, 26 March 2019 (UTC)

Latest revision as of 22:02, 26 March 2019

Ok, old version no longer needed. A couple cleanup requests:

  • add return
  • change looping to flow control OR add a separate section for continue, break & return
  • the above 3 should each have a lv3 heading

--StDoodle (#1059825) 18:57, 18 April 2010 (UTC)

Never mind; the stuff, I fixed it. --StDoodle (#1059825) 19:21, 18 April 2010 (UTC) (Though further info for break, continue & return might be nice).

I was not aware of that use of switch statements, thanks! --StDoodle (#1059825) 13:55, 3 May 2010 (UTC)

  • That use of switch statements was one of the things zarqon and I pushed for when we were cajoling veracity to add it as a mafia feature. It is so much more readable to use a single switch statement than 6 "else if" statements. --Bale 23:33, 3 May 2010 (UTC)

Er, that is to say, I knew how to use them with a top-level variable, but not that you could omit that and use a sep. expression each time... and I could swear I tried to use expressions that way (apparently you can't do both at once!). :P --StDoodle (#1059825) 02:29, 4 May 2010 (UTC)

I'm wondering if a more explicit example is necessary. What I'm talking about is:


switch {
case x < 0 && x == x % 2:
   print(x+" is negative and even");
   break;
case x < 0:
   print(x+" is negative and odd");
   break;
case x == 0:
   print("x is zero");
   break;
case x == x % 2 && x < 10:
   print(x+" is a positive one digit even number");
   break;
case x != x % 2
   print(x+" is an odd number");
   break;
default:
   print(x+" is greater than 9");
}

Do you think an example like that would be helpful? --Bale 03:19, 4 May 2010 (UTC)

That looks good; I'd add in one line with a different variable though, just to make the differentiation in use clear. Such as an initial check for y > 9000 or something. Then again, I pretty much figured it out from what's on the page, so I'm not sure it's necessary. Perhaps three samples are in order: a simple switch with a generic variable to test, your example, and a "This won't work" example that tries to mix the two. --StDoodle (#1059825) 13:51, 4 May 2010 (UTC)

Regarding foreach and sorting: since it sorts by index, not by value, that should be mentioned somewhere in that blurb. --Heeheehee 12:10, 22 June 2010 (UTC)

  • foreach DOES NOT SORT - it uses whatever ordering is provided by the aggregate you're iterating over. For maps and arrays, the objects themselves maintain the keys in sorted order. For plural typed constants, the ordering is however the elements were originally listed, which is completely arbitrary (and can even include duplicate keys). -- User:Jasonharper 16:26, 22 June 2010 (UTC) (signature added later)
I know, but I felt it would take too much pain to write a full-length guide on maps and aggregates that includes that information. Hopefully, someone would in the future. --PhilmASTErpLus 04:08, 23 June 2010 (UTC)


repeat until

Does it loop as long as the boolean expression is true, or false? I assume false, somewhat different from how do while loops work. --PhilmASTErpLus 04:19, 23 June 2010 (UTC)

The loop ends when the expression is false. --Bale 04:32, 23 June 2010 (UTC)

static

When using static in a function to declare/initialize variables do the variables retain those values from call to call, or will this cause an error when trying to run? Thanks --Yoyo (talk) 22:02, 26 March 2019 (UTC)