Conditional Operators
This class of operators contain those operators which are used to perform comparison on the operands. It goes like this:
Operator Symbol | Operator Name | Operator Description |
---|---|---|
condition ? expersion1 : expersion2 | Conditional Operator | It is a simple version of if-else statement. If the condition is true than expersion1 is executed else expersion2 is executed. |
expersion1 ?? expersion2 | Conditional Operator | If expersion1 is non-null returns its value else returns expression2 value. |
Example: Using Conditional Operators in the program
void main()
{
int a = 5;
// Conditional Statement
var c = (a < 10) ? "Statement is Correct, Geek" : "Statement is Wrong, Geek";
print(c);
// Conditional statement
int? n;
// Warning: Operand of null-aware operation '??' has type 'int' which excludes null.
// For batter practice make both same type to avoid warning
// var d = n ?? 10;
var d = n ?? "n has Null value";
print(d);
// After assigning value to n
n = 10;
// we make it all ready null safe
//d = n ? ? "n has Null value";
d = n;
print(d);
}
Output:
Statement is Correct, Geek
n has Null value
10
Note: Also In The Above Code,You May Notice That The Variable ‘n’ is Declared As “int? n”.By declaring n as int?, you are indicating that the variable n can hold an integer value or a null value. The ? denotes that the variable is nullable, meaning it can be assigned a null value in addition to integer values.
Operators in Dart
The operators are special symbols that are used to carry out certain operations on the operands. The Dart has numerous built-in operators which can be used to carry out different functions, for example, ‘+’ is used to add two operands. Operators are meant to carry operations on one or two operands.
Contact Us