0% found this document useful (0 votes)
2 views

Conditional_Statements

The document explains conditional statements in programming, specifically in Solidity, which allow for decision-making based on specified conditions. It covers three types: 'if', 'if-else', and 'if-else-if', providing syntax and examples for each type to illustrate their usage in controlling code execution. The examples demonstrate how to evaluate conditions and execute different code blocks accordingly.

Uploaded by

Riya Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Conditional_Statements

The document explains conditional statements in programming, specifically in Solidity, which allow for decision-making based on specified conditions. It covers three types: 'if', 'if-else', and 'if-else-if', providing syntax and examples for each type to illustrate their usage in controlling code execution. The examples demonstrate how to evaluate conditions and execute different code blocks accordingly.

Uploaded by

Riya Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Conditional Statements

Conditional statements act like decision-making constructs that allow you to


execute different blocks of code based on certain conditions. They enable you
to control the flow of execution within your program by evaluating whether a
condition is true or false, and then executing specific code accordingly.
They are of following types:
a. if
b. if-else
c. If-Else-If Statement

a. if Statement: The if statement in Solidity is used to execute a block of code


if a specified condition is true.

Syntax:

Syntax: if (condition)
{ // code to execute }

Example:

// Check if a number is positive


uint256 number = 10;
if (number > 0){
// Number is positive
// Code to execute
}

This example checks if a given number is positive. If the number is greater


than 0, it executes the code block inside the if statement.

1|Page
Explanation:

• uint256 is an unsigned integer type in Solidity, which means it can hold


non-negative whole numbers. The 256 refers to the number of bits used
to store the number, allowing for a very large range of values.
• number is the name of the variable.
• 10 is the value assigned to the variable number.
• This line checks if the value of the number is greater than 0.
• If the condition number > 0 evaluates to true, the code inside the curly
braces {} will be executed.
• // Number is positive is a comment explaining that if the condition
number > 0 is true, it means the number is positive.
• // Code to execute is another comment indicating where you would place
the code that you want to execute if the number is indeed positive.

b. if...else: This is the fundamental conditional statement, checking a single condition


and executing different code blocks based on the outcome (true or false).

Syntax:

if (condition) {
// code to execute if condition is true
}
else {
// code to execute if condition is false
}

Example 1:

uint age = 18;


if (age >= 18) {
// Grant voting rights
(code for eligible voters)
}
else {
// Deny voting rights (code for ineligible voters)
}

2|Page
This example checks if the age is 18 or above. If true, it grants voting rights;
otherwise, it denies them.

Example 2:

// Check if a number is even or odd uint256 number = 11;


if (number % 2 == 0)
{ // Number is even // Code to execute
}
else {
// Number is odd
// Code to execute
}

This example determines whether a given number is even or odd. If the


number is divisible by 2 with no remainder, it executes the block inside the if
statement. Otherwise, it executes the block inside the else statement.

Explanation:

● We declare a variable number and assign it the value 11.


● Condition Check: The condition number % 2 == 0 is evaluated. Here, % is
the modulo operator which returns the remainder when the number is
divided by. In this case, 11 % 2 results in 1, so the condition number % 2 ==
0 evaluates to false.
● Execution: Since the condition in the if statement is false, the execution
proceeds to the else block.
● Execution of else Block: Inside the else block, the comment indicates that
the number is odd. This block is executed, and any code within it would
be executed as well.

c. If-Else-If Statement: This allows you to chain multiple conditions and


execute corresponding code blocks sequentially. It's useful when there are
more than two conditions to check and more than two possible outcomes.

3|Page
Syntax :

if (condition1) {
// code to execute if condition 1 is true
} else if
(condition2) {
// code to execute if condition 2 is true
}
else {
// code to execute if both condition1 and condition2 are
false
}

Example 1: Age verification with additional citizenship check for voting

uint age = 20;


bool isCitizen = true;
if (age >= 18) {
if (isCitizen) {
// Grant voting rights (eligible voter who is a citizen)
} else {
// Deny voting rights (eligible voter but not a citizen)
}
} else {
// Deny voting rights (underage)
}

This code first checks if the user is 18 or older. If true, it then checks their
citizenship status. Only if both conditions are met are voting rights granted.

Example 2: Ticket Pricing

// Ticket Pricing
uint256 age = 25;
uint256 ticketPrice;

4|Page
if (age <= 5) {
ticketPrice = 0; // Free for children under 5
} else if (age <= 12) {
ticketPrice = 10; // Child ticket price
} else if (age <= 65) {
ticketPrice = 20; // Adult ticket price
} else {
ticketPrice = 15; // Senior citizen discount
}

This example calculates the ticket price based on age categories using
if..else..if statements.

5|Page

You might also like