Conditional Statements
Posted: Sat Oct 25, 2008 1:33 am
Well, what good does it do to have a program that only follows one path. To have a good program you will most often need to have conditional statements.
Before you can write an if statement you will need to understand the logical operators:
All of these operators will return a Boolean value(true of false)
Guess what logical statements take... a Boolean value.
Basically if the statement evaluates to true, then it will execute the code inside of the if statement
There are 3 elements that make up basic conditional statements:
So the basic layout of a chain of if statements looks like this
You don't necessarily need to have any else if statements, or even an else if you don't need it, but I showed an example with those to show you how you can use them.
Take these two different but similar statements for example
AND
Now what is the difference between those two? In the first one, both of the conditions will be evaluated no matter what. In the second one, conditionTwo will only be evaluated if conditionOne is false. That is how if and else if works, in order for an else if to be evaluated the if(or else if) statement above it must have evaluated to false. An if statement alone is considered to be by itself.
So now we're going to put these if statements to use. For a simple example, make a program to determine if a number is even or not. There are multiple ways to find out if a number is even, but we're going to go with a pretty simple one. If a number is even, than that means it divides evenly by two, right? So if a number evenly divides by another number that means that there is no remainder, correct? Well in C#, there % is the modulus operator, and returns the remainder of division. You use it just like a division operator, and instead of the quotient, it will return the remainder.
Just for the sake of practicing using methods, and because it is considered good practice to make a method for something that you could be using in multiple places, that way if you need to make changes later on, you only have to make them in that method, instead of everywhere you used that bit of code in your project. In this project it's not important, but it is good to get in the habit of good practice. Ok, so a method to determine if a number is even should look something like this:
So now we need to write our conditional statements:
Pretend that we had the user enter a number into the console and stored it into userNum
What if we wanted to know if an expression evaluates to false instead of true?
Well there are two ways that we can do this, see if the expression is equal to false, or put the not operator(!) in front of it.
I prefer to test whether it is equal to false, because when you are up late and your eyes are tired it is much more clear and it's harder to misread it.
For the above example it would look like this, remember that we flip flopped the order:
Well that's a pretty simple program, let's add some complexity(not too much).
Let's make a program where the user can enter a number, and then enter a number to divide by.
The program will see if it is evenly divisible by the number.
If it is not, it will check to see if it is even
So like we were talking about earlier, a number is evenly divisible by another number if the remainder of the division is zero.
So a method determining this would look something like this:
Now we don't really need to, but since we can, I'm going to change the original IsEven method to use the IsEvenlyDivisibleBy method.
So our main method should look something like this:
Remember that you have to add static before bool on your methods in order to be able to call them from you Main method.
I'll attach my project to this post so you can see an example of the last program if you want.
Before you can write an if statement you will need to understand the logical operators:
Code: Select all
== equals
!= not equals
< less than
<= less than or equal to
> greater than
>= greater than or equal to
Guess what logical statements take... a Boolean value.
Basically if the statement evaluates to true, then it will execute the code inside of the if statement
There are 3 elements that make up basic conditional statements:
Code: Select all
if if this statement evaluates to true then execute the code inside the if brackets
else if if the previous conditions evaluated to false, then check if this one is true
else if all the if statements evaluate to false, then this will be executed
Code: Select all
if( firstCondition)
{
//
// code for when first condition evaluates to true
//
}
else if( secondCondition )
{
//
// code for when first condition evaluates to false and second evaluates to true
//
}
else
{
//
// code for when all the conditions have evaluated to false
//
}
Take these two different but similar statements for example
Code: Select all
if( conditionOne )
{
...
}
if( conditionTwo )
{
...
}
Code: Select all
if( conditionOne )
{
...
}
else if( conditionTwo )
{
...
}
So now we're going to put these if statements to use. For a simple example, make a program to determine if a number is even or not. There are multiple ways to find out if a number is even, but we're going to go with a pretty simple one. If a number is even, than that means it divides evenly by two, right? So if a number evenly divides by another number that means that there is no remainder, correct? Well in C#, there % is the modulus operator, and returns the remainder of division. You use it just like a division operator, and instead of the quotient, it will return the remainder.
Just for the sake of practicing using methods, and because it is considered good practice to make a method for something that you could be using in multiple places, that way if you need to make changes later on, you only have to make them in that method, instead of everywhere you used that bit of code in your project. In this project it's not important, but it is good to get in the habit of good practice. Ok, so a method to determine if a number is even should look something like this:
Code: Select all
bool IsEven(int num)
{
// return if the remainder of dividing by two is zero
return num % 2 == 0;
}
Pretend that we had the user enter a number into the console and stored it into userNum
Code: Select all
if( IsEven(userNum) ) // notice that IsEven returns a boolean so it can be used as the boolean expression
{
Console.WriteLine("The number " + userNum.ToString() + " is even!");
}
else // if is even is false then it must be even
{
Console.WriteLine("The number " + userNum.ToString() + " is not even!");
}
Well there are two ways that we can do this, see if the expression is equal to false, or put the not operator(!) in front of it.
I prefer to test whether it is equal to false, because when you are up late and your eyes are tired it is much more clear and it's harder to misread it.
For the above example it would look like this, remember that we flip flopped the order:
Code: Select all
if( IsEven(userNum) == false)
{
Console.WriteLine("The number " + userNum.ToString() + " is not even!");
}
else // it must be even
{
Console.WriteLine("The number " + userNum.ToString() + " is even!");
}
Let's make a program where the user can enter a number, and then enter a number to divide by.
The program will see if it is evenly divisible by the number.
If it is not, it will check to see if it is even
So like we were talking about earlier, a number is evenly divisible by another number if the remainder of the division is zero.
So a method determining this would look something like this:
Code: Select all
bool IsEvenlyDivisibleBy(int numberToDivide, int numberToDivideBy)
{
return numberToDivide % numberToDivideBy == 0;
}
Code: Select all
bool IsEven(int num)
{
return IsEvenlyDivisibleBy(num, 2);
}
Code: Select all
static void Main(string[] args)
{
Console.WriteLine("Please enter a number.");
int userNum = int.Parse(Console.ReadLine( ));
Console.WriteLine("Please enter a number to divide that number by.");
int numToDivideBy = int.Parse(Console.ReadLine( ));
if( IsEvenlyDivisibleBy(userNum, numToDivideBy) )
{
Console.WriteLine(userNum.ToString( ) + " is evenly divisible by " + numToDivideBy.ToString( ));
}
else
{
Console.WriteLine(userNum.ToString( ) + " is not evenly divisible by " + numToDivideBy.ToString( ));
if( IsEven(userNum) )
{
Console.WriteLine("But it is evenly divisible by 2");
}
else
{
Console.WriteLine("It isn't evenly divisible by 2 either");
}
}
}
I'll attach my project to this post so you can see an example of the last program if you want.