Let's learn forr Loop

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 3834 red flag
Rating: 5 out of 5  
 1 vote(s)

Yes it's not a spelling mistake but forr loop in C#.It's the reverse for loop. In this article, we will look into that.
Recommendation
Read Using Yield Statement for finding Factorial of a number before this article.

Introduction

Yes it's not a spelling mistake but forr loop in C#.It's the reverse for loop. In this article, we will look into that.

Straight to program

Now to be so surprise.(: The VS intellisense shows up

As it says "Tab twice to insert the 'forr' snippet", so we go

using System;
using System.Collections.Generics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
	static void Main(string[] args)
        {
            for (int i = length - 1; i >= 0; i--)
            {

            }
	}
     }
}

The code snippet is very similar to the normal for loop but length variable is assigned to the index variable in decreasing manner.

So let us write a small program as under

using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int length = 10;
            for (int i = length - 1; i >= 0; i--)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();

        }
    }
}

Output
---------
9
8
7
6
5
4
3
2
1
0

Conclusion

Hope this will be helpful for the newbies.Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 6/22/2016 | Points: 25
Yo yo yo !!! After reading the title, first I felt it must be a spelling mistake but then thought Niladri can't do this and yes I was right. forr is a modified version of for loop. So forr must be the combination of for and r (for and r means reverse) perhaps.

Learnt new thing today, Thanks Niladri.

Keep it up!

Login to post response

Comment using Facebook(Author doesn't get notification)