Saturday, December 31, 2011

2.1.6. The risk of assignment inside an "if"

Beware of the equality operator: remember that the format is "if (a == b)" ... If we do not remember it and write "if (a = b)", we are trying to assign to "a" the value "b".

In some C compilers, this could be a serious problem, because in C language it is acceptable to assign a value within an "if" (although most modern compilers would warn us that it might be an unintended assignment).

For the C# language, such risk does not exist, because in C# the "condition" must be strictly "true" or "false", so an assignment would lead to a compile error: "Can not implicitly convert type 'int' to 'bool'". This is the case in the following program:



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

Friday, December 30, 2011

2.1.5. Logical operators: &&, ||, !


Conditions can be chained with "and", "or", and so on, which are expressed as follows

Operator, Meaning
&& = And
|| = Or
! = Not

So we can write things like

if ((option==1) && (user==2)) ...
if ((option==1) || (option==3)) ...
if ((!(option==correctOption)) || (pressedKey==ESC)) ...

Suggested exercises:

  • 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

We have seen that the ">" symbol is used to check whether a number is greater than another. The symbol for "less than" is also simple, but the others are a little less obvious, so let's see them:

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.");
}
}

Suggested exercises:
  • 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

We discussed that the basic format of "if" is if (condition) statement; That "sentence" can be a single statement or a compound one. Compound statements are formed by grouping a number of simple statements using curly braces ("{" and "}"), as in this example:

/*---------------------------*/
/* 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" */
 

In this case, if the number is positive, we write a text and then... we write another (we might have used only one "WriteLine" to display both texts, separated with a "line break"; we will learn more complex things to do within a compound statement later).

As shown in this example, each new "block" is usually written a little more on the right than the others, so it's easy to see where each section begins and ends. For example, the content of "Example06" is a little more to the right than the header "public class Example06", and the content of "Main" even more to the right, and also the compound statement after the "if statement. This extra spacing of the text is often called "indented writing". A typical size for indentation is 4 spaces, although in this text many times we will use only two spaces, not to reach the right margin of the paper too easily.