Jump to content

ContinueCase - Bug or by design?


Recommended Posts

After reading this topic: http://www.autoitscript.com/forum/index.ph...st&p=169029

I did a little testing and find the behaviour of ContinueCase to be unexpected according to the documentation on Select and Switch

Example: I would think this would make $newstring = "AB" but does not. Instead makes it "A1B"

$string = "ABCD" 
$newString = ""

Select
    
    Case StringInStr($string, "A")
        $newString = "A" 
        ContinueCase

    Case StringInStr($string, "1")
        $newString &= "1" 
        ContinueCase

    Case StringInStr($string, "B")
        $newString &= "B" 
        
EndSelect

MsgBox(0, "", $newString)oÝ÷ ØZ¦®ËajÛ©¢w§r§µêâayø«²Ø§²Ö§qì!z·   «ý¸¬¶»jYl¹»ªç§µÆ¬zÆ«yÆ«®'¢ëpØhº×¯j[¶)ජwb¶*'jT­®)à"t­®«¶íç±ë-jצz{Hë,zw¶ëay¦è½ç(uçîËb¢v¬#
.×±¥ç-jwl¶)eºÇ   «ü*'¶)îx&¬yú+«b·
.ÙÞyÛhm殶­sbb33c·7G&ærÒgV÷C´$4BgV÷C²¢b33c¶æWu7G&ærÒgV÷C²gV÷C° ¥6VÆV7@  66R b7G&ætå7G"b33c·7G&ærÂgV÷C´gV÷C²FVâb33c¶æWu7G&ærf׳ÒgV÷C´gV÷C² 6öçFçVT66P  66R  b7G&ætå7G"b33c·7G&ærÂgV÷C³gV÷C²FVâb33c¶æWu7G&ærf׳ÒgV÷C³gV÷C² 6öçFçVT66P  66R0 b7G&ætå7G"b33c·7G&ærÂgV÷C´"gV÷C²FVâb33c¶æWu7G&ærf׳ÒgV÷C´"gV÷C² ¤VæE6VÆV7@ ¤×6t&÷ÂgV÷C²gV÷C²Âb33c¶æWu7G&ær

So, is it just me? Is the function buggy? Or does the documentation need to be made a little more explicit?

Thanks

Link to comment
Share on other sites

I can understand your confusion, but its behavior is certainly by design. I've looked through the documentation on both Select...End Select and ContinueCase and I just don't see where the documentation is unclear; it's functioning exactly as described in the help file.

My Projects:DebugIt - Debug your AutoIt scripts with DebugIt!
Link to comment
Share on other sites

It does appear to do exactly as described in the help file:

Remarks

Normally in a Select or Switch block, a case ends when the next Case statement is encountered. Executing the ContinueCase will tell AutoIt to stop executing the current case and start executing the next case.

But that is counter-intuitive. I would have expected ContinueCase to continue with TESTING the next case, not unconditionally executing it. And that also seems more useful.

<_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Moderators

It does appear to do exactly as described in the help file:

But that is counter-intuitive. I would have expected ContinueCase to continue with TESTING the next case, not unconditionally executing it. And that also seems more useful.

<_<

Makes sense to me, if you have structured your cases as basic conditions, one condition that could never be true unless ContinueCase was sent from previous case.

But to be honest, I don't see why anyone would ever use it rather than a simple condition statement unless they were trying to save code space on a case that could actually be true.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

It does appear to do exactly as described in the help file:

But that is counter-intuitive. I would have expected ContinueCase to continue with TESTING the next case, not unconditionally executing it. And that also seems more useful.

<_<

That's what I thought (the un-counter-intuitive way) since under Select the help file says

If the expression is true the following statements up to the next Case or EndSelect statement are executed

Thus I let myself think this meant that a ContinueCase would indeed continue with testing the next case and so on and so on.

Makes sense to me, if you have structured your cases as basic conditions, one condition that could never be true unless ContinueCase was sent from previous case.

Which makes sense? What PsaltyDS said: that it's counter-intuitive, or the documentation makes sense as it is?

But to be honest, I don't see why anyone would ever use it rather than a simple condition statement unless they were trying to save code space on a case that could actually be true.

I kind of thought the original thread that I linked to had a valid case in this post: http://www.autoitscript.com/forum/index.ph...st&p=429791

where the OP was looking to iterate through different groups that a member could belong to and assign cumulative drive mappings based on the ultimate resulting memberships.

Anyway, in the end I guess we can all agree: it's just me... :)

Edited by ResNullius
Link to comment
Share on other sites

Just to verify: Same funky behavior with Switch as with Select:

$string = "ABCD" 
$newString = ""
Switch $string
    Case "ABCD" 
        $newString = "A" 
        ContinueCase
    Case "1" 
        $newString &= "1" 
        ContinueCase
    Case "B" 
        $newString &= "B" 
