Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In today’s advancing era, many programming languages are emerging with different functionality and add-ons. Carbon is termed the experimental successor of C++. So, Ninja, if you’re a C++ or C programmer, it would be comparatively easy to learn Carbon. In this article, you will learn about the jump statements in Carbon.
What is Carbon
Carbon is the general-purpose, open-source, and experimental programming language launched by Google in Toronto in July 2022. For existing C++ developers and codebases, it is designed to be both interoperable and scalable. Just like Typescript serves the additional purpose of JavaScript, Kotlin serves some other functions to Java. Similarly, Carbon serves some additional functionality to C++.
Jump Statements in Carbon
As the name suggests, jump statements are those which help in changing the flow of the program based on some conditions. Jump statements in Carbon are also termed as the control flow statements as they determine the flow of the program, i.e., whether it has to terminate or continue the loop or stop the execution of the function.
Carbon provides us with four jump statements - continue, break, return, and returned var- the reserved words (keywords) that mean they cannot be used as variables. So, let’s learn about them.
continue
continue is used in the loops. This jump statement skips some parts of the loop based on the conditions specified and continues to the next iteration after encountering the continue. It does not stop the iteration, instead jumps off from the specified iterations.
Syntax:
continue;
It would be more transparent with the following example -
You’ll be more clear from the output we received. The continue statement is present in the if block of the while loop. The loop keeps on iterating for 0,1,2,3 and 4 values of x, but when x got the value as 5, it entered the if block where the continue statement is there, due to which 5 gets skipped and further iterations are continued. Therefore, there is no 5 in the output.
break
Similar to continue, the break statement also executes in the loops. From the name itself, it is signified that it breaks the flow of the iteration. Like continue, break statements do not skip particular iterations. Instead, it ultimately terminates the loop after getting break statement in the loop.
Syntax:
break;
Let’s see what the output of the above code will be if the break statementis used instead of continue.
As discussed above, the break statement stops further iterations. It is evident from the output that the iteration ceases when the condition x==5 is satisfied because it gets the break statement in the if block.
return
return statement is used in the functions, which are the block of code executed only when they are called. It takes the flow out of the function in the program and terminates the function being executed. It returns some value, string, etc., and returns the execution to the caller of the function. The return statement can be omitted if the return is empty or void.
Syntax:
return;
Code:
// Program showing whether the input number is negative or positive
package sample api;
fn NegPos(i: i32) -> i32 {
if (i > 0) {
return 1; //further code won't be executed if the condition satisfies
}
if (i < 0) {
return -1; //it terminates the function's execution
}
return 0;
}
fn Main() ->i32{
var x: i32 = NegPos(-3); //function call and storing return value of function
if(x == -1){
Print("Negative");
}
else{
Print("Positive");
}
return 0;
}
Output
In this example, an integer is passed to the function NegPos() as its parameter. The function tells whether the integer is positive or negative (excluding zero). Thus, input -3 gave the output as Negative, and the execution of the function stopped after encountering the return statement.
returned var
This jump statement can be taken as an extension of the return statement. Just like return, returned var also terminates the execution of the function and returns the flow and value (if any) to the ]function’s caller. But the difference is, you can avoid copying of a variable while returning it by adding areturned prefix to the variable's declaration and using return var instead of return expression.
Syntax:
returned var variableDeclaration;
.
.
return var;
Example Code:
package sample api;
fn RectArea(length: i32, breadth: i32) -> i32 {
returned var area: i32;
area = length*breadth;
// Here the return area would be invalid because 'returned' is used
return var;
}
fn Main() -> i32{
Print("Area of rectangle(in units^2) is: {0}",RectArea(4,5));
return 0;
}
Output
In the above example, the area of a rectangle is calculated by passing the length and breadth as arguments to the function RectArea(). It uses returned var; thus, it avoids copying the variable while returning. It simply returns the var using the return var statement.
Congratulations Ninja! You learned the concept of jump statements in the emerging language - Carbon.
Frequently Asked Questions
Why is Carbon termed the successor of C++?
Carbon contains all the functionality of C++ and overcomes the shortcomings of C++.
What is the need for Carbon?
Carbon is designed to support fast and scalable development, performance-critical software, modern OS platforms, hardware architectures, interaction with and migration from the existing C++ code, and the software and language evolution.
What is the difference between return and returned var?
returned var avoids making a copy of the variable to be returned, which is not the case with the return statement.
What is the main difference between break and continue?
The break statement terminates the further iteration of the loop, whereas continue skips a particular iteration based on where it is used.
What is the difference between Carbon and C++?
Carbon contains all the functionality of C++, but the inverse is invalid. The declarations start with the introducer keywords example, functions with fn, and variables with var. Imports in Carbon are more like the C++ modules.
Conclusion
In this article, you learned about jump statements in Carbon and types of jump statements. ‘break’, ‘continue’, ‘return’, and ‘returned var’ are the four jump statements in Carbon that control the flow of the program after meeting some condition.
It is the time to practice and learn more about it. So, do read the following related articles:
If you find this article helpful, you can read many useful articles and practice coding questions in multiple languages on our platform Code Studio. Coding Ninjas provides the interview bundle, interview experiences, and a guided pathway to crack good companies at a good package. So, keep practicing and upskilling with us.