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 187 views
187 views asked Apr 21, 2017 by avibootz
3 answers 238 views
1 answer 164 views
164 views asked Mar 6, 2023 by avibootz
2 answers 210 views
210 views asked Nov 25, 2020 by avibootz
1 answer 212 views
1 answer 215 views
2 answers 299 views
...