/*---------------------------*/
/* C# Example #11: */
/* example11.cs */
/* */
/* Conditions with if (7) */
/* incorrect comparison */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example11
{
public static void Main()
{
int number;
Console.WriteLine("Enter a number");
number = Convert.ToInt32(Console.ReadLine());
if (number = 0)
Console.WriteLine("The number is zero.");
else
if (number < 0)
Console.WriteLine("The number is negative.");
else
Console.WriteLine("The number is positive.");
}
}
Saturday, December 31, 2011
2.1.6. The risk of assignment inside an "if"
Friday, December 30, 2011
2.1.5. Logical operators: &&, ||, !
- Create a program that asks the user a numbers and answers if it is a mutiple of 2 or a multiple of 3.
- Create a program that asks the user for two integers and answers "One number is positive," "Both numbers are positive" or "None of the numbers is positive," as appropriate.
- Create a program that asks the user for three numbers and displays the greatest one.
- Create a program that asks the user to enter two numbers and says if they are equal or, otherwise, which one is the greatest.
Thursday, December 29, 2011
2.1.4. if-else
We can specify what should happen if the condition is not met, using the "else" command:
/*---------------------------*/
/* C# Example #8: */
/* example08.cs */
/* */
/* Conditions with if (4) */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example08
{
public static void Main()
{
int number;
Console.WriteLine("Enter a number");
number = Convert.ToInt32(Console.ReadLine());
if (number > 0)
Console.WriteLine("The number is positive.");
else
Console.WriteLine("The number is zero or negative.");
}
}
We could try to avoid using "else" if we place an "if" after another, like this:
/*---------------------------*/
/* C# Example #9: */
/* example09.cs */
/* */
/* Conditions with if (5) */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example09
{
public static void Main()
{
int number;
Console.WriteLine("Enter a number");
number = Convert.ToInt32(Console.ReadLine());
if (number > 0)
Console.WriteLine("The number is positive.");
if (number <= 0)
Console.WriteLine("The number is zero or negative.");
}
}
But the behavior is not the same: in the first case (Example 8) the computer will see if the value is positive, and if it is not, it proceed to the second statement, but if it was positive, the program finishes. In the second case (Example 9), even if the number is positive, the program checks the other condition, in order to see if the number is negative or zero, so the second program is slightly slower.
We can chain several "if" statements, using "else" to mean "if this condition is not met, let's check to this other":
/*---------------------------*/
/* C# Example #10: */
/* example10.cs */
/* */
/* Conditions with if (6) */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example10
{
public static void Main()
{
int number;
Console.WriteLine("Enter a number");
number = Convert.ToInt32(Console.ReadLine());
if (number > 0)
Console.WriteLine("The number is positive.");
else
if (number < 0)
Console.WriteLine("The number is negative.");
else
Console.WriteLine("The number is zero.");
}
}
Suggested exercise:
· Improve the solution for the exercises in the previous paragraph, using "else".
Wednesday, December 28, 2011
2.1.3. Relational operators
Operator - Operation
- < Less than
- > Greater than
- <= Less than or equal to
- >= Greater than or equal to
- == Equal to
- != Not equal to (different from)
So an example to display if a number is NOT zero might be:
/*---------------------------*/
/* C# Example #7: */
/* example07.cs */
/* */
/* Conditions with if (3) */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example07
{
public static void Main()
{
int number;
Console.WriteLine("Enter a number");
number = Convert.ToInt32(Console.ReadLine());
if (number != 0)
Console.WriteLine("The number is not zero.");
}
}
- Create a program that multiplies two integer numbers as follows: ask the user a first integer. If the entered number is 0, the program should write on screen "The product of 0 by any number is 0". If the entered number is other than zero, the user is asked for a second number and the product of both is displayed.
- Create a program that asks the user two integer numbers. If the second number is not zero, it must display the result of dividing the first by the second. Otherwise, if the second number is zero, it must print "Error: I can not divide by zero."
Monday, December 26, 2011
2.1.2. "if" and compound statements
/*---------------------------*/
/* C# Example #6: */
/* example06.cs */
/* */
/* Conditions with if (2) */
/* Compound statements */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example06
{
public static void Main()
{
int number;
Console.WriteLine("Enter a number");
number = Convert.ToInt32(Console.ReadLine());
if (number > 0)
{
Console.WriteLine("The number is positive.");
Console.WriteLine("Remember you can also use negative numbers.");
} /* End of "if" */
} /* End of "Main" */
} /* End of "Example06" */