Operators
It is used to operate on variables and values. We can examine it under 4 main headings.
We will see "if" statement in Conditional Expressions section. However, since we will use it in this section, if we talk about it briefly, condition part works if it is "true", it does not work if it is "false".
Example:
if(true) print("true");
if(false) print("false");
if(1==1) print("1 is equal to 1");
/*
Output:
true
1 is equal to 1
*/
Arithmetic Operators
Addition(+): Adds values.
Example:
print(5+10+15); //Output: 20
Text name="HAK";
print("Hello "+name); //Output: Hello HAK
Subtraction(-): Subtracts values.
Example:
Int num=100;
print(num-10-20); //Output: 70
Multiplication(*): Multiplies values.
Example:
print(10*(5-3)); //Output: 20
print(2.5*2); //Output: 5
Division(/): Divides values.
Example:
Int num=50;
print("10% of number {num} becomes {num*10/100}."); //Output: 10% of the number 50 becomes 5.
print(10/4); //Output: 2
print(10.0/4); //Output: 2,5
Remainder(%): Finds remainder from division of values.
Example:
print(10%4); //Output: 2
print(10.0%4); //Output: 2.0
print(19%5%3); //Output: 1
Increment by 1(++): Increments variable by 1.
Example:
Int num=0;
num++;
print(num); //Output: 1
++num;
print(num); //Output: 2
Decrease by 1(--): Decreases variable by 1.
Example:
Int num=5;
num--;
print(num); //Output: 4
--num;
print(num); //Output: 3
We put increment and decrement operators before or after and it apparently did same thing. If increment or decrement operator is on right, value is returned first, then incremented by 1, if it is on left, it is incremented by 1 and then value is returned.
Example:
Int num1=0;
print(num1++); //Output: 0
Int num2=5;
print(--num2); //Output: 4
Assignment Operators
It is used to assign values to objects or variables. Assigns value on right to object or variable on left.
Assignment(=)
Example:
Int num=5;
print(num); //Output: 5
Text message="Hello";
print(message); //Output: Hello
Assignment with Arithmetic Operators
Usage: <ArithmeticOperator>=
Let's add 5 to number.
Int num=1;
num+=5;
print(num); //Output: 6
Let's subtract 10 from number.
Int num=50;
num-=10;
print(num); //Output: 40
Let's multiply number by 2.
Int num=15;
num*=2;
print(num); //Output: 30
Let's divide number by 5.
Int num=10;
num/=5;
print(num); //Output: 2
Let's assign remainder of number to number.
Int num=11;
num%=5;
print(num); //Output: 1
Comparison Operators
Compares value to right with value to left.
If Equal(==)
Example:
Text systemPassword="asd123";
Metin enteredPassword="asd123";
eğer(systemPassword==enteredPassword) print("Logged In");
//Output: Logged In
If Not Equal(!=)
Example:
Text password="asd123";
Text againPassword="asd12";
eğer(password!=againPassword) print("Passwords are not same");
//Output: Passwords are not same
If Greater(>)
Example:
Int phone=6; //inch
Int tablet=10; //inch
if(tablet>phone) print("Tablet is bigger than phone");
//Output: Tablet is bigger than phone
If Less(<)
Example:
Int score=49;
if(score<50) print("Failed");
//Output: Failed
If Greater or Equal(>=)
Example:
Int score=60;
if(score>=50) print("Passed");
//Output: Passed
If Less or Equal(<=)
Example:
Int productPrice=40;
Int availableMoney=50;
if(productPrice<=availableMoney){
availableMoney-=productPrice;
print("Purchased\nMoney Remaining: {availableMoney}");
}
/*
Output:
Purchased
Money Remaining: 10
*/
Logical Operators
It is used to specify logic between variables or values.
And(&&): Returns "true" if value to right and left is "true".
Example:
Truth hasNeed=true;
Truth hasMoney=true;
if(hasNeed && hasMoney) print("Can buy");
//Output: Can buy
Or(||): Returns "true" if value to right or left is "true".
Example:
Truth nextToID=false;
Truth nextToDriverLicense=true;
if(nextToID || nextToDriverLicense) print("Can Log In");
//Output: Can Log In
Not(!): Returns "false" if value to right is true, and "true" if false.
Example:
Truth internetConnected=false;
if(!internetConnected) print("No Connection");
//Output: No Connection