Switch Case Statement

3. Control Flow Statement

Dart Switch case statement is used to avoid the long chain of the if-else statement. It is the simplified form of nested if-else statement. The value of the variable compares with the multiple cases, and if a match is found, then it executes a block of statement associated with that particular case. The assigned value is compared with each case until the match is found. Once the match found, it identifies the block of code to be executed. Dart Switch Case Statement Flowchart The syntax is given below. Syntax: Here, the expression can be integer expression or character expression. The value 1, 2, n represents the case labels and they are used to identify each case particularly. Each label must be ended with the colon(:). The labels must be unique because same name label will create the problem while running the program. A block is associated with the case label. Block is nothing but a group of multiple statements for a particular case. Once the switch expression is evaluated, the expression value is compared with all cases which we have defined inside the switch case. Suppose the value of the expression is 2, then compared with each case until it found the label 2 in the program. The break statement is essential to use at the end of each case. If we do not put the break statement, then even the specific case is found, it will execute all the cases until the program end is reached. The break keyword is used to declare the break statement. Sometimes the value of the expression is not matched with any of the cases; then the default case will be executed. It is optional to write in the program. Let’s understand the following example. Output: Explanation – In the above program, we have initialized the variable n with value 3. We constructed the switch case with the expression, which is used to compare the each case with the variable n. Since the value is 3 then it will execute the case-label 3. If found successfully case-label 3, and printed the result on the screen. Let’s have a look at another scenario. Example – Output: Explanation – In the above program, we have initialized the variable Roll_num with the value of 90014. The switch test-expression checked all cases which are declared inside the switch statement. The test-expression did not found the match in cases; then it printed the default case statement. Benefit of Switch case As we discussed above, the switch case is a simplified form of if nested if-else statement. The problem with the nested if-else, it creates complexity in the program when the multiple paths increase. The switch case reduces the complexity of the program. It enhances the readability of the program.

October 12, 2024 / 0 Comments
read more

if else-if Statement

3. Control Flow Statement

Dart if else-if statement provides the facility to check a set of test expressions and execute the different statements. It is used when we have to make a decision from more than two possibilities. Dart if else if Statement Flow Diagram Syntax Here, this type of structure is also known as the else….if ladder. The condition is evaluated from top to bottom. Whenever it found the true condition, statement associated with that condition is executed. When all the given condition evaluates false, then the else block is executed. Let’s understand the following example. Example – Write a program to print the result based on the student’s marks. Output: Explanation – The above program prints the result based on the marks scored in the test. We have used if else if to print the result. We have initialized the marks variable with the integer value 74. We have checked the multiple conditions in the program. The marks will be checked with the first condition since it is false, and then it moved to check the second condition. It compared with the second condition and found true, then it printed the output on the screen. This process will continue until all expression is evaluated; otherwise the control will transfer out of the else if ladder and default statement is printed. You should modify the above value and notice the result. Nested If else Statement Dart nested if else statement means one if-else inside another one. It is beneficial when we need a series of decisions. Let’s understand the following example. Example – Write a program to find the greatest number. Output: In the above program, we have declared three variables a, b, and c with the values 10, 20, and 30. In the outer if-else we provided the condition it checks if a is greater than b. If the condition is true then it will execute the inner block otherwise the outer block will be executed. In the inner block we have another condition that checks if variable a is greater than c. If the condition is evaluated true then the inner block will be executed. Our program returned the false in first condition, and then it skipped the inner block check another condition. If satisfied the condition and printed the output on the screen.

October 12, 2024 / 0 Comments
read more

Dart if Statements

3. Control Flow Statement

