c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I834 materiais10.250 seguidores
Pré-visualização50 páginas
program is written in the same way as the other examples, and code for the Main() method is as 
follows:
static void Main(string[] args)
{
long t1 = Convert.ToInt64(args[0]);
long t2 = Convert.ToInt64(args[1]);
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
33 
Console programs
Console.WriteLine("Quotient of {0} and {1} is {2}", t1, t2, t1 / t2);
Console.WriteLine("Modulus of {0} and {1} is {2}", t1, t2, t1 % t2);
}
Explanation
The Main() method has a parameter which is an array (arrays are discussed below), and which may 
represent arguments on the command line. Since the arguments from the command line are always 
strings, they are converted to integers (in this case of the type long) before the result can be calculated, 
which is the quotient and modulus of the two numbers. Note specially the operator % which is the 
modulus operator (remainder of division).
Note that if you run the program and do not specify two arguments, or if there is one of the arguments 
that is not an integer the program will crash with an error message. But if you specify more than two 
arguments, the last are just ignored.
© Agilent Technologies, Inc. 2012 u.s. 1-800-829-4444 canada: 1-877-894-4414
Budget-Friendly. Knowledge-Rich.
The Agilent In\ufb01niiVision X-Series and 
1000 Series offer affordable oscilloscopes 
for your labs. Plus resources such as 
lab guides, experiments, and more, 
to help enrich your curriculum 
and make your job easier.
See what Agilent can do for you.
www.agilent.com/\ufb01nd/EducationKit
Scan for free 
Agilent iPhone 
Apps or visit 
qrs.ly/po2Opli
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
34 
Program control
5 Program control
The above examples are all sequential so that the statements may be carried out in the order they are 
written. In practice, all programs need to make the execution of statements depending on a condition that 
occurs when using control statements. C# has the following fundamental statements for program control
\u2022	 if
\u2022	 while
\u2022	 do
\u2022	 for
\u2022	 switch
Control statements are best illustrated through examples in the form of simple methods, which are the 
subject of this section.
Control statements are used in methods in the same manner as the other statements, for example 
WriteLine, but a control statement makes the execution of one or more other statements depending on 
a condition. Thus, it needs to refer to multiple statements as a whole, which is done by means of a block, 
which is just a number of statements in brackets:
{
statement1;
statement2;
....
}
For example are the statements in a method a block, but it will be apparent hereinafter, that one can 
have blocks within a block. You\u2019ve actually already seen examples, where a class is also a kind of block 
which instead of statements contains methods.
Control statements are needed to be able to write programs that do something interesting. First with 
control statements available, you can begin to work on algorithms and hence write programs that solve 
a specific problem. The following will therefore also to some extent focus on algorithms.
if
An if statement has the form:
if (condition) block
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
35 
Program control
and the meaning is that if the condition is true, then perform the block (the block\u2019s statements). Otherwise, 
nothing happens. A condition is an expression whose value is a bool and hence an expression that is true 
or false. If only a single statement has to be controlled by a condition, you must omit the parentheses 
and simply write:
if (condition) statement;
Exam09
Sort two numbers
The task is to write a program where the user can enter two integers. The program will then print the 
two numbers in ascending order. An example of an operation of the program might be:
How to
The task can be decomposed into two sub problems:
\u2022	 Enter two integers
\u2022	 Print the numbers in ascending order
To solve the first problem one has to perform the same operation (enter an integer) twice, and it is 
therefore worthwhile to write this operation as a method:
static int Enter()
{
Console.Write("Enter an integer: ");
string text = Console.ReadLine();
return Convert.ToInt32(text);
}
Note that the method has a type, but it is explained below.
To print the results you have to find the smallest number, and it is here the if statement comes at the 
track. I will use the approach that if the first number is greater than the last, I will swap the two numbers, 
and in one way or another I have in C# to determine (test) if the first number is greater than the other. 
That\u2019s exactly what an if statement is used to.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
36 
Program control
One can define a method for the printing of the results follows:
static void Sort1(int a, int b)
{
if a > b then
{
save a in a help variable t
a = b
b = t
}
Console.WriteLine("{0} {1}", a, b);
}
Strictly speaking it is not a method, but it is a solution formulated by using an informal language \u2013 it is 
an algorithm. The task is therefore to write this algorithm in C#:
static void Sort1(int a, int b)
{
if (a > b)
{
int t = a;
a = b;
b = t;
}
Console.WriteLine("{0} {1}", a, b);
}
With us you can 
shape the future. 
Every single day. 
For more information go to:
www.eon-career.com
Your energy shapes the future.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
37 
Program control
Then the program itself can be written as follows:
static void Main(string[] args)
{
int a = Enter();
int b = Enter();
Sort1(a, b);
}
Explanation
The method Sort1() has a condition in the form of an if statement that tests whether the value of a 
is greater than the value of b, and when it is true, the next block, which reverses the two numbers, is 
executed. Since the block is only executed if the value of a is greater than the value of b, the value of a 
will always be less than the value of b after the block is executed. The result is that the two numbers are 
printed in ascending order.
You should note how to write a condition in an if statement, and that it is an expression whose value 
is either true or false. It is an expression whose type is bool. Also note that the expression should be in 
parentheses.
You should also note the method Enter(), which is a method with a return value. Return values are 
addressed later in the section on methods, but until then you can think of a return value as a value 
attached to the name of the method with a return statement. That way you can get the value transferred 
to the place where the method is called such by writing:
int a = Enter();
This means that the value attached to the name of the method by return is stored in variable a.
Comment
A method\u2019s statements are an algorithm or a solution. The biggest challenge in writing programs is to 
learn to write algorithms, because a given problem can often be solved in several ways. Above I have 
shown an algorithm to swap two numbers and it can often be a good idea to start writing an algorithm in 
an informal language, because in this way frees the solution from the many details of the programming 
language and thus can focus on the problem itself and how it is solved. Once you have formulated the 
algorithm in an informal language, it is typically an easy task to translate the algorithm to the specific 
programming language, which here is C#.
Note especially the algorithm to swap the two numbers. It is a simple