c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I826 materiais10.227 seguidores
Pré-visualização50 páginas
WriteLine() writes the result. In this case, it builds a string from a number of parts or elements. Note that 
the individual elements are separated by + which here means string concatenating, and integer values 
automatically are converted to a string.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
24 
Variables
Operators
C# has a number of operators, which acts on variables or values. The above program used the + operator. 
Note that the significance of the operator is dependent on the type of the variables or constants the 
operator acts on. In the first case where both operands are integers, the meaning is addition and in the 
second case (in the WriteLine statement), the meaning are string concatenating. Note that the above 
program also used the = operator, called the assignment operator and is used to assign a variable a value.
C# has the following operators in order of priority, and with decreasing priority downwards:
() . [] function(\u2026) new typeof sizeof checked unchecked
+ - ! ~ ++ -- (unary operatorer)
* / %
+ -
<< >>
< > <= >= is as
== !=
&
^
|
&&
||
?:
= *= /= %= += -= <<= >>= &= ^= |=
The individual operators are explained as they are used. The priority is of importance in expressions 
that involve multiple operators. The general rule is that you first evaluate the operators with the highest 
priority and in the case where there are several operators with the same priority they are evaluated from 
left. In for example the expression
a + b * c
b * c is calculated first, since * has higher precedence than +. If instead you writes
(a + b) * c
a + b are first calculated as parentheses have higher precedence than *. In most cases the use of operators are 
without much difficulties, but some operators requires a little explanation. Note particularly assignment 
operators, for example =+. For example means the following
int a = 11;
a += 2;
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
25 
Variables
that the variable a gets the value 13. Thus, it is just shorthand for the following:
int a = 11;
a = a + 2;
Another operator you should make special attention to is ++, which counts a variable up by 1. For 
example means
int n = 7;
++n;
that the variable n has the value 8. ++ may be written on both sides of the variable, and these can also 
be written as follows:
int n = 7;
n++;
The result in this case is the same. If the last statement, however, is included in an expression, it has 
significance on which side you write the operator. The rule is that if the operator is first (left), the variable 
incremented, after which the expression value is calculated, and is the operator after the variable the 
expression is calculated first, and then the variable is incremented. The result of the following statements
int n = 7;
int a = 0;
a = ++n;
your chance 
to change 
the world
Here at Ericsson we have a deep rooted belief that 
the innovations we make on a daily basis can have a 
profound effect on making the world a better place 
for people, business and society. Join us.
In Germany we are especially looking for graduates 
as Integration Engineers for 
\u2022	 Radio Access and IP Networks
\u2022	 IMS and IPTV
We are looking forward to getting your application!
To apply and for all current job openings please visit 
our web page: www.ericsson.com/careers
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
26 
Variables
is such that n gets the value 8 and a gets the value of 8 (n is incremented by 1 and the result assigned 
to a), while the result of the following
int n = 7;
int a = 0;
a = n++;
is that n gets the value 8 and a value of 7 (a is given value of n, and then n is counted up by 1).
Operator \u2013 operates in the same manner, but the value is decremented by 1. If you run the following 
example:
namespace Exam04
{
class Program
{
static void Main(string[] args)
{
int n = 7;
Console.WriteLine(n++);
Console.WriteLine(++n);
Console.WriteLine(n--);
Console.WriteLine(--n);
}
}
}
you got the result:
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
27 
Console programs
4 Console programs
As mentioned in the foreword this book treats only console applications. It is programs that are executed 
from a command line that writes the results to a text screen and the user can input data to a prompt. In this 
section I will look at how the user can enter data into the program and how to format the program\u2019s output.
Exam05
Perimeter and area of a circle
As an example I will show an application, where the user must enter the radius of a circle, and the program 
calculates and writes the circle\u2019s circumference and area. Below is an example of a running program:
How to
The code can be written as follows (where I have not included the program\u2019s using statements and the 
program\u2019s namespace):
static void Main(string[] args)
{
Console.Write(&quot;Enter radius: &quot;);
string text = Console.ReadLine();
double r = Convert.ToDouble(text);
double p = r * 2 * Math.PI;
double a = r * r * Math.PI;
Console.WriteLine(
&quot;Perimeter and area of a circle with radius {0:F4}: {1:F4}, {2:F4}&quot;, r, p, a);
}
Explanation
The program has in principle the same structure as the first example and consists only of a Main() 
method. The program writes a help text, then the user must enter a number (radius):
string text = Console.ReadLine();
Note that the help text is written with the method Write(). The difference between this method and 
WriteLine() is that the Write() does not end with a newline. ReadLine() is a method in the class Console 
that collects user input until the user type Enter. Then the entries are returned as a string \u2013 a variable of 
type string. Note that ReadLine() always returns a string, and it is then the programs task to convert the 
input to a different type as needed. In this case, the input is converted to a double with the statement:
double r = Convert.ToDouble(text);
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
28 
Console programs
Convert is a class in the namespace System which defines a family of conversion functions. Note that 
these \u2013 here ToDouble() \u2013 requires that the user has actually entered a legitimate number. If not, the 
program stops with an exception, there is an error handling. Next the perimeter and area are calculated:
double p = r * 2 * Math.PI;
double a = r * r * Math.PI;
Here Math.PI is a constant in the class Math which is a class in the System namespace. Finally the program 
write the result with WriteLine(), but this time the function has several arguments. The first argument 
is called a format string and is followed by three variables. The values of the variables are inserted into 
the format string determined by the so-called placeholders. For example is {0: F4} a placeholder that 
indicates that here, the first variable after the format string is added, that is the value of the r. The next 
placeholder is called {1: F4}. It indicates that here the variable p is added \u2013 variable number 2 after the 
format string. F4 means that the value is added as a decimal number (F) with 4 places after the decimal 
point. Similarly, states {2: F4} to be inserted a value formatted as a decimal number with 4 digits. It is 
in this case, the variable a.
Maersk.com/Mitas
\ufffde Graduate Programme 
for Engineers and Geoscientists
Month 16
I was a construction
supervisor in 
the North Sea 
advising and 
helping foremen 
solve problems
I was a
he
s
Real work 
International opportunities