Link to home
Start Free TrialLog in
Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

asked on

new to C# - exit while

I am new to C#. This is a sample. How can I exit while if 'q' ? Break below in C#, it exit "if" only. If 'q' I need to exit from while loop .

                  ...

                  while (true)
                            {
                               
                                if (line_tmp == "q")
                                {
                                    break;
                                }
                             
                                else
                                {
                                    Console.WriteLine("There is test");
                                }
                            }
                  ...
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Replace break; with return;
...or initialize the variable and change the conditional check:

    line_tmp = "";
    while (line_tmp != "q")
    {

    }
it exit "if" only
break has no effect on if blocks--it is only applicable for loops and switch statements. Why do you think it is exiting the if only?
ASKER CERTIFIED SOLUTION
Avatar of Anuradha Goli
Anuradha Goli
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I wonder if I might be humored in getting an answer to:  what is different about the code posted in the selected "solution" and the original post? I wish all my answers were that easy.