Understanding very simple flow & output following tutorial

  • Thread starter Thread starter ddDerelict
  • Start date Start date
D

ddDerelict

Guest
Apologies for the formatting but in order to post anything I had not run thru notepad my account must be verified and it is not immediately obvious how that occurs. The MS account I used to login with is 14 years old ...


I'm just getting started and following this tutorial: Csharp.net

My first question regards a simple routine to define a couple of int variables, request user input, evaluate input relative to defined criteria and provide feedback to user on response acceptability.

Sample 1 which doesn't work is followed by Sample 2 which does. I'm obviously not understanding something foundational about the basic program hierarchy...

Sample 1

using System;

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

/* 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!");

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 OR
static void ConOR(string[] vs)
{
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!");
}

}
}



Sample 2

using System;

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

/* 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!");

}

}
}

I also don't understand why the following conditionals:

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

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
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!");

result in the following results:

Please enter a number between 0 and 10: [[FIRST]]
10
Good job!
Please enter a number between 10 and 20: [[SECOND]]
20
Good job!
Please enter a number between 20 and 30: [[THIRD]]
20
Hey! The number should be more than 20 or more and less than 30!

I expected the first two to fail since they are not mathematically within the >< range the way third one did. In order for the first cycle of 0-10 inclusively isn't something like a "<= >=" needed (I've not got to operators yet)?

Any help will be appreciated.

Continue reading...
 
Back
Top