If statement allows us to a block of code execute when the given condition returns true. In Dart programming, we have a scenario where we want to execute a block of code when it satisfies the given condition. The condition evaluates Boolean values TRUE or FALSE and the decision is made based on these Boolean values. Dart If Statement Flow Diagram The syntax of if statement is given below. Syntax – The given condition is if statement will evaluate either TRUE or FALSE, if it evaluates true then statement inside if body is executed, if it evaluates false then statement outside if block is executed. Let’s understand the following example. Example – 1 Output: Explanation – In the above program, we declared an integer variable n. We specified the condition in if statement. Is the given number is smaller than 40 or not? The if statement evaluated the true, it executed the if body and printed the result. Example – 2 Output: In the above program, we can see that the if condition evaluated the false then execution skipped the if body and executed the outside statement of if block.

October 12, 2024 / 0 Comments
read more

Control Flow Statement

3. Control Flow Statement

The control statements or flow of control statements are used to control the flow of Dart program. These statements are very important in any programming languages to decide whether other statement will be executed or not. The code statement generally runs in the sequential manner. We may require executing or skipping some group of statements based on the given condition, jumps to another statement, or repeat the execution of the statements. In Dart, control statement allows to smooth flow of the program. By using the control flow statements, a Dart program can be altered, redirected, or repeated based on the application logic. Categories of Flow Statement In Dart, Control flow statement can be categorized mainly in three following ways. Dart Decision-Making Statements The Decision-making statements allow us to determine which statement to execute based on the test expression at runtime. Decision-making statements are also known as the Selection statements. In Dart program, single or multiple test expression (or condition) can be existed, which evaluates Boolean TRUE and FALSE. These results of the expression/condition helps to decide which block of statement (s) will execute if the given condition is TRUE or FALSE. Dart provides following types of Decision-making statement. Dart Looping Statements Dart looping statements are used to execute the block of code multiple-times for the given number of time until it matches the given condition. These statements are also called Iteration statement. Dart provides following types of the looping statements. Dart Jump Statements Jump statements are used to jump from another statement, or we can say that it transfers the execution to another statement from the current statement. Dart provides following types of jump statements – The above jump statements behave differently.

October 11, 2024 / 0 Comments
read more

Enumeratio

2. Data Types

An enumeration is a set of values called as elements, members, etc. This is essential when we carried out the operation with the limited set of values available for variable. For example – you can think of the days of the month can only be one of the seven days – Sun, Mon, Tue, Wed, Thur, Fri, Sat. Initializing an Enumeration The enumeration is declared using the enum keyword, followed by the comma-separated list of the valid identifiers. This list is enclosed within the curly braces {}. The syntax is given below. Syntax – Here, the enum_name denotes the enum type name and list of the identifiers enclosed within the curly bracket. Each of the identifier in the enumeration list has its index position. The index of the first enumeration is 0; the second enumeration is 1, and so on. Example – Let’s define an enumeration for months of the year. Let’s say programming example – Example – Output: Example – 2 Output:

October 11, 2024 / 0 Comments
read more

Runes

2. Data Types

As we discussed earlier, Dart String is a sequence of characters, letters, numbers, and unique characters. It is the sequence of UTF – 16 Unicode characters where Dart Runes are the sequence UTF – 32 Unicode code points. It is a UTF-32 string which is used to print the special symbol. For example – The theta (Θ) symbol is signified by using the corresponding Unicode equivalent \u0398; here ‘\u’ refers to Unicode, and the numbers are in the hexadecimal. Sometimes the hex digits are the more than 4 digits then it should be placed in curly brackets ({}). Let’s understand it by the following example. Example – Output: The Dart provides the dart: core library which has the Dart Runes. The String code unit can be retrieved in the following three methods. String.codeUnitAt() Method We can access the character’s code unit in the given string by using the codeUnitAt() method. It accepts the index position as an argument and returns the 16-bit UTF-16 code unit at the passed index position of the string. The syntax is the given below. Syntax – Output: Explanation – In the above code, the variable str holds string value “JavaTpoint”. We called the codeuUnitAt() function and passed index position. It returned the code unit of 0th index character. String.codeUnits Property The codeUnits property returns UTF-16 code units for given string in the form of a list. The syntax is given below. Syntax – Let’s have a look at following example – Example – Output: Explanation – The codeUnits returned the list of the code unit corresponding to the given character. String.runes Property The runes property is used to iterate the given string though the UTF-16 code unit. The Syntax is given below. Syntax – Consider the following example. Example – Output:

