C# beginner: program flow not understood

Art Hansen 566 Reputation points
2020-11-22T09:01:15.743+00:00

I’m just starting to follow a tutorial at https://csharp.net-tutorials.com/

I’m trying to understand why the processing flow doesn’t “drop through”. In the first code set it stops after the main block despite no errors being identified by VS. The second code set runs as intended executing all three blocks.

Code set 1

using System;

namespace Tute_03_conditionals
{
    class Program
    {
     /* verbose */
       static void Main(string[] args1)
        {
            int number;

            Console.WriteLine("Please enter a number between 0 and 10:");
            number = int.Parse(Console.ReadLine());

            if (number > 10)
                Console.WriteLine("Hey! The number should be 10 or less!");
            else
            if (number < 0)
                Console.WriteLine("Hey! The number should be 0 or more!");
            else
                Console.WriteLine("Good job!");
        }

        /// using || which is C# for OR
       static void ConOR(string[] args2)
       {
           int number_or;

           Console.WriteLine("Please enter a number between 10 and 20:");
           number_or = int.Parse(Console.ReadLine());

           if ((number_or > 20) || (number_or < 10))
               Console.WriteLine("Hey! The number should be more than 10 or more and less than 20!");
           else
               Console.WriteLine("Good job!");
       }

        /// using && which is C# for AND
       static void ConAND(string[] args3)
       {
           int numAND;

            Console.WriteLine("Please enter a number between 20 and 30:");
            numAND = int.Parse(Console.ReadLine());

            if ((numAND < 30) && (numAND > 20))
                Console.WriteLine("Good job!");
            else
                Console.WriteLine("Hey! The number should be more than 20 or more and less than 30!");
       }


    }
}

Code set 2

using System;

namespace Tute_03_conditionals
{
    class Program
    {
        static void Main(string[] args1)
        {
            int number;
            int number_or;
            int numAND;

            /* verbose */
            Console.WriteLine("Please enter a number between 0 and 10:");
            number = int.Parse(Console.ReadLine());

            if (number > 10)
                Console.WriteLine("Hey! The number should be 10 or less!");
            else
            if (number < 0)
                Console.WriteLine("Hey! The number should be 0 or more!");
            else
                Console.WriteLine("Good job!");

            /// using || which is C# for OR
            Console.WriteLine("Please enter a number between 10 and 20:");
            number_or = int.Parse(Console.ReadLine());

            if ((number_or > 20) || (number_or < 10))
                Console.WriteLine("Hey! The number should be more than 10 or more and less than 20!");
            else
                Console.WriteLine("Good job!");

            /// using && which is C# for AND
            Console.WriteLine("Please enter a number between 20 and 30:");
            numAND = int.Parse(Console.ReadLine());

            if ((numAND < 30) && (numAND > 20))
                Console.WriteLine("Good job!");
            else
                Console.WriteLine("Hey! The number should be more than 20 or more and less than 30!");

        }
    }
}

Any help will be appreciated.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,203 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bonnie DeWitt 811 Reputation points
    2020-11-22T21:11:10.777+00:00

    Hi Art,

    You can call the ConOR and ConAND methods directly from your Main() method. The way you've structured the ConOR and ConAND methods, they don't need any parameters:

    class Program
        {
         /* verbose */
           static void Main(string[] args1)
            {
                int number;
    
                Console.WriteLine("Please enter a number between 0 and 10:");
                number = int.Parse(Console.ReadLine());
    
                if (number > 10)
                    Console.WriteLine("Hey! The number should be 10 or less!");
                else
                if (number < 0)
                    Console.WriteLine("Hey! The number should be 0 or more!");
                else
                    Console.WriteLine("Good job!");
    
                ConOR();
                ConAND();
            }
    
            /// using || which is C# for OR
           static void ConOR()
           {
               int number_or;
    
               Console.WriteLine("Please enter a number between 10 and 20:");
               number_or = int.Parse(Console.ReadLine());
    
               if ((number_or > 20) || (number_or < 10))
                   Console.WriteLine("Hey! The number should be more than 10 or more and less than 20!");
               else
                   Console.WriteLine("Good job!");
           }
    
            /// using && which is C# for AND
           static void ConAND()
           {
               int numAND;
    
                Console.WriteLine("Please enter a number between 20 and 30:");
                numAND = int.Parse(Console.ReadLine());
    
                if ((numAND < 30) && (numAND > 20))
                    Console.WriteLine("Good job!");
                else
                    Console.WriteLine("Hey! The number should be more than 20 or more and less than 30!");
           }
    
    
        }
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Leon Laude 85,646 Reputation points
    2020-11-22T10:16:26.323+00:00

    Hi @Art Hansen ,

    I suggest you ask the subject matter experts in the dedicated C# forum over here:
    https://social.msdn.microsoft.com/Forums/en-US/home?forum=csharpgeneral

    ----------

    (If the reply was helpful please don't forget to upvote or accept as answer, thank you)

    Best regards,
    Leon


  2. MotoX80 31,561 Reputation points
    2020-11-22T16:04:48.363+00:00

    ConOR and ConAND are functions. Main is the entry point to your program and is the code that gets executed. In Main you need to add statements to call the functions.

    https://csharp.net-tutorials.com/basics/functions/

    0 comments No comments

  3. Art Hansen 566 Reputation points
    2020-11-23T07:22:45.803+00:00

    Thank you all - The provided link to the dedicated C# forum points back to this forum with a banner on the "ask question" page stating that dedicated forum -- among many others -- has been migrated here.

    Are the terms "function" and "method" being used interchangeably above as both substantive answers appear to be advising the same thing?

    Adding these two lines inside the main block {}s now does what I was trying to do:
    ConAND();
    ConOR();

    Are those two statements then what are termed "calls"?

    There doesn't seem to be any difference between defining the function/method preceding or following the Main () method. Is there any standard preference regarding placement of the "non-Main ()" methods?

    Thanx for your time and expertise.
    Art


  4. Art Hansen 566 Reputation points
    2020-11-24T07:38:16.263+00:00

    Hi Bonnie - thank you and, yes, I'm in the midst of discovering Classes. Waaaaaay back in the past I was a COBOL and Assembler programmer for a global firm and the whole OOP paradigm requires a significant mental shift but I can certainly sense it's potential particularly for geographically dispersed teams supporting enterprise wide applications.