Table of contents
1.
Introduction
2.
Conditional Statements
2.1.
If and Else Statements
2.1.1.
Example
2.1.2.
Output
2.2.
Nested if-else Statements
2.2.1.
Example
2.2.2.
Output
2.3.
If-Else If ladder
2.3.1.
Example
2.3.2.
Output
2.4.
Match Statements
2.4.1.
Example
2.4.2.
Output
3.
Frequently Asked Questions
3.1.
What is Carbon?
3.2.
How are variables declared in Carbon? 
3.3.
What are the different conditional statements in Carbon?
3.4.
Is there any alternative to the if-else statement?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Conditional Statements in Carbon

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

Introduction

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.

If and Else Statements

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

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

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

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.

output

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

output

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

Frequently Asked Questions

What is Carbon?

Carbon is a successor of the C++ language launched by the developers of Google. It is in the experimental stage and used to solve the difficulties of C++ and has advanced features like memory safety, generics, etc.

How are variables declared in Carbon? 

Variables are declared with the var keyword, and is used after the variable names. For example, var x: i32 = 10. I32 specifies the integer type in this example.

What are the different conditional statements in Carbon?

The conditional statements in carbon can be implemented using if-else, nested if-else, if-else if ladder, and match statements. The design for the match statement is still provisional in Carbon.

Is there any alternative to the if-else statement?

An alternative, the if-else statement can be a match statement. It is used to replace the long else-if ladder to simplify the conditions. The match statement is similar to the switch statement in other programming languages.

Conclusion

In this article, we have discussed the conditional statements in carbon. We have looked at different implementations of conditional statements that are if-else, nested if-else, if-else if ladder, and match statements
You can check out our other articles if you want to dive deep into other concepts related to Carbon language -

You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questionsand the Ultimate guide path for interviews. Do upvote our blogs to help other ninjas grow.

Happy Coding !

Live masterclass