GAMS [ Home | Downloads | Documentation | Solvers | APIs | Tools | Model Libraries | Resources | Sales | Support | Contact Us | Search ]

breakcont1.gms : Test break and continue statements

Description

Keyword: break, continue

Contributor: Michael Bussieck, August 2016

Small Model of Type : GAMS


Category : GAMS Test library


Main file : breakcont1.gms

$Title 'Test break and continue statements' (BREAKCONT1,SEQ=694)

$onText
Keyword: break, continue

Contributor: Michael Bussieck, August 2016
$offText


$onechoV > ct.gms
$echo "%1"                                                       > runCompTest.gms
$echo $if errorfree $abort Error: We expect a compilation error >> runCompTest.gms
$echo $clearerror                                               >> runCompTest.gms
$call gams runCompTest.gms a=c lo=2
$if errorlevel 1 $abort %msg%
$offecho

$set msg Continue outside control structure. We expect a compilation error
$batinclude ct "continue;"

$set msg Break outside control structure. We expect a compilation error
$batinclude ct "break;"

$set msg Break n outside control structure. We expect a compilation error
$batinclude ct "break 2;"

$set msg Break n with n<1 level. We expect a compilation error
$batinclude ct "set i /i1/; loop(i, break -1);"

$set msg Break 0. We expect a compilation error
*$batinclude ct "set i /i1/; loop(i, break 0);"

$set msg Break [const expression]. We expect a compilation error
$batinclude ct "set i /i1/; loop(i, break [abs(-1)]);"

$set msg Break scalar symbol. We expect a compilation error
$batinclude ct "set i /i1/; scalar k /1/; loop(i, break k);"

$set msg (Loop) Break n with n>control level. We expect a compilation error
$batinclude ct "set i /i1/; loop(i, break 2);"

$set msg (For) Break n with n>control level. We expect a compilation error
$batinclude ct "scalar k /0/; for(k=1 to 10, break 2);"

$set msg (While) Break n with n>control level. We expect a compilation error
$batinclude ct "scalar k /0/; k=0; while(k<10, break 2; k=k+1);"

$set msg (Repeat) Break n with n>control level. We expect a compilation error
$batinclude ct "scalar k /0/; k=0; repeat break 2; k=k+1; until k>10;"

