0

I tried to do a loop to exit if only defined values ​​are entered with true but infinite loops are accrued. I think ı can use goto but ı don't want to do that. I am a new learner so Any help would be appreciated

enter image description here

using System;

class Program
{
    public static void Main(string[] args)
    {

        Console.Write("Birinci sayınızı giriniz=>");
        int s1 = Convert.ToInt16(Console.ReadLine());
        Console.Write("İkinci sayınızı giriniz =>");
        int s2 = Convert.ToInt16(Console.ReadLine());
        Console.Write("İlk önce yapmak istediğiniz işlemi giriniz");
        Console.Write("Toplama(1),Çıkarma(2),Çarpma(3),Bölme(4),Hepsi(5):");
        int mn = Convert.ToInt16(Console.ReadLine());
        while (true)
        {
            switch (mn)
            {
                case 1:
                    Toplama(s1, s2);
                    Console.ReadKey();
                    break;
                case 2:
                    Çıkarma(s1, s2);
                    Console.ReadKey();
                    break;
                case 3:
                    Çarpma(s1, s2);
                    Console.ReadKey();
                    break;
                case 4:
                    Bolme(s1, s2);
                    Console.ReadKey();
                    break;
                case 5:
                    Toplama(s1, s2);
                    Çıkarma(s1, s2);
                    Çarpma(s1, s2);
                    Bolme(s1, s2);
                    Console.ReadKey();
                    break;
                default:
                    Console.WriteLine("Yanlış bir değer girdiniz.");
                    break;
            }
        }
    }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86

2 Answers2

0

You must use goto statement or to add some bool variables and if at the end of loop if you want to break while(true). Labelled break exist in java, but not in C#, there are many questions about this: equivalent of Java's labelled break, using break and labels.

Also notice that your code will look like this if you are using goto:

while (true)
{
    switch (mn)
    {
        case 1:
            Toplama(s1, s2);
            Console.ReadKey();
            goto loopend;
            break;
        /* Other cases*/
    }
}
loopend: return; // or some other code

If compiler see } after label it will not compile because label is not a statement. Also there was question previous about this: label at the end of method.

0

take the last input inside while loop so you can take input from user until your condition get valid

You can do something like this

using System;

class Program
{
    public static void Main(string[] args)
    {

        Console.Write("Birinci sayınızı giriniz=>");
        int s1 = Convert.ToInt16(Console.ReadLine());
        Console.Write("İkinci sayınızı giriniz =>");
        int s2 = Convert.ToInt16(Console.ReadLine());
        Console.Write("İlk önce yapmak istediğiniz işlemi giriniz");

        bool isNotComplete = true; //make it true on start
        while (isNotComplete)
        {
            Console.Write("Toplama(1),\nÇıkarma(2),\nÇarpma(3),\nBölme(4),\nHepsi(5),\nEXIT(0):\n");
            int mn = Convert.ToInt16(Console.ReadLine());
            switch (mn)
            {
                case 1:
                    Toplama(s1, s2);
                    Console.ReadLine();
                    break;
                case 2:
                    Çıkarma(s1, s2);
                    Console.ReadLine();
                    break;
                case 3:
                    Çarpma(s1, s2);
                    Console.ReadLine();
                    break;
                case 4:
                    Bolme(s1, s2);
                    Console.ReadLine();
                    break;
                case 5:

                    Toplama(s1, s2);
                    Çıkarma(s1, s2);
                    Çarpma(s1, s2);
                    Bolme(s1, s2);
                    Console.ReadLine();
                    break;
                case 0:
                    isNotComplete = false;
                    break;
                default:
                    Console.WriteLine("Yanlış bir değer girdiniz.");
                    break;
            }
        }
    }

    private static void Bolme(int s1, int s2)
    {
        Console.Write("Bolme");
    }

    private static void Çarpma(int s1, int s2)
    {
        Console.Write("Çarpma");
    }

    private static void Çıkarma(int s1, int s2)
    {
        Console.Write("Çıkarma");
    }

    private static void Toplama(int s1, int s2)
    {
        Console.Write("Toplama");
    }
}

Edit1:

after chat with lazar I made changes now you just need to add one more case to add flag to loop out from while condition

here is fiddle like to try it out https://dotnetfiddle.net/jjEIPN

Dark S
  • 308
  • 2
  • 15
  • You didn't try your code, or you didn't read the question? Because question is not about helper functions and storing return values(we don't know what those methods are doing). Question is only about `break` statement. – Lazar Đorđević Mar 25 '21 at 11:29
  • as per my understanding, "I tried to do a loop to exit if only defined values ​​are entered with true" it means he wants to keep get input until he computing results from inpute get the desire result. and as per his code snippet, he directly applied true condition in while loop which obviously will end up in infinite loop.. plzz correct if i'm wrong in question understanding – Dark S Mar 25 '21 at 11:36
  • My understanding is that he wants to proceed in next iteration only if input is not as desired (0-5 integer value). So he wants to break loop directly from `case1` for example, but labelled `break` doesn't exist in C#. That is why in he wrote in the title _goto alternative_. – Lazar Đorđević Mar 25 '21 at 11:39
  • he has different action methods for different input cases so if he trying to build app like simple calculator then then he just need an extra case which will turn that whilecondition flag false.. or if he wants something other then maybe mybe my suggestion will help – Dark S Mar 25 '21 at 11:43
  • We don't know what is his app doing, or if this is just example to understand alternatives for `goto`. Don't assume something that is not in question. – Lazar Đorđević Mar 25 '21 at 11:45
  • and plus he dont want goto as well – Dark S Mar 25 '21 at 11:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230364/discussion-between-lazar-dordevic-and-t-giri). – Lazar Đorđević Mar 25 '21 at 11:46
  • my intentions were if user did not enter true statements loop doesnt break and continue,but ı cannot exit loop after the desired results and stuck – Ahmet Alper Kızıltunç Mar 26 '21 at 12:33
  • ı cannot use goto because this is a homework for my college and our teacher doesnt allow goto – Ahmet Alper Kızıltunç Mar 26 '21 at 12:34
  • ı checked one of the answers and it is working now,thanks! – Ahmet Alper Kızıltunç Mar 26 '21 at 12:36