C script samples.
In this section samples of cscript are displayed that you show you the features of the embedded C script interpreter. You can use the C script for the Experimenter and inside the Cscript block.
++--
main() {
int i,j;
j = 0;
j += 1;
j *= 4;
j /= 2;
j -= 2;
j |= 2;
for(i=0;i<10;i+=j-1)
print("x");
}
|
and-or
main() {
test(0,0);
test(10,0);
test(0,10);
test(10,10);
}
test(int i, int j) {
puts("");
print(i);
print(j);
puts("");
if (i && j)
puts("both i and j are non-zero");
else if (i || j)
puts("either i and j are non-zero");
else if ((i==0) && (j==0))
puts("both i and j are zero");
}
|
block
main() {
int i;
if (i == 1)
print("1111111");
else
print("0000000");
}
|
break continue
main() {
int i;
puts("loop1: for-break");
for (i=0;i<6;i=i+1) {
print("a");
print(i);
if (i > 3)
break;
print("b");
}
puts("");
puts("loop2: for-break in {}");
for (i=0;i<6;i=i+1) {
print("a");
print(i);
if (i > 3) { break; }
print("b");
}
puts("");
puts("loop3: for-continue");
for (i=0;i<6;i=i+1) {
print("a");
print(i);
if (i > 3) continue;
print("b");
}
puts("");
puts("loop4: while-break");
i = 0;
while (i<6) {
print("a");
print(i);
i=i+1;
if (i > 3) break;
print("b");
}
puts("");
puts("loop5: while-continue");
i = 0;
while (i<6) {
print("a");
print(i);
i=i+1;
if (i > 3) continue;
print("b");
}
puts("");
puts("loop6: do-continue");
i = 0;
do {
print("a");
print(i);
i=i+1;
if (i > 3) continue;
print("b");
} while (i < 6);
puts("");
puts("loop7: do-break");
i = 0;
do {
print("a");
print(i);
i=i+1;
if (i > 3) break;
print("b");
} while (i < 6);
puts("");
print("ok");
}
|
eob
main() {
int i, j;
for (i=0;i<3;i=i+1)
for(j=0;j<3;j=j+1)
print(j+i);
if (i == 1)
do {i=i-1;} while(i >-5);
i=i-10;
// for (i=0;i<10;i=i+1) ;
// else
print("0000000");
print(i);
puts("");
print("execution ended without problems!");
}
|
if
main() {
if (1==(1+3)*2-7)
print(1);
else
print(0);
if (1!=(1+3)*2-7) {
print(0);
} else
print(1);
if (1==(1+3)*2-7)
print(1);
else {
print(0);
}
if (1!=(1+3)*2-7) {
print(0);
} else {
print(1);
}
if (1==1)
print("a");
if (1!=1)
print("b");
}
|
loop
main() {
int i;
for (i = 0; i < 2; i = i+1)
print(1);
i = 0;
for (; i < 2; i = i+1)
print(2);
i = 0;
for ( ; i < 2; i = i+1)
print(3);
for (i = 0;i < 2;) {
print(4);
i = i + 1;
}
for (i = 0; i < 2; ) {
print(5);
i=i+1;
}
print("ok");
}
|
loop2
main() {
int i;
// first test for() and statement on the same line
// puts("for() and statement on same line");
for (i = 0; i < 10;i = i+1) print(1);
puts("");
/*
// now with a line break
// puts("line-break between for() and statement");
for (i=0;i < 10; i = i+1)
print(2);
puts("");
// block instead of statement
// puts("{ }-block instead of statement");
for (i = 0; i < 10; i = i+1) {
print(3);
}
puts("");
// no initialisation statement in for()
// puts("no initialisation statement");
i = 0;
for ( ; i < 10; i = i+1)
print(4);
puts("");
*/
for (;;) {
print(5);
}
puts("");
print("ok");
}
|
loop4
main() {
int i;
for (i=0; i<100000; i = i+1) {
}
print("");
}
|
recursive
main() {
recurs(0);
}
recurs(int level) {
int i;
i = level+1;
print(i);
if(level == 5) {
print("level 5 reached");
}else {
recurs(i);
}
print(i);
}
|
return
main() {
puts("calling function");
test(4);
puts("function has returned");
return 0;
puts("ERROR - this line comes AFTER main() has returned");
}
test(int val) {
if (val > 1) {
if (val > 2) {
if (val > 3) {
puts("about to return from function");
return 77;
puts("ERROR!");
}
}
}
}
|
test
int i, j; /* global vars */
char ch;
main()
{
int i, j;
puts("C Demo Program.");
print_alpha();
do {
puts("enter a number (0 to quit): ");
i = getnum();
if(i < 0 ) {
puts("numbers must be positive, try again");
}
else {
for(j = 0; j < i; j=j+1) {
print(j);
print("summed is");
print(sum(j));
puts("");
}
}
} while(i!=0);
}
/* Sum the values between 0 and num. */
sum(int num)
{
int running_sum;
running_sum = 0;
while(num) {
running_sum = running_sum + num;
num = num-1;
}
return running_sum;
}
/* Print the alphabet. */
print_alpha()
{
for(ch = 'A'; ch<='Z'; ch = ch + 1) {
putch(ch);
}
puts("");
}
|