If statements are a fundamental programming construct used to implement conditional logic in a program. If statements allow the programmer to execute a set of instructions if a specified condition is true. If the condition is false, the program will either execute a different set of instructions or skip the block of code entirely. In Artsy language, if statements are expressed using the "if-elif-else" syntax.
The syntax for an if statement in Artsy language is as follows:
if (condition) {
// code to be executed if the condition is true
}
elif (condition) {
// code to be executed if the first condition is false and the second condition is true
}
else {
// code to be executed if all conditions are false
}
Here, the condition is an expression that evaluates to either true or false.
Here is an example program written in Artsy language that uses an if statement:
declare a as int;
let a = 5;
if (a < 10) {
print "a is less than 10";
addline;
}
elif (a > 10) {
print "a is greater than 10";
addline;
}
else {
print "a is equal to 10";
addline;
}
In this example, the program declares an integer variable named "a" and assigns it an initial value of 5. The if statement is used to determine whether the value of "a" is less than, greater than, or equal to 10. If the condition in the if statement is true, the program will execute the block of code within the if statement. If the condition is false, the program will check the next condition in the elif statement. If all conditions are false, the program will execute the code within the else statement.
If statements are often used within loops to provide more control over the loop's behavior. Here is an example program written in Artsy language that uses an if statement within a while loop:
declare i as int;
let i = 0;
while (i < 10) {
if (i % 2 == 0) { # (Working Progress)
print i;
addline;
}
let i = i + 1;
}
In this example, the program declares an integer variable named "i" and assigns it an initial value of 0. The while loop is used to execute the if statement and increment the value of "i" until it reaches 10. The if statement is used to determine whether the value of "i" is even. If the condition is true, the program will execute the print statement, displaying the value of "i" on the screen.