Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
What are Loops?
3.
For Loops in Carbon Language
3.1.
Syntax
3.2.
Example
3.3.
Output
4.
Flowchart of For Loop
5.
While Loops in Carbon Language
5.1.
Syntax
5.2.
Example
6.
Flowchart of While Loop
7.
Break Statement in Loops
7.1.
Example
8.
Continue Statement in Loops
8.1.
Syntax
8.2.
Example
9.
Frequently Asked Questions
9.1.
Why loops are important?
9.2.
What is looping statement?
9.3.
Why continue statement is important?
9.4.
When can we use break statement?
9.5.
What is the use of while loop?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

Loop statements in Carbon

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction 

Have you ever thought about how loops can help in saving time while coding? How loops are useful in repeating tasks till a specific condition? 

Introduction to Loop Statements

In this article, we will discuss about writing loop statements in Carbon Language. We will discuss how using loops, we can write various logics with few lines of code only. We will also discuss how we can use break and continue statements in loops. Moving forward, let’s first understand what exactly loops are.

What are Loops?

To perform certain logic while doing programming, we often have to repeat certain tasks in a sequence, so it is not a good practice to write the same piece of code again and again in coding, so we use loops in such cases in order to write reasonable solutions. In this article, we will discuss the below loops in carbon language.

  • for loop
  • While loop           
About Loops
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

For Loops in Carbon Language

Whenever we know the exact number of times a task has to be completed, then, in such cases, we can use for loop. They are used to perform a fixed number of iterations while doing programming, for loops are range-based loops.

Syntax

for (condition) {

 //code

 } 

Example

package sample api;

fn Main() -> i32 {
var arr: [i32;3] = (1,2,3);
for (i:i32 in arr) 
{
   Print("{0}",i);
}
 return 0;
}

Output

Output screen of for loop

Flowchart of For Loop

When for loop starts, it checks for the terminating condition. If it satisfies the terminating condition, then the loop will end, but if the condition is not met, then it again executes the statements of the loop, makes the updation and checks for the terminating condition again.

Flowchart of For Loop

 

While Loops in Carbon Language

Whenever we want to perform a certain task till a specific condition in programming, then in such cases, we can use while loops. They are used to perform a task for the unknown number of times till a specific condition.

Syntax

while (condition) { 

//code

}

Example

package sample api;

fn Main() -> i32 {
var i:i32 =1;
while (i<5) {
   Print("{0}",i);
   i = i+1;
 }
 return 0;
}

Output

Output screen of while loop

Flowchart of While Loop

When a while loop starts, it checks for a condition. If the condition is true, then it executes the line of code written in the while loop until the statement becomes false. When the condition becomes false, then the loop will end.

Flowchart of while loop


In loops, there are two more main statements that we can use:

  • break statement
  • continue statement

Break Statement in Loops

Whenever we want to terminate the loop after a certain condition, then in such cases, we can use a break statement. They are used for exiting from a loop.

Syntax

break;

Example

package sample api;

fn Main() -> i32 {
var i: i32 = 1;
while (i < 5) {
   if(i == 3) {
     break;
   }
  Print("{0}",i);
  i = i+1;
}
 return 0;
}

Output

Out

Continue Statement in Loops

Whenever in a program we want to skip an iteration, then in such cases, we can use a continue statement. They are used to skip the current iteration. 

Syntax

continue;

Example

package sample api;

fn Main() -> i32 {
var i: i32 = 1;
while (i < 7) {
   if(i == 4) {
   i = i+1;
   continue;
}
  Print("{0}",i);
  i = i+1;
}
  return 0;
}

Output

Output screen with continue statement

Frequently Asked Questions

Why loops are important?

Loops play an important role in programming. It helps in performing repeated tasks with few lines of code.

What is looping statement?

Looping statements are the sequence of repeated statements that are executed to satisfy conditions in programming.

Why continue statement is important?

In programming continue statement is used to go to the next statement instantly. For example, if we want to read a file using file handling, then we can simply use continue statement to skip empty lines in that file.

When can we use break statement?

We can use break statement when we want to end the execution of a loop at a specific condition.

What is the use of while loop?

While loops are really helpful when we want to execute a certain task until the specified condition is not false.

Conclusion

In this article, we have learned about using for loop and while loop statements in carbon language. We have also discussed how we can use a break and continue statements inside loops. If you want to know more, then you can refer to the below-mentioned article on Coding Ninjas Studio.


I hope this article has helped you in learning about loop statements in Carbon language. Coding Ninjas keeps providing you with such content so that you can learn and grow. In case you want to read more articles, then you can visit our platform, Coding Ninjas Studio. There are many articles on almost every technical topic which you search for, it will help you in growing your skills by making you understand the topic very well. You can also practice coding on our platform to keep the learning process healthy.

Happy Coding!

Previous article
Conditional Statements in Carbon
Next article
Jump Statements in Carbon
Live masterclass