Introduction
A programming language has an important aspect of taking decisions in code. Solidity delivers the if…else and switch statements to execute different instructions based on circumstances. This is too significance to loop through multiple items. Solidity provides different constructs such as for loops and while statements.
Solidity expressions
A statement, comprising multiple operands and optionally zero or more operators is refers to expression. That give result in a single value, object, or function. The operand may be a variable,literal, function invocation, or another expression itself.
An example of an expression is as follows:
Age > 10
Age is a variable and 10 is an integer literal in the preceding code.Also age and 10 are operands. The ( > ) symbol greater than is the operator. This expression returns in the preceding code. The age is a variable and 10 is an integer literal. Age and 10 are operands and the ( > ) greater than symbol is the operator. This expression returns a single boolean value. That value may be true or false depending on the value stored in Age . Expressions might be more complex comprising multiple operands and operators, as follows:
((Age > 10) && (Age < 20) ) || ((Age > 40) && (Age < 50) )
There are multiple operators in play in the preceding code. The operator && acts as an AND operator between two expressions that in turn comprises operands and operators. There is also an OR operator represented by the || operator between two complex expressions.a single boolean value ( true or false ) depending on the value stored in Age. Expressions might be more complex comprising multiple operands and operators, as follows:
((Age > 10) && (Age < 20) ) || ((Age > 40) && (Age < 50) )
There are multiple operators in play in the preceding code. The operator && acts as an AND operator between two expressions that in turn comprises operands and operators. There is also an OR operator represented by the || operator between two complex expressions.
The if decision control
Solidity offers conditional code execution with the help of the if…else instructions.
The general structure of if…else is as follows:
if (this condition/expression is true) {
Execute the instructions here
}
else if (this condition/expression is true) {
Execute the instructions here
}
else {
Execute the instructions here
}
if and if-else are those keywords in Solidity, which inform the compiler that they contain a decision control condition, for example, if (a > 10) . Here, if contains a condition that may evaluate to either true or false .The code instructions that follow in the pair of double-brackets ( { ) and ( } ) should be executed,if a > 10 evaluates to true.Also else is a keyword that gives an alternate path if none of the previous conditions are true. It too keeps a decision control instruction and executes the code instructions if a > 10 tends to be true . The following example describes the use of ‘IF’-‘ELSE IF’ – ‘ELSE’ conditions. An enum with multiple constants is declared. A State Management function accepts an uint8 argument, which is converted into an enum constant and compared within the if…else decision control structure. If the value is 1 then the returned result is 1 ; if the argument contains 2 or 3 as value, then the else…if portion of code gets executed; and if the value is other than 1 , 2 , or 3 then the else part is executed.
The while loop
We need to execute a code segment repeatedly based on a condition. Solidity gives while loops precisely. The common form of the while loop is as follows:
Declare and initialize a counter while we have to check the value of counter using an expression or condition) { Execute the instructions here Increment the value of counter }
while is that keyword in Solidity which informs the compiler that it contains a decision control instruction. If this expression evaluates to true then the code instructions that follow in the pair of double-brackets { and } should be executed. The while loop remains executing until the condition turns false. In the following example, mapping is declared along with counter . The counter provide help to loop the mapping since there is no out-of-the-box support in Solidity to loop mapping. It is enough to understand that we are logging information whenever an event is invoked. The SetNumber function adds data to mapping and the getnumbers function runs a while loop to retrieve all entries within the mapping and log them using events. A temporary variable is used as a counter that is incremented by 1 at every execution of the while loop.
For more details visit:https://www.technologiesinindustry4.com/2021/01/what-are-solidity-expressions.html