$set msg Expect limited increment (loop, nestlevel 1);
set i / i1*i10 /; scalar cnt;
cnt=0; loop(i, break$sameas('i6',i);         cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;
cnt=0; loop(i, break$sameas('i6',i) 1;       cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;
cnt=0; loop(i, break$sameas('i6',i)1;        cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;
cnt=0; loop(i, continue$(mod(ord(i),2)=0);   cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;

* Really ugly syntax is allowed by our compiler!!!
cnt=0; loop(i, break$sameas('i6',i)1display'x'; cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;

$set msg Expect limited increment (for, nestlevel 1);
scalar v;
cnt=0; for(v=1 to 10, break$(v=6);           cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;
cnt=0; for(v=1 to 10, break$(v=6) 1;         cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;
cnt=0; for(v=1 to 10, continue$(mod(v,2)=0); cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;

$set msg Expect limited increment (while, nestlevel 1);
cnt=0; v=1; while(v<11, v=v+1; break$(v-1=6);         cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;
cnt=0; v=1; while(v<11, v=v+1; break$(v-1=6) 1;       cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;
cnt=0; v=1; while(v<11, v=v+1; continue$(mod(v,2)=0); cnt=cnt+1); abort$(cnt<>5) '%msg%', cnt;

$set msg Expect limited increment (repeat/until, nestlevel 1);
cnt=0; v=1; repeat v=v+1; break$(v-1=6);         cnt=cnt+1; until v>10; abort$(cnt<>5) '%msg%', cnt;
cnt=0; v=1; repeat v=v+1; break$(v-1=6) 1;       cnt=cnt+1; until v>10; abort$(cnt<>5) '%msg%', cnt;
cnt=0; v=1; repeat v=v+1; continue$(mod(v,2)=0); cnt=cnt+1; until v>10; abort$(cnt<>5) '%msg%', cnt;

set j / j1*j3 /; scalar w;

$set innerLoop loop(i, break$sameas('i6',i); cnt=cnt+1);

$set msg Expect limited increment (loop/loop)
cnt=0; loop(j, break$(ord(j)=3);           %innerLoop%);  abort$(cnt<>10) '%msg%', cnt;
cnt=0; loop(j, continue$(mod(ord(j),2)=0); %innerLoop%);  abort$(cnt<>10) '%msg%', cnt;

$set msg Expect limited increment (for/loop)
cnt=0; for(v=1 to 3, break$(v=3);           %innerLoop%); abort$(cnt<>10) '%msg%', cnt;
cnt=0; for(v=1 to 3, continue$(mod(v,2)=0); %innerLoop%); abort$(cnt<>10) '%msg%', cnt;

$set msg Expect limited increment (while/loop)
cnt=0; v=1; while(v<4, v=v+1; break$(v-1=3);           %innerLoop%); abort$(cnt<>10) '%msg%', cnt;
cnt=0; v=1; while(v<4, v=v+1; continue$(mod(v-1,2)=0); %innerLoop%); abort$(cnt<>10) '%msg%', cnt;

$set msg Expect limited increment (repeat/loop)
cnt=0; v=1; repeat v=v+1; break$(v-1=3);           %innerLoop%; until v>3; abort$(cnt<>10) '%msg%', cnt;
cnt=0; v=1; repeat v=v+1; continue$(mod(v-1,2)=0); %innerLoop%; until v>3; abort$(cnt<>10) '%msg%', cnt;

$set innerLoop loop(i, continue$(mod(ord(i),2)=0); cnt=cnt+1);

$set msg Expect limited increment (loop/loop)
cnt=0; loop(j, break$(ord(j)=3);           %innerLoop%);  abort$(cnt<>10) '%msg%', cnt;
cnt=0; loop(j, continue$(mod(ord(j),2)=0); %innerLoop%);  abort$(cnt<>10) '%msg%', cnt;

$set msg Expect limited increment (for/loop)
cnt=0; for(v=1 to 3, break$(v=3);           %innerLoop%); abort$(cnt<>10) '%msg%', cnt;
cnt=0; for(v=1 to 3, continue$(mod(v,2)=0); %innerLoop%); abort$(cnt<>10) '%msg%', cnt;

$set msg Expect limited increment (while/loop)
cnt=0; v=1; while(v<4, v=v+1; break$(v-1=3);           %innerLoop%); abort$(cnt<>10) '%msg%', cnt;
cnt=0; v=1; while(v<4, v=v+1; continue$(mod(v-1,2)=0); %innerLoop%); abort$(cnt<>10) '%msg%', cnt;

$set msg Expect limited increment (repeat/loop)
cnt=0; v=1; repeat v=v+1; break$(v-1=3);           %innerLoop%; until v>3; abort$(cnt<>10) '%msg%', cnt;
cnt=0; v=1; repeat v=v+1; continue$(mod(v-1,2)=0); %innerLoop%; until v>3; abort$(cnt<>10) '%msg%', cnt;

$set innerLoop loop(i, break$sameas('i6',i) 2; cnt=cnt+1);

$set msg Expect limited increment (loop/loop)
cnt=0; loop(j, break$(ord(j)=3);           %innerLoop%);  abort$(cnt<>5) '%msg%', cnt;
cnt=0; loop(j, continue$(mod(ord(j),2)=0); %innerLoop%);  abort$(cnt<>5) '%msg%', cnt;

$set msg Expect limited increment (for/loop)
cnt=0; for(v=1 to 3, break$(v=3);           %innerLoop%); abort$(cnt<>5) '%msg%', cnt;
cnt=0; for(v=1 to 3, continue$(mod(v,2)=0); %innerLoop%); abort$(cnt<>5) '%msg%', cnt;

$set msg Expect limited increment (while/loop)
cnt=0; v=1; while(v<4, v=v+1; break$(v-1=3);           %innerLoop%); abort$(cnt<>5) '%msg%', cnt;
cnt=0; v=1; while(v<4, v=v+1; continue$(mod(v-1,2)=0); %innerLoop%); abort$(cnt<>5) '%msg%', cnt;

$set msg Expect limited increment (repeat/loop)
cnt=0; v=1; repeat v=v+1; break$(v-1=3);           %innerLoop%; until v>3; abort$(cnt<>5) '%msg%', cnt;
cnt=0; v=1; repeat v=v+1; continue$(mod(v-1,2)=0); %innerLoop%; until v>3; abort$(cnt<>5) '%msg%', cnt;

$set msg Expect limited increment (loop, nestlevel 10)
alias (i,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10);
cnt=0;
loop(i1,
 loop(i2,
  loop(i3,
   loop(i4,
    loop(i5,
     loop(i6,
      loop(i7,
       loop(i8,
        loop(i9,
         loop(i10, break$sameas('i6',i10) 10; cnt=cnt+1)
        )
       )
      )
     )
    )
   )
  )
 )
);
abort$(cnt<>5) '%msg%', cnt;