locked
C# For Loop Statement - How do i pass variable at counter part in for loop statement RRS feed

  • Question

  • I want to pass variable at counter in for loop statement.

    for example, i have the below for loop statement in C#.

    int start = 5;
    
    int end = 10;
    
    int count = 0;
    
    if (start > end)
    
    {
    
         count = count+1;
    
    }
    
    else if (start < end)
    
    {
    
       count = count-1;
    
    }
    
    for (int i = start; i >= end; i--) ==> i want to make it as "i--" as variable. and i want pass count++ or count-- as variable.
    
    {
    
    }
    
    final output should be
    
    for (int i = start; i >= end; "count++ or count--") ==> I just want to pass count++ or count-- as variable instaed of i++.
    
    {
    
    }
    pls. guide us if any alternate way to do this..


    Monday, November 9, 2020 2:28 PM

Answers

  • If end is included into the loop, the try this approach too:

    int start = ...

    int end = ...

    int step = start <= end ? +1 : -1;

    int e = end + step;

    for( int i = start; i != e; i += step )

    {

       Console.WriteLine( i );

    }

    It should work for next cases:

    • start = 5 and end = 10
    • start = 10 and end = 5
    • start = 5 and end = 5



    • Edited by Viorel_MVP Tuesday, November 10, 2020 11:20 AM
    • Marked as answer by Gani tpt Tuesday, November 10, 2020 3:03 PM
    Tuesday, November 10, 2020 11:19 AM

All replies

  • The 3 statements in a 'for' loop can be any statements - there is no requirement that the third statement has to increment the counter variable:

    for (int i = start; i >= end; count--)
    {
    }

    The first statement is executed only once, the second is checked on every iteration, and the third is executed at the end of every iteration.

    However, with this example you better have a way to exit the loop - either you're adjusting 'i' within the loop or you have a 'break' statement somewhere.

    If you're setting up an unusual 'for' loop, you are not really doing yourself or anyone else a favor - you should code to make your code easy to understand, not clever.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Monday, November 9, 2020 2:43 PM
  • Thanks for your reply.

    but, i don't want to directly give count-- in the for loop statement. because, the count++ or count-- may vary based on the condition.

    Also, it is not 3 statements. first one is a condition (if else)

    and second one is final for loop statement.

    so we cannot directly give "count--" in for loop. Instead we can pass the variable count.

    ????

    below is the clear .


    • Edited by Gani tpt Tuesday, November 10, 2020 2:03 AM image screenshot
    Tuesday, November 10, 2020 1:50 AM
  • Hi Gani tpt,
    The for statement defines initializer, condition, and iterator sections:

    for (initializer; condition; iterator)
        body

    If you don’t change the loop variable in the iterator part of the for loop, it will cause an infinite loop. 
    According to your code, it first generates the i variable and sets its value to 5.
    Then program calculates the condition: i>= 10 and the result is false, the program will no longer execute.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 10, 2020 3:18 AM
  • the iterator part of the for loop will work without any problem.

    based on my condition, i just want to pass the interator.

    even if i change the iterator, internally i will manage the code if ++ or --.

    i think this is my first unique question for iterator issue...

    or give some other alternative way to handle this..?

    Tuesday, November 10, 2020 5:53 AM
  • If end is included into the loop, the try this approach too:

    int start = ...

    int end = ...

    int step = start <= end ? +1 : -1;

    int e = end + step;

    for( int i = start; i != e; i += step )

    {

       Console.WriteLine( i );

    }

    It should work for next cases:

    • start = 5 and end = 10
    • start = 10 and end = 5
    • start = 5 and end = 5



    • Edited by Viorel_MVP Tuesday, November 10, 2020 11:20 AM
    • Marked as answer by Gani tpt Tuesday, November 10, 2020 3:03 PM
    Tuesday, November 10, 2020 11:19 AM
  • Yes. It is awesome logic..perfectly matching..

    Kudoos...

    Tuesday, November 10, 2020 3:03 PM