Dart language provides a pre-defined data type called boolean which can store two possible values, either true or false. To declare a boolean variable in Dart programming language, the keyword bool is used. Most commonly, boolean is used in decision-making statements.
The syntax for declaring a boolean value is as follows:
Syntax: bool variable_name = true/false;
Example 1:
The following example shows how we can assign boolean values in case of comparison of 2 predefined values.
Dart
main() {
bool check;
int val1=12;
int val2=9;
// Assigning variable check
// value depending on condition
check=(val1>val2);
print(check);
}
Output:
Example 2:
The following example shows how we can use boolean values to check which of the two arguments passed is greater.
Dart
main(List<String> arguments) {
// Taking values of arguments
// inside variables val1 and val2
int val1=int.parse(arguments[0]);
int val2=int.parse(arguments[1]);
bool check;
// Assigning variable check
// value depending on condition
check=(val1>val2);
if(check){
print('First argument is greater');
}else{
print('Second argument is greater or both are equal');
}
}
Suppose we run this dart program named main.dart using :Â
dart main.dart 12 9
Output:
In the above example, as the value inside the first argument (12) is greater than the value of the second argument (9), the value inside the boolean variable check becomes true. Now, as if the condition is true the first statement is printed.Â
Example 3:
The following example shows how we can use boolean values to check which of the passed 2 strings is greater.
Dart
main(List<String> arguments) {
//Taking values of lengths inside variables len1 and len2
int len1=arguments[0].length;
int len2=arguments[1].length;
bool check;
//Assigning variable check value depending on condition
check=(len1>len2);
if(check){
print('First length is greater and its length is $len1');
}else{
print('Second length is greater or equal and its value is $len2');
}
}
Suppose we run this dart program named main.dart using :
dart main.dart GeeksforGeeks Dart
Output:
In the above example, as the length of the first string (13) is greater than the length of the second string (4), the value inside the boolean variable check becomes true. Now, as if the condition is true the first statement is printed.Â
Similar Reads
Boolean Data Type In programming languages, we have various data types to store different types of data. Some of the most used data types are integer, string, float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or False. These values are not case-sensitive depend
13 min read
JavaScript Boolean To represent logical values, JavaScript uses the Boolean data type, which has two possible values: true or false. These values often result from comparisons or logical operations. Additionally, the Boolean() function can convert other types of values into Boolean, determining their truthy or falsy n
4 min read
ES6 Boolean The Boolean of ES6 is nothing different than the other Boolean objects. It also represents two values True or False. There are two properties and three methods that build this object in ES6 JavaScript. Properties of Boolean Object: JavaScript constructor: In ES6 JavaScript, the constructor property
3 min read
Python Boolean Python Boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e. True or False. Generally, it is used to represent the truth values of the expressions.Python Boolean TypeBoolean value can be of two types only i.e. either True or False. The output
7 min read
Dart - Collections Collections are groups of objects that represent a particular element. The dart::collection library is used to implement the Collection in Dart. There are a variety of collections available in Dart.Dart CollectionThere are 5 Interfaces that we have in the Dart Collection, as mentioned below:ListQueu
7 min read
Dart - Comments In every programming language comments play an important role for a better understanding of the code in the future or by any other programmer. Comments are a set of statements that are not meant to be executed by the compiler. They provide proper documentation of the code. Types of Dart CommentsDart
2 min read