9 Replies - 661 Views - Last Post: 01 December 2014 - 12:28 PM Rate Topic: -----

#1 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 440
  • Joined: 19-January 12

Using if statement and loops

Posted 28 November 2014 - 12:52 PM

Hi, so before anyone tries to eat me alive for writing terrible C# code, I just want to say that I just started learning the language and this is my very first shot at writing a simple program that is not "Hello World"
So I recently learnt the basics of the if statement and how to loop in a console application, here's what it looks like:
namespace Testing_IF_Statement
{
    class Program
    {
        static void Main(string[] args)
        {
        START:
          int a,b,c,d;
            Console.WriteLine("Hello, this is a Console Calculator");
            Console.WriteLine();
            Console.WriteLine("Type in 2 numbers");
            b = Convert.ToInt32(Console.ReadLine());
            c = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();
            Console.WriteLine("What do you want to do now?");
            a = Convert.ToInt32(Console.ReadLine());
            if (a==1)
            {
                d = b + c;
                    Console.WriteLine("This is your answer - " + d);
                    Console.WriteLine("_________________________________");
                goto START ;
               
            }
                else if(a == 2)
                {
                    d = b - c;
                    Console.WriteLine("This is your answer - " + d);
                    Console.WriteLine("_________________________________");
                goto START ;
                }
             else if(a == 3)
                {
                    d = b * c;
                    Console.WriteLine("This is your answer - " + d);
                    Console.WriteLine("_________________________________");
                goto START ;
                }
             else if(a == 4)
                {
                     d = b / c;
                    Console.WriteLine("This is your answer - " + d);
                    Console.WriteLine("_________________________________");
                goto START ;
                }
            }
        }
    }



And again, this is just what I wrote in a few minutes without putting any thought into it.

Questions:
1)I have heard that the way I'm looping by using the goto statement is considered quite ancient, why is this and how else could I loop the program?
2)I declared a as an integer and asked the user to type 1,2,3,4 to perform mathematical functions, but when I tried declaring a as a string and searching if a == "PLUS" etc it would throw an error. Is there any mistake in how I approached this?
3)I know this is a bit premature to ask, but instead of writing _________ in the console to separate the loop is there any professional way to add a separator?
4)In general how can I improve the code?

Is This A Good Question/Topic? 0
  • +

Replies To: Using if statement and loops

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13566
  • View blog
  • Posts: 54,125
  • Joined: 12-June 08

Re: Using if statement and loops

Posted 28 November 2014 - 12:58 PM

Quote

1)I have heard that the way I'm looping by using the goto statement is considered quite ancient, why is this and how else could I loop the program?

Yes, it is bad form. Look at this recent topic. http://www.dreaminco...ic/358759-goto/
Was This Post Helpful? 1
  • +
  • -

#3 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 440
  • Joined: 19-January 12

Re: Using if statement and loops

Posted 28 November 2014 - 01:02 PM

View Postmodi123_1, on 28 November 2014 - 12:58 PM, said:

Yes, it is bad form. Look at this recent topic. [url]http://www.dreamincode.net/forums/topic/358759-goto/[/url]

Ok thank you. That discussion sums it up then.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,909
  • Joined: 12-December 12

Re: Using if statement and loops

Posted 28 November 2014 - 01:09 PM

2) This line will cause an error if you declare a as a string and enter "PLUS":
a = Convert.ToInt32(Console.ReadLine());

3) What's wrong with ___________? It's only a Console application
4) One suggestion is to investigate TryParse, anticipating that the user will not enter the value(s), or type of values, that you are hoping for.

Beware of integer division as well.



How did you latch on to goto? Is it encouraged in the tutorial you are studying?

This post has been edited by andrewsw: 28 November 2014 - 01:12 PM

Was This Post Helpful? 1
  • +
  • -

#5 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,909
  • Joined: 12-December 12

Re: Using if statement and loops

Posted 28 November 2014 - 01:15 PM

Quote

how else could I loop the program?


C# Loops :MSDN

This post has been edited by andrewsw: 28 November 2014 - 01:16 PM

Was This Post Helpful? 1
  • +
  • -

#6 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 440
  • Joined: 19-January 12

Re: Using if statement and loops

Posted 28 November 2014 - 01:18 PM

View Postandrewsw, on 28 November 2014 - 01:09 PM, said:

How did you latch on to goto? Is it encouraged in the tutorial you are studying?

Thanks!
Yes, I first saw it in a book that I came across in a store. But I read online that it is not advisable to use it anymore. The discussion above sums it up well.
Was This Post Helpful? 0
  • +
  • -

#7 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 440
  • Joined: 19-January 12

Re: Using if statement and loops

Posted 01 December 2014 - 11:38 AM

Revised for anyone who wanted it
   static void Main(string[] args)
        {
            while (true)
            {
                int a, b, c, d;
                Console.WriteLine("Hello This is a Console Calculator");
                Console.WriteLine();
                Console.WriteLine("Type in 2 numbers");
                b = int.Parse(Console.ReadLine());
                c = int.Parse(Console.ReadLine());
                Console.WriteLine();
                Console.WriteLine("What do you want to do now?");
                a = Convert.ToInt32(Console.ReadLine());
                if (a == 1)
                {
                    d = b + c;
                    Console.WriteLine("This is your answer - " + d);
                    Console.WriteLine("_________________________________");
                    

                }
                else if (a == 2)
                {
                    d = b - c;
                    Console.WriteLine("This is your answer - " + d);
                    Console.WriteLine("_________________________________");
                   
                }
                else if (a == 3)
                {
                    d = b * c;
                    Console.WriteLine("This is your answer - " + d);
                    Console.WriteLine("_________________________________");
                    
                }
                else if (a == 4)
                {
                    d = b / c;
                    Console.WriteLine("This is your answer - " + d);
                    Console.WriteLine("_________________________________");
                    
                }
            }
        }


This is way easier than using a goto statement :D
Feel free to correct me if I have used anything incorrectly

This post has been edited by Bluezap: 01 December 2014 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,909
  • Joined: 12-December 12

Re: Using if statement and loops

Posted 01 December 2014 - 11:48 AM

                 Console.WriteLine("This is your answer - " + d);
                 Console.WriteLine("_________________________________");

You repeat these lines a number of times. You could just have them once, after the if-block.
Was This Post Helpful? 0
  • +
  • -

#9 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 440
  • Joined: 19-January 12

Re: Using if statement and loops

Posted 01 December 2014 - 12:26 PM

View Postandrewsw, on 01 December 2014 - 11:48 AM, said:

                 Console.WriteLine("This is your answer - " + d);
                 Console.WriteLine("_________________________________");

You repeat these lines a number of times. You could just have them once, after the if-block.

Wow didn't even see that. Noob mistake lol :\
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,909
  • Joined: 12-December 12

Re: Using if statement and loops

Posted 01 December 2014 - 12:28 PM

It is not a mistake, just unnecessary repetition ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1