Introduction
Hello Ninjas! Welcome to yet another article on Carbon. You must have learned about the Carbon language. Today we will be discussing Conditional Statements in Carbon. In your daily life, there may be situations where you must have taken decisions by analyzing certain conditions; the technical name for them is Conditional statements.

Sounds interesting? Without any further delay, let’s jump over to understanding Conditional statements in detail.
Conditional Statements
Conditional Statements are used to make decisions based on some conditions. The conditions are some set of boolean expressions, which are based on the true or false result. This condition changes the flow of the execution, and this process is also known as decision-making.
Let’s discuss how to implement conditional statements in Carbon one by one.
If and Else Statements
The if-else statement is also known as the two-way selection statement. It is used to execute the code based on true or false conditions. The following flowchart explains it well.

Example
package ExplorerTest api;
fn Main() -> i32 {
var a: i32 = 1;
var b: i32 = 2;
if(a==b){
Print("a is equal to b");
}
else{
Print("a is not equal to b");
}
return 0;
}
Output

Nested if-else Statements
Sometimes a sequence of conditions is required to make a decision. It can be done using nested if-else statements. If a condition is true, that block of code will execute, and you can add more conditions using another if-else statement inside that block. The nested if executes as the result of the primary if.
Example
package ExplorerTest api;
fn Main() -> i32 {
var a: i32 = 1;
var b: i32 = 2;
if(a==b){
Print("a is equal to b");
}
else{
if(a>b){
Print("a is greater than b");
}
else{
Print("a is less than b");
}
}
return 0;
}
Since a is not equal to b, the execution will go in the else block, and the nested if-else statement will get executed according to the condition.
Output

If-Else If ladder
The sequence of conditions can be added after the if block. One or more else-if clauses can be used to achieve this. If the condition is true, that block will be evaluated. Finally, an optional else block can be added and executed if all the above conditions are false.
Example
package ExplorerTest api;
fn Main() -> i32 {
var a: i32 = 1;
var b: i32 = 2;
if(a==b){
Print("a and b are equal");
}
else if(a>b){
Print("a is greater than b");
}
else if(a<b){
Print("a is less than b");
}
return 0;
}
Output

We should keep in mind that, unlike other languages where curly brackets can be skipped for single-line if statements, it is mandatory in Carbon. To verify this, we can see the example.
package ExplorerTest api;
fn Main() -> i32 {
var a: i32 = 1;
var b: i32 = 2;
if(a==b)
Print("a and b are equal");
else if(a>b){
Print("a is greater than b");
}
else if(a<b){
Print("a is less than b");
}
return 0;
}
It will give the syntax error as shown.
Match Statements
The match statement in carbon is similar to the switch statement in other languages like C++. It is used to compensate for the long else-if ladder if many conditions are used. A match statement consists of more than one case label, which can be tested against the match expression. If an expression matches a case, then the flow is transferred to that block.
Example
package ExplorerTest api;
fn Matcher(var num: i32) -> i32 {
var number: auto = num;
match (number) {
case 1 => {
Print("The number is 1");
return number;
}
case 2 => {
Print("The number is 2");
return number;
}
case 3 => {
Print("The number is 3");
return number;
}
default => {
Print("Default");
return number;
}
}
}
fn Main() -> i32 {
Matcher(3);
Matcher(2);
Matcher(0);
return 0;
}
Output

Note that the match statement is provisional, yet there is no design for it in Carbon.