How to stop a for loop at a specific cycle?

FranK Duc 121 Reputation points
2021-03-26T14:24:01.493+00:00

Hello,

I am trying to stop a loop at a specific cycle.

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution {

    // Complete the maxSubsetSum function below.
    static int maxSubsetSum(int[] arr) {

         int size = arr.Length;
        int max_so_far = int.MinValue,
         max_ending_here = 0; 

        for (int i = 0; i < size; i++) 
        {

            if (i % 2 == 0)
            {

            max_ending_here = max_ending_here + arr[i];


            if (max_so_far < max_ending_here)
                max_so_far = max_ending_here;

            // cant tell why the loop not stopping at the largest sum

                 Console.WriteLine("\t3rd loop cycle!:  " +max_ending_here );
            }

        }


        return max_so_far;

    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int n = Convert.ToInt32(Console.ReadLine());

        int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp))
        ;
        int res = maxSubsetSum(arr);

        textWriter.WriteLine(res);

        textWriter.Flush();
        textWriter.Close();
    }
}

The output return :

Input (stdin)

5

3 7 4 6 5

Your Output (stdout)

12

Expected Output

13

Debug output

 3rd loop cycle!:  3

 3rd loop cycle!:  7

 3rd loop cycle!:  12


5

3 5 -7 8 10

Your Output (stdout)

6

Expected Output

15

Debug output

 3rd loop cycle!:  3

 3rd loop cycle!:  -4

 3rd loop cycle!:  6


5

2 1 5 8 4

Only this output return the right answer.

Your Output (stdout)

11

Expected Output

11

Debug output

 3rd loop cycle!:  2

 3rd loop cycle!:  7

 3rd loop cycle!:  11

Thank you

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,222 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,031 Reputation points
    2021-03-26T14:31:45.283+00:00

    Use a break when the condition is hit e.g.

    var breakOn = 9;
    
    for (int index = 0; index < 22; index++)
    {
    
        if (index == breakOn)
        {
            Console.WriteLine($"Breaking on {breakOn}");
            break;
        }
        Console.WriteLine($"{index}");
    }
    

    Another example

    int[] breakItems = {11, 15, 19};
    
    for (int index = 0; index < 22; index++)
    {
    
        if (breakItems.Contains(index))
        {
            Console.WriteLine($"Breaking on {index}");
            break;
        }
        Console.WriteLine($"{index}");
    }
    

    Or

    for (int index = 0; index < 22; index++)
    {
    
        switch (index)
        {
            case 11:
                Console.WriteLine("\t11");
                break;
                case 15:
                    Console.WriteLine("\t15");
                    break;
                case 19:
                    Console.WriteLine("\t19");
                    break;
                default:
                    Console.WriteLine(index);
                    break;
        }
    
    }
    
    
    bool breakOut = false;
    for (int index = 0; index < 22; index++)
    {
    
        switch (index)
        {
            case 11:
                Console.WriteLine("\t11");
                breakOut = true;
                break;
            case 15:
                Console.WriteLine("\t15");
                breakOut = true;
                break;
            case 19:
                Console.WriteLine("\t19");
                breakOut = true;
                break;
            default:
                Console.WriteLine(index);
                break;
        }
    
        if (breakOut)
        {
            break;
        }
    
    }
    

    If this was in a method and wanted to return a value we use return. Also look at continue

    Or multiple cases

    private static bool BreakOutWithoutExpression(in int index)
    {
        var result = false;
    
        switch (index)
        {
            case 11:
            case 15:
            case 19:
                result = true;
                break;
        }
    
        return result;
    }
    

    See also
    https://kodify.net/csharp/loop/exit-loop/


  2. Karen Payne MVP 35,031 Reputation points
    2021-03-26T15:14:37.65+00:00

    If using the current release or C#8 then there are switch expressions

    private static bool BreakOut(int index) => index switch {11 => true, 15 => true, 19 => true, _ => false};
    

    .

    for (int index = 0; index < 22; index++)
    {
        if (!BreakOut(index)) continue;
        Console.WriteLine(index);
        break;
    }