October 11, 2024 / 0 Comments
read more

Symbol

2. Data Types

Symbol object is used to specify an operator or identifier declared in a Dart programming language. Generally, we do not need to use symbols while Dart programming, but they are helpful for APIs. It usually refers to identifiers by name, because identifier names can vary but not identifier symbols. Dart symbols are dynamic string name that is used to derive the metadata from a library. It mainly accumulates the connection between human-readable strings that are enhanced to be used by computers. Symbols have a term which is called Reflection; it is a technique that used to the metadata of at run-time, for example – the number of methods used in class, a number of constructors in a class, or numbers of arguments in a function. The dart:mirrors library has all of the reflection related classes. It can be used with the command-line applications as well as web applications. Syntax – The hash(#) symbol, followed by the name is used to define Symbol in Dart. The syntax is given below. Syntax – Here, the valid identifier such as function, valid class, public member name, or library name can be used in place of name value. Let’s understand the following example. Example – In the above code, we have declared a class Foo in a library foo_lib. The class contains the methods m1, m2, and m3. We save the above file as foo.dart. Now, we are creating new file FooSymbol.dart and run the following code. FoolSystem.dart Output: Dart Convert Symbol to String We can convert the Dart symbol into the string by using a built-in class MirrorClass, which is provided by the dart:mirror package. Let’s understand the following example. Example – Output:

October 11, 2024 / 0 Comments
read more

Map

2. Data Types

Dart Map is an object that stores data in the form of a key-value pair. Each value is associated with its key, and it is used to access its corresponding value. Both keys and values can be any type. In Dart Map, each key must be unique, but the same value can occur multiple times. The Map representation is quite similar to Python Dictionary. The Map can be declared by using curly braces {} ,and each key-value pair is separated by the commas(,). The value of the key can be accessed by using a square bracket([]). Declaring a Dart Map Dart Map can be defined in two methods. The syntax of declaring Dart Map is given below. Using Map Literals To declare a Map using map literal, the key-value pairs are enclosed within the curly braces “{}” and separated by the commas. The syntax is given below. Syntax – Example – 1: Output: Example – 2: Adding value at runtime Output: Explanation – In the above example, we declared a Map of a student name. We added the value at runtime by using a square bracket and passed the new key as a course associated with its value. Using Map Constructor To declare the Dart Map using map constructor can be done in two ways. First, declare a map using map() constructor. Second, initialize the map. The syntax is given below. Syntax – After that, initialize the values. Example – 1: Map constructor Output: Note – A map value can be any object including NULL. Map Properties The dart:core:package has Map class which defines following properties. Properties Explanation Keys It is used to get all keys as an iterable object. values It is used to get all values as an iterable object. Length It returns the length of the Map object. isEmpty If the Map object contains no value, it returns true. isNotEmpty If the Map object contains at least one value, it returns true. Example – Output: Map Methods The commonly used methods are given below. addAll() – It adds multiple key-value pairs of other. The syntax is given below. Syntax – Parameter: Let’s understand the following example. Example – Output: remove() – It eliminates all pairs from the map. The syntax is given below. Syntax – Let’s have a look at following example. Example – Output: remove() – It removes the key and its associated value if it exists in the given map. The syntax is given below. Syntax – Parameter – Let’s understand the following example. Example – Output: forEach() – It is used to iterate the Map’s entries. The syntax is given below. Syntax – Output:

October 11, 2024 / 0 Comments
read more

Sets

2. Data Types

The Dart Set is the unordered collection of the different values of the same type. It has much functionality, which is the same as an array, but it is unordered. Set doesn’t allow storing the duplicate values. The set must contain unique values. It plays an essential role when we want to store the distinct data of the same type into the single variable. Once we declare the type of the Set, then we can have an only value of the same type. The set cannot keep the order of the elements. Dart Initializing Set Dart provides the two methods to declare/initialize an empty set. The set can be declared by using the {} curly braces proceeded by a type argument, or declare the variable type Set with curly braces {}. The syntax of declaring set is given below. Syntax – The setname refers to the name of the set variable, and type refers to the data type of the set. Note – It should be remembered that the syntax of the set is much similar to the map literals. If we forget to define the type annotation with {} or with the variable it’s assigned to; then, Dart compiler will create Map object instead of Set.Let’s have a look at the following example of set declaration –Example –void main(){     print(“Initializing the Set”);     var names = <String>{“James”,”Ricky”, “Devansh”,”Adam”};     print(names);  }  Output:Initializing the Set {James, Ricky, Devansh, Adam} Add Element into Set The Dart provides the two methods add() and addAll() to insert an element into the given set. The add() method is used to add the single item into the given set. It can add one at a time when the addAll() method is used to add the multiple elements to an existing set. The syntax is given below. Syntax: Consider the following example – Example – Output: Explanation – We have declared two sets of names and emp. The set names consisted of few elements, while emp is an empty set. We added the single element “Jonathan” by using the add() method then; we called the addAll() method and passed another set names as an argument. It added the multiple values to the emp set. Access the Set Element Dart provides the elementAt() method, which is used to access the item by passing its specified index position. The set indexing starts from the 0 and goes up to size – 1, where size is the number of the element exist in the Set. It will throw an error if we enter the bigger index number than its size. The syntax is given below. Syntax: Consider the following example. Example – Output: Explanation – In the above example, we have set names. We applied the elementAt() method and passed index position 3 as an argument. We created a variable x, which holds the assessed value, and then we printed the result. Dart Finding Element in Set Dart provides the contains() method, which is used to find an element in the set. It accepts the single item as an argument ad return the result in Boolean type. If the given element present in the set, it returns true otherwise false. The syntax is given below. Syntax: Example – Output: Explanation – In the above program, to find the element in the given set, we called contains() method and passed value “Ricky” as an argument. We used the conditional statement to find out whether an element belongs to the given set or not. The given element present in the set then condition became true, it printed if block statement. Note – We will learn conditional statement in the next section. Dart Remove Set Element The remove() method is used to eliminate or remove an element from the given set. It takes the value as an argument; the value is to be removed in the given set. The syntax is given below. Syntax – Example – Output: Explanation – In the above program, we removed the “Peter” from the given set by using the remove() method. It returned the newly modified set object. Dart Iterating Over a Set Element In Dart, the set element can be iterated using the forEach method as following – Example – Output: Dart Remove All Set Element We can remove entire set element by using the clear() methods. It deletes or removes all elements to the given set and returns an empty set. The syntax is as follow- Syntax – Example – Output: TypeCast Set to List The Set object can convert into the List Object using the toList() method. The syntax is as follows. Note – The type of List must be the same as the type of Set. Syntax – Dart Set Operations Dart Set provides the facility to perform following set operations. These operations are given below. Union – The union is set to combine the value of the two given sets a and b. Intersection – The intersection of the two set a and b returns all elements, which is common in both sets. Subtracting – The subtracting of two sets a and b (a-b) is the element of set b is not present in the set a. Let’s understand the following example. Example – Output: Dart Set Properties The few properties of the Dart set as follows. Properties Explanations first It is used to get the first element in the given set. isEmpty If the set does not contain any element, it returns true. isNotEmpty If the set contains at least one element, it returns true length It returns the length of the given set. last It is used to get the last element in the given set. hashcode It is used to get the hash code for the corresponding object. Single It is used to check whether a set contains only one element.

October 11, 2024 / 0 Comments
read more

Lists

2. Data Types

Dart List is similar to an array, which is the ordered collection of the objects. The array is the most popular and commonly used collection in any other programming language. The Dart list looks like the JavaScript array literals. The syntax of declaring the list is given below. The Dart list is defined by storing all elements inside the square bracket ([]) and separated by commas (,). Let’s understand the graphical representation of the list – list1 – It is the list variable that refers to the list object. Index – Each element has its index number that tells the element position in the list. The index number is used to access the particular element from the list, such as list_name[index]. The list indexing starts from 0 to length-1 where length denotes the numbers of the element present in the list. For example, – The length of the above list is 4. Elements – The List elements refers to the actual value or dart object stored in the given list. Types of Lists The Dart list can be categorized into two types – Fixed Length List The fixed-length lists are defined with the specified length. We cannot change the size at runtime. The syntax is given below. Syntax – Create the list of fixed-size The above syntax is used to create the list of the fixed size. We cannot add or delete an element at runtime. It will throw an exception if any try to modify its size. The syntax of initializing the fixed-size list element is given below. Syntax – Initialize the fixed size list element Let’s understand the following example. Example – Output: Explaination – In the above example, we have created a variable list1 that refers the list of fixed size. The size of the list is five and we inserted the elements corresponding to its index position where 0th index holds 10, 1st index holds 12, and so on. Growable List The list is declared without specifying size is known as a Growable list. The size of the Growable list can be modified at the runtime. The syntax of the declaring Growable list is given below. Syntax – Declaring a List Syntax – Initializing a List Consider the following example – Example – 1 Output: In the following example, we are creating a list using the empty list or List() constructor. The add() method is used to add element dynamically in the given list. Example – 2 Output: List Properties Below are the properties of the list. Property Description first It returns the first element case. isEmpty It returns true if the list is empty. isNotEmpty It returns true if the list has at least one element. length It returns the length of the list. last It returns the last element of the list. reversed It returns a list in reverse order. Single It checks if the list has only one element and returns it. Inserting Element into List Dart provides four methods which are used to insert the elements into the lists. These methods are given below. The add() Method This method is used to insert the specified value at the end of the list. It can add one element at a time and returns the modified list object. Let’s understand the following example – Syntax – Example – Output: Explanation – In the above example, we have a list named odd_list, which holds odd numbers. We inserted a new element 11 using add() function. The add() function appended the element at the end of the list and returned the modified list. The addAll() Method This method is used to insert the multiple values to the given list. Each value is separated by the commas and enclosed with a square bracket ([]). The syntax is given below. Syntax – Let’s understand the following example – Output: Explaination – In the above example, we don’t need to call the add() function multiple times. The addAll() appended the multiple values at once and returned the modified list object. The insert() Method The insert() method provides the facility to insert an element at specified index position. We can specify the index position for the value to be inserted in the list. The syntax is given below. Let’s understand the following example – Output: Explanation – In the above example, we have a list of the random numbers. We called the insert() function and passed the index 2nd value 10 as an argument. It appended the value at the 2nd index and returned the modified list object. The insertAll() Method The insertAll() function is used to insert the multiple value at the specified index position. It accepts index position and list of values as an argument. The syntax is given below. Syntax – Let’s understand the following example – Example – Output: Explanation – In the above example, we have appended the list of values at the 0th index position using the insertAll() function. It returned the modified list object. Updating List The Dart provides the facility to update the list and we can modify the list by simply accessing its element and assign it a new value. The syntax is given below. Syntax – Let’s understand the following example – Example – Output: Explanation – In the above example, we have accessed the 3rd index and assigned the new value 55 then printed the result. The previous list updated with the new value 55. replaceRange() – The Dart provides replaceRange() function which is used to update within the given range of list items. It updates the value of the elements with the specified range. The syntax is given below. Syntax – Let’s understand the following example – Example – Output: Explanation – In the above example, we called the replaceRange() to the list which accepts the three arguments. We passed the starting index 0th, end index 4 and the list of the elements to be replaced as a third arguments. It returned the new list with the replaced element from the given range. Removing List Elements The Dart provides following functions to remove the list elements. The remove() Method It removes one element at a time from the given list. It accepts element as an argument. It removes the

October 11, 2024 / 0 Comments
read more

Posts pagination

Previous 1 … 116 117 118 … 445 Next