Skip to content
gene-2012 edited this page Jan 6, 2024 · 1 revision

Loop in PhiScript

For-else Statements

Usage of for-else

for (<var> = <start>; <condition>; <next>){
    <code>
}
else{
    <code>
}
//Else can be omitted

Example of for-else

import io;
import os;
for (var i = 0; i < 10; i += 1){
    io.print(i);
}
else{
    io.print("i is greater than 10!");
}
os.system("pause");
//output:
//0
//1
//2
//3
//4
//5
//6
//7
//8
//9
import io;
import os;
for (var i = 10; i < 10; i += 1){
    io.print(i);
}
else{
    io.print("i is greater than 10!");
}
os.system("pause");
//output:"i is greater than 10!"

While-else Statements

Usage of while-else

while (<condition>){
    <code>
}
else{
    <code>
}
//Else can be omitted

Example of while-else

import io;
import os;
var x = io.get_number("Please input a number:", "Invalid input!");
while (x > 0){
    io.print(x);
    x = x - 1;
}
else{
    io.print("X = 0!");
}
os.system("pause");

Break & Continue Statements

Usage of break and continue

<some loop>{
    <code>
    if (<condition>){
        break;
    }
}
<some loop>{
    if (<condition>){
        continue;//at the beginning
    }
    <code>
}

Example of break and continue

import io;
import os;
for (var i = 0; i < 10; i += 1){
    if(i % 2 == 0){
        continue;
    }
    if (i == 5){
        break;
    }
    io.print(i);
}
//output:
//1
//3

Clone this wiki locally