For Loop in Dart Explained with Real and Practical Examples

in CCS6 days ago

Hello everyone!


for loop.png

Designed with Canva

Today I am here to discuss loops and specifically for loop. All the programming languages use loops because their amazing roles in the code.

What are Loops

Loops play an important role in the programming which runs a specific block of code until a specific condition is met. The loops are help us to reduce the number of lines of code. They eliminate the redundancy of the code.

There are three types of the loops:

  • For Loop
  • While Loop
  • Do-While Loop

For Loop

for loop is used when the number of iterations are known. When we know the limit of iterations we can simply use for loop and it will run the block of code in its body untill all the iterations are completed. It is the most simplest form of the loops.

This is the structure of the for loop:

for(int i = 1; i<=10; i++)

  1. First of all initialization is carried out
  2. Then the condition is defined
  3. Then the increment or decrement operator is used according to the requirements and logic

So this is the simple format of the for loop.

Print 100 numbers using For Loop


image.png

Look at this simplest code of 2 lines which is serving to print 100 numbers. Imagine if we try printing 100 numbers without using loop then we have to write a lot of code more than 100 lines of code but the loop has made it easy.

Write Table of Any Number in Dart Programming

We can use dart programming language to write the tables and for many other purposes. I will teach you how you can write table of any number given by the user just by using for loop and some basic arithmetic operations.


image.png

Here you can see the simple code to generate the table of any number given by the user. I have used for loop. I have started running the program from 1 to 10. There are total iterations in the table. So we know the iterations and I have applied the same condition in the for loop.

Finding Sum and Average Upto n Number in Dart Programming Language


image.png

We can use for loop to find sum upto n numbers and similarly we can find the average of upto n numbers. Here you can see in the code at first I am getting a user input. Then I am running for loop upto n numbers and adding all the number upto n. After that I am finding the average of the numbers.

So in this way we can use for loop in different ways when we know the number of iterations. It is very useful in the programming and you can say the building blocks.

Posted using SteemX

Sort:  

🎉 Congratulations!

Your post has been upvoted by the SteemX Team! 🚀

SteemX is a modern, user-friendly and powerful platform built for the Steem community.

🔗 Visit us: www.steemx.org

✅ Support our work — Vote for our witness: bountyking5

banner.jpg

 6 days ago 

🎉 Congratulations, @mohammadfaisal!

Your post For Loop in Dart Explained with Real and Practical Examples was rewarded by CCS Curation Trail

CCS Logo

“Home is where your heart is ❤️.”

👉 Join the CCS Curation Trail | Community Invitation

Vote Banner

Vote for visionaer3003 as witness