We reviewed boolean logic tables.
If A and B are boolean variables, then consider:
AND -- A && B
Both A and B must be true to get a true result.
OR -- A || B
Either A and B must be true to get a true result - so if you have at least one
true, then the result will be true.
NOT -- !A
Not gives you the inverse (opposite), and it only takes one variable (whereas
AND and OR take 2).
The following code was built in class today (9/13/07). The commented part was done first and can be put back in to see an example
of how true/false booleans work. Notice that it only gives you one try and
guessing, so next week (based upon Chapter 5 content), we'll add the idea of
repeating the guessing.
using System;
using System.Collections.Generic;
using System.Text;
namespace _303_fall_07_lecture__07
{
class Program
{
static void Main(string[] args)
{
//bool A, B;
//A = false;
//B = false;
//if (A == true)
//{
// Console.WriteLine("1");
//}
//else
//{
// if (B)
// {
// Console.WriteLine("2");
// }
// else
// {
// Console.WriteLine("3");
// }
//}
//Console.Write("Press ENTER to end the program");
//Console.ReadLine();
int magic_number = 13;
int guess;
Console.Write("Please enter a number between 1 and 100: ");
guess = Convert.ToInt32(Console.ReadLine());
if (guess == magic_number)
{
Console.WriteLine("You got it!");
}
else if (guess < magic_number)
{
Console.WriteLine("You should guess higher");
}
else // not really necessary to test if (guess > magic_number) b.c we know
{
Console.WriteLine("You should guess lower");
}
}
}
}