/*---------------------------*/
/* 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" */
Monday, October 24, 2011
2.1. Conditional statements
/*---------------------------*/
/* C# Example #5 */
/* example05.cs */
/* */
/* Conditions with if */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System; public class Example05
{
public static void Main()
{
int number;
Console.WriteLine("Enter a number");
if (number>0) Console.WriteLine("The number is positive.");
}
}
- Create a program that asks the user an integer number and answers if it is even (hint: you must check if the remainder obtained by dividing by two equals zero: if (x% 2 == 0) ...).
- Create a program that asks the user two integer numbers and answers which one is the greatest.
- Create a program that asks the user two integers and answers if the first one is a multiple of the second (hint: as before, you should check if the remainder of the division is zero: a% b == 0).
Saturday, October 22, 2011
1.8. User-entered data: ReadLine
public class Example03
{
public static void Main()
{
int firstNumber;
int secondNumber;
int sum;
System.Console.WriteLine("Enter the first number");
firstNumber = System.Convert.ToInt32(
System.Console.ReadLine());
System.Console.WriteLine("Enter the second number");
secondNumber = System.Convert.ToInt32(
System.Console.ReadLine());
sum = firstNumber + secondNumber;
System.Console.WriteLine("The sum of {0} and {1} is {2}",
firstNumber, secondNumber, sum);
}
}
using System;
public class Example04
{
public static void Main()
{
int firstNumber;
int secondNumber;
int sum;
Console.WriteLine("Enter the first number");
firstNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second number");
secondNumber = Convert.ToInt32(Console.ReadLine());
sum = firstNumber + secondNumber;
Console.WriteLine("The sum of {0} and {1} is {2}",
firstNumber, secondNumber, sum);
}
}
- Multiply two numbers typed by user.
- The user will type two numbers (x and y), and the program will calculate the result of their division and the remainder of that division.
- The user will type two numbers (a and b), and the program will display the result of the operation (a + b) * (a-b) and the result of the operation a2-b2.
- Sum three numbers typed by user.
- Ask the user a number and show its multiplication table. For example, if the number is 3, something like this should be written
Friday, October 21, 2011
1.7. Comments
/* ---- C# example: sum of two preset numbers ---- */
public class Example02b {
public static void Main()
{
int firstNumber;
int secondNumber;
int sum; /* Because we keep it for later use */
firstNumber = 234;
secondNumber = 567;
/* We calculate the sum first */
sum = firstNumber + secondNumber;
/* And finally we display the result */
System.Console.WriteLine("The sum of {0} and {1} is {2}",
firstNumber, secondNumber, sum);
}
}
Thursday, October 20, 2011
1.6. Identifiers
Wednesday, October 19, 2011
1.5. Introduction to variables: int
- (We still skip the details of what "public", "class", "static" and "void" mean).
- Main() indicates where the body of the program begins, and the body of the program is delimited between curly braces ( "{" and "}" )
- int firstNumber, reserves space for an integer number, which we will refer to as "firstNumber".
- int secondNumber, prepares space for another integer number, which we will call "secondNumber".
- int sum; reserves space to store a third integer, called "sum".
- firstNumber = 234, sets the value for the first number to be added.
- secondNumber = 567, sets the value for the second number.
- sum = firstNumber + secondNumber, calculates the sum of those two numbers and stores the result in another variable, instead of displaying it directly on screen.
- System.Console.WriteLine ("The sum of {0} and {1} is {2}", firstNumber, secondNumber, sum), displays the text and the values of these three variables (the two starting numbers and their sum).
Saturday, October 15, 2011
1.4. Basic mathematical operations in C#
- Create a program to calculate the product of 12 and 13.
- Write a program that calculates the difference (subtraction) between 321 and 213.
- Make a program that computes the result of dividing 301 by 3.
- Create a program that calculates the remainder of the division of 301 by 3.
- First, the operations indicated in parentheses.
- Then the negation
- After that, multiplication, division and the remainder of the division.
- Finally, addition and subtraction.
- If two operations have the same priority, they are analyzed left to right.
- -2 + 3 * 5
- (20+5) % 6
- 15 + -5*6 / 10
- 2 + 10 / 5 * 2 - 7 % 1
public class MultiplyOverflow
{
public static void Main()
{
System.Console.WriteLine(10000000*10000000);
}
}
Thursday, October 13, 2011
1.3. Display integer numbers on screen
public class Example01sum
{
public static void Main()
{
System.Console.WriteLine(3+4);
}
}
Monday, October 10, 2011
1.2. How to test this program using Mono
Saturday, October 8, 2011
1.1 Display a text using C#
public class Example01
{
public static void Main()
{
System.Console.WriteLine("Hello");
}
}
- WriteLine("Hello"); - "Hello" is the text we want to write, and WriteLine is the command which writes a text on screen (and moves the cursor to the next line).
- Console.WriteLine("Hello"); - because WriteLine is a command related to “Console” (the black text screen we can use when we use the operating system).
- System.Console.WriteLine("Hello"); - because the commands related to Console belong to “System” category.
- The curly braces { and } are used to mark the beginning and the end of a program block (now, the “main” block of the program).
- public static void Main() - Main is the "body" of our program (a program can be split intro several parts, as we'll see later). Every C# program must have a "Main" block. Why "void"? Why "static"? Why "public"? Why those empty parentheses ? We'll see those details later. For now, we'll just remember that this is the usual way to write "Main".
- public class Example01 – At this point, we'll just consider that "Example01" is the name for our program, and that a line like that must exist in all of our programs. Again, we'll see later the details on why is it a "class" and why is it "public".
- Each C# statement must end with a semicolon (;)
- C# is a free format language, so we can write several commands on one line, and we can split statements into several lines or using spaces. For this reason, the above program could also be written like this (though not advisable, because it can be less readable):
public class Example01 {
public
static
void Main() { System.Console.WriteLine("Hello"); } }
public class Ejemplo01 {public static void Main(){System.Console.WriteLine("Hola");}
}
- The vast majority of orders that we can find in the C# language are English words or abbreviations of them. But keep in mind that C# is case sensitive, so "WriteLine" is a recognized word, but "writeLine", "WriteLine" or "WriteLine" are not.
1. First contact with C#
Friday, October 7, 2011
0. Suggested exercises
- Find in Internet an emulator for a 80s computer, such as the Amstrad CPC series, the MSX range or the Commodore 64 (Sinclair ZX Spectrum is not recommended for this exercise) and test the first example program in section 0.1 (the BASIC source program).
- Change the first example, so that it writes you name, instead of "Hello".
- Find a Pascal compiler named Free Pascal, install it and test the second example program.
- Change the second example, so that it writes you name.
- Find a C compiler for the operating system you are using (on Linux it is probably installed; on Windows or Mac OS X, you can try CodeLite) and test the third example program.
- Change the third example, so that it writes you name.
0.3. Pseudocode
Read card’s magnetic bandConnect with bank centralIf connection_available and correct_card: Request PIN If PIN is correct Check balance If balance >= shopping_amount Accept Operation balance = balance - amount End If End IfEnd If
Wednesday, October 5, 2011
0.2. Assemblers, compilers and interpreters
0.1. High-level and low-level languages.
PRINT "Hello"
program HelloUsingPascal;
#include
public class Example01
print("Hello")
dosseg
Monday, September 19, 2011
0. Introduction
Index
- Why?
- 0. Introduction
- 0.1 High-level and low-level languages
- 0.2. Assemblers, compilers and interpreters
- 0.3. Pseudocode
- 0. Suggested exercises
- 1. First contact with C#
- 1.1 Display a text using C#
- 1.2. How to test this program using Mono
- 1.3. Display integer numbers on screen
- 1.4. Basic mathematical operations in C#
- 1.5. Introduction to variables: int
- 1.6. Identifiers
- 1.7. Comments
- 1.8. User-entered data: ReadLine
- 2.1. Conditional statements
- 2.1.2. "if" and compound statements
- 2.1.3. Relational operators
- 2.1.4. if-else
- 2.1.5. Logical operators: &&, ||, !
- 2.1.6. The risk of assignment inside an "if"
- 2.1.7. Introduction to Flowcharts
- 2.1.8. Conditional operator: ?
- 2.1.9. switch
- 2.2. Iteration statements
- 2.2.1. while
- 2.2.2. do ... while
- 2.2.3. for
- 2.3. Exiting a loop: "break" command
- 2.4. "Continue" statement: force the next iteration
- 2.4b. Solved exercises with "for"
- 2.5. The "goto" statement
- 2.6. More on flowcharts. Box (Chapin) diagrams.