none
If statements RRS feed

  • Question

  • So in my university i am currently going for a study of if, else if and else statements. Since its a subject i just forgot about. So i started to write a program involved with alooot of if statements. Now it works fine but if a if statement is written under alot of other if statements then in the Console i have to press enter a hole lot of times before the if statement "answer" shows up i will leave a quick examble of my problem below

    Console.WriteLine("Test");                                                                                                                                                      string T = (Console.ReadLine());                                                

    if(T == "Yes")

    {

    Console.WriteLine("Answer for if statement")

    }

    if(T == "Another answer")

    {

    Console.WriteLine("A hole alot other if statements to yes ")

    }

    if(T == "Another answer")

    {

    Console.WriteLine("A hole alot other if statements to yes ")

    }

    if(T == "Another answer")

    {

    Console.WriteLine("A hole alot other if statements to yes ")

    }

    if(T == "Another answer")

    {

    Console.WriteLine("A hole alot other if statements to yes ")

    }

    if(T == "Another answer")

    {

    Console.WriteLine("A hole alot other if statements to yes ")

    }

    if(T == "Another answer")

    {

    Console.WriteLine("A hole alot other if statements to yes ")

    }

    if(T == "Another answer")

    {

    Console.WriteLine("A hole alot other if statements to yes ")

    }

    if(T == "No")

    {

    Console.WriteLine("Now if i answer No from start i have to press enter multiple times to make this apear ):");

    }

    How can i fix this ?

    Btw forget about that the strings make non sense i just have to write this down in a hurry hope you get the point

    Sunday, February 10, 2019 1:01 PM

All replies

  • I just tried out your code. This is the whole content of the "Program.cs" (note that I had to fix some semicolons that you were missing):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Testing
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Test"); string T = (Console.ReadLine());
                if (T == "Yes")
                {
                    Console.WriteLine("Answer for if statement");
                }
    
                if (T == "Another answer")
                {
                    Console.WriteLine("A hole alot other if statements to yes ");
                }
    
    
                if (T == "Another answer")
                {
                    Console.WriteLine("A hole alot other if statements to yes ");
                }
    
                if (T == "Another answer")
                {
                    Console.WriteLine("A hole alot other if statements to yes ");
                }
    
                if (T == "Another answer")
                {
                    Console.WriteLine("A hole alot other if statements to yes ");
                }
    
                if (T == "Another answer")
                {
                    Console.WriteLine("A hole alot other if statements to yes ");
                }
    
                if (T == "Another answer")
                {
                    Console.WriteLine("A hole alot other if statements to yes ");
                }
    
                if (T == "Another answer")
                {
                    Console.WriteLine("A hole alot other if statements to yes ");
                }
    
                if (T == "Another answer")
                {
                    Console.WriteLine("A hole alot other if statements to yes ");
                }
    
                if (T == "No")
                {
                    Console.WriteLine("Now if i answer No from start i have to press enter multiple times to make this apear ):");
                }
            }
        }
    }
    

    When I run it, it works as expected. I only have to type "No" and press enter once, and the result is displayed.

    So if your code needs multiple enters, there must be something else that is not present in the block that we saw. Try copying into Program.cs the code that I posted, and you will see that it works as expected.

    Sunday, February 10, 2019 2:36 PM
    Moderator
  • Hello,

    In the future please use the code block for posting code.

    Okay, rather than have multiple if statements one option is to use a dictionary of possible values to check against in tangent here with a ternary operator.

    using System;
    using System.Collections.Generic;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                var comparer = StringComparer.OrdinalIgnoreCase;
    
                var items = new Dictionary<string, string>(comparer)
                {
                    { "yes","answered yes"},
                    { "no", "answered no" },
                    { "maybe", "answered maybe" }
                };
    
                Console.WriteLine("Test");
    
                var userResponse = (Console.ReadLine());
    
                if (!userResponse.IsNullOrWhiteSpace())
                {
                    Console.WriteLine(items.ContainsKey(userResponse) ? items[userResponse] : $"{userResponse} not found");
                }
                else
                {
                    Console.WriteLine("Nothing to look for.");
                }
    
                Console.ReadLine();
    
            }
    
        }
    
        public static class StringExtensions
        {
            public static bool IsNullOrWhiteSpace(this string sender)
            {
                return string.IsNullOrWhiteSpace(sender);
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, February 10, 2019 2:37 PM
    Moderator
  • Hello thanks for the answer this can maybe be bacause on my other project i didnt use {} I would definitely try it out and see whats happening.

    Sunday, February 10, 2019 2:59 PM