How to exit a foreach loop in C#

1 Answer

0 votes
using System;
 
class ExitForeachLoop_CSharp
{
    static void Main(string[] args)
    {
        string s = "c# programmer";
 
        foreach (char ch in s) {
            if (ch == 'g') {
                break;
            }
            Console.WriteLine(ch);
        }
    }
}
 

 
/*
run:
      
c
#
 
p
r
o
 
*/

 



answered Aug 8, 2024 by avibootz

Related questions

1 answer 186 views
186 views asked Apr 21, 2017 by avibootz
3 answers 235 views
1 answer 161 views
161 views asked Mar 6, 2023 by avibootz
2 answers 202 views
202 views asked Nov 25, 2020 by avibootz
1 answer 210 views
1 answer 212 views
2 answers 298 views
...