EndSwitch
MsgBox(0, "", $newString)

As per the help file for ContinueCase, but still funky...

<_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

But ... Psalty, what are you testing for? As far as I understand it, the switch/case structure tests a static. Something that's been evaluated prior to the test. The select/case structure is more dynamic and can test conditions similar to the If/Then/ElseIf/Else/EndIf.

$count = 0
Do
    $count += 1
    $var = Random(1, 6, 1)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $var = ' & $var & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console  
    Switch $var
        Case 1, 2, 3
            MsgBox(0, "", "123")
            ContinueCase
        Case 4, 5, 6
            MsgBox(0, "", "456")
            ContinueCase
        Case 1
            MsgBox(0, "", "1")
            ContinueCase
        Case 6
            MsgBox(0, "", "6")
            ContinueCase
    EndSwitch
Until $count = 6

Odd.... with my code, as soon as something evaluates true, all the others are automatically true. Is this a misunderstanding on the usage of "ContinueCase"?

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

But ... Psalty, what are you testing for? As far as I understand it, the switch/case structure tests a static. Something that's been evaluated prior to the test. The select/case structure is more dynamic and can test conditions similar to the If/Then/ElseIf/Else/EndIf.

The test was of the funky behavior of ContinueCase. The funkiness was already demonstrated in Select, and I was just verifying it was the same funkiness in Switch. The default test of a Switch Case is "=", so just listing a value tests if it is equal (non-case sensitive) to the static input value on the Switch line.

<_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Two things that come to my mind:

- by just reading the code, I would say the next case would be checked.

"ContinueCase" sounds like: countinue with next case, and that includes checking ("Case").

- maybe it would be best to look at another language like java or c,

and make select/switch behave not contradictory.

ciao

Xandl

PS: I think I never stumbled over that, as I use ContinueCase rarly.

Link to comment
Share on other sites

I think everyone needs to look at how this is done in PHP and C++ (Not C#). By default the case statement will cascade or fallthrough.

PHP:

CODE
switch ($i) {

case "apple":

echo "i is apple";

break;

case "bar":

echo "i is bar";

break;

case "cake":

echo "i is cake";

break;

}

?>

It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example:

CODE
switch ($i) {

case 0:

echo "i equals 0";

case 1:

echo "i equals 1";

case 2:

echo "i equals 2";

}

?>

Here, if $i is equal to 0, PHP would execute all of the echo statements! If $i is equal to 1, PHP would execute the last two echo statements. You would get the expected behavior ('i equals 2' would be displayed) only if $i is equal to 2. Thus, it is important not to forget break statements (even though you may want to avoid supplying them on purpose under certain circumstances).

In a switch statement, the condition is evaluated only once and the result is compared to each case statement. In an elseif statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a switch may be faster.

The statement list for a case can also be empty, which simply passes control into the statement list for the next case.

C++:

CODE
// This program is carefully constructed to use the "fall through"

// feature of the switch statement.

#include

using namespace std;

int main()

{

int modelNum;

cout << "Our TVs come in three models:\n";

cout << "The 100, 200, and 300. Which do you want? ";

cin >> modelNum;

cout << "That model has the following features:\n";

switch (modelNum)

{

case 300: cout << "\tPicture-in-a-picture\n";

case 200: cout << "\tStereo sound\n";

case 100: cout << "\tRemote control\n";

break;

default : cout << "You can only choose the 100, ";

cout << "200, or 300.\n";

}

return 0;

}

Edited by weaponx
Link to comment
Share on other sites

Ah, so the cascading is the expected result when one uses "ContinueCase" I was under the impression that the interperator just hopped out at that point and and evaluated the items as one would normally expect.

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

/codebox]

C++:

CODE
// This program is carefully constructed to use the "fall through"

// feature of the switch statement.

#include

using namespace std;

int main()

{

int modelNum;

cout << "Our TVs come in three models:\n";

cout << "The 100, 200, and 300. Which do you want? ";

cin >> modelNum;

cout << "That model has the following features:\n";

switch (modelNum)

{

case 300: cout << "\tPicture-in-a-picture\n";

case 200: cout << "\tStereo sound\n";

case 100: cout << "\tRemote control\n";

break;

default : cout << "You can only choose the 100, ";

cout << "200, or 300.\n";

}

return 0;

}

So for anyone familiar with DOS, this appears to be similar to the "If Errorlevel" checking in batch files, where you had to start your checking with the highest errorlevel and work you way down to the lowest?

Makes sense when dealing with fixed numbers that you can rank in your case checking, but not so much with logical constructs like the "Case StringInStr()" which my dumb brain sees as something that should still be evaluated after a "ContinueCase".

Anyway, I think this is starting to get too deep for me, so if the behaviour is the same in other languages, I'm willing to let it be <_<

Edited by ResNullius
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...