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
algorithm, but it\u2019s really important 
and it is an algorithm that you will meet many times.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
38 
Program control
if-else
An if statement can be combined with an else part:
if (condition)
block_1
else
block_2
where both block_1 and block_2 can be simple statements. The significance is that block_1 is executed if 
condition is true, and if not then block_2 is executed. As an example, the preceding method that prints 
two numbers in ascending order can be written in the following way:
static void Sort2(int a, int b)
{
if (a < b)
Console.WriteLine(&quot;{0} {1}&quot;, a, b);
else
Console.WriteLine(&quot;{0} {1}&quot;, b, a);
}
Note also that the method can be written as follows:
static void Sort2(int a, int b)
{
if (a < b)
Console.WriteLine(&quot;{0} {1}&quot;, a, b); else Console.WriteLine(&quot;{0} {1}&quot;, b, a);
}
and as another example could be written as:
static void Sort2(int a, int b)
{
if (a < b)
{
Console.WriteLine(&quot;{0} {1}&quot;, a, b);
}
else
{
Console.WriteLine(&quot;{0} {1}&quot;, b, a);
}
}
Seen from the machine, the three versions are equally good, and the choice is solely a matter of what 
you think is most readable.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
39 
Program control
Exam10
A quadratic equation
I\u2019ll show a program that can solve a quadratic equation, and therefore a program that solves a classical 
task from school and mathematics teaching. A quadratic equation is expressed as:\ufffd\ufffd \ufffd\ufffd FE[D[ \ufffd
The solution formula is:
Given the discriminant : DFEG \ufffd\ufffd \ufffd \ufffd\ufffd6ROXWLRQ\ufffd\ufffd°°°¯°°°®­ !r\ufffd \ufffd \ufffd \ufffd\ufffd \ufffd\ufffd \ufffdGLID GE GLIDE GLIVROXWLRQVQR \ufffd
The task is to write a program where the user must enter the equation\u2019s coefficients (that is a, b and c). 
The program will then determine the equation\u2019s solutions using the above formula and print the result 
on the screen.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
40 
Program control
An example of an operation of the program might be:
How to
The solution of the equation can be informally described as follows:
calculate the discriminant d
if d < 0 then no solutions
else if d > 0 then calculate two solutions
else calculate one solution
Immediately there is not much solution in that, but it breaks down the task into three sub problems, 
each of which is simpler than the original problem. This kind of problem decomposition is a principle 
that recurs in many tasks and is an important step towards a complete solution. Each of the three sub-
problems is relatively simple, and the program can be written as follows:
class Program
{
static void Main(string[] args)
{
double a = Enter(&quot;Enter a&quot;);
double b = Enter(&quot;Enter b&quot;);
double c = Enter(&quot;Enter c&quot;);
Solve(a, b, c);
}
static double Enter(string text)
{
Console.Write(text + &quot;: &quot;);
string line = Console.ReadLine();
return Convert.ToDouble(line);
}
static void Solve(double a, double b, double c)
{
double d = b * b - 4 * a * c;
Console.WriteLine(&quot;The equation {0:F4}x^2 + {1:F4}x + {2:F4} = 0&quot;, a, b, c);
if (d < 0) Result();
else if (d > 0) Result(a, b, d);
else Result(a, b);
}
static void Result()
{
Console.WriteLine(&quot;has no solution&quot;);
}
static void Result(double a, double b)
{
Console.WriteLine(&quot;has the solution {0:F4}&quot;, -b / (2 * a));
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
41 
Program control
static void Result(double a, double b, double d)
{
double y = Math.Sqrt(d);
Console.WriteLine(&quot;has the solutions {0:F4} and {1:F4}&quot;,
(-b - y) / (2 * a), (-b + y) / (2 * a));
}
}
Explanation
Note first the method Enter() which is substantially the same as in the previous example. There are 
two differences. The guiding text that tells the user what needs to be transmitted, are this time sent as 
a parameter. The second difference is that the method this time converts the input to a double, and the 
method must also return a double \u2013 the method\u2019s type is double.
The method Solve() solves the equation, and it performs three things:
\u2022	 calculates the discriminant
\u2022	 print the equation
\u2022	 implement the above algorithm, which divides up into three sub problems, and here you 
mainly observe how the if-else statements is used
The three methods to print the result does not require much explanation, but note the last, and how 
to determine the square root of a number. This is done by the method Sqrt(), which is a method in the 
class Math.
Comment
The method Solve() includes if-else statements:
if (d < 0) Result();
else if (d > 0) Result(a, b, d);
else Result(a, b);
and when each condition control only a single statement, I have used, that it is not necessary to place 
the statement in a block. It\u2019s something you can discuss, and many will prefer to write the code in the 
following way, as they think it gives a more readable code:
if (d < 0)
{
Result();
}
else if (d > 0)
{
Result(a, b, d);
}
else
{
Result(a, b);
}
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
42 
Program control
Seen from the finished program and the machine it is irrelevant, and the two codes are translated into 
the same and the one is neither more nor less effective than the other. The choice is the programmer\u2019s 
and is only a question of readability, and you should simply choose the version that you think is most 
readable. I think the first version is the most readable, but it\u2019s far from all who agree in that.
That a program is readable is actually more important than that, and here one must bear in mind that 
programs often require maintenance by anyone other than the one who originally wrote the program, 
and for it to be possible, it should be easy both to read and understand the program.
while
It is often needed to carry out a statement or a block several times until a condition occurs. Here you 
can use a while statement, which has the following form:
while (condition)
block
The significance is that the block and its statements are performed as long as the condition after while 
is true. Then the program continues with the next statement after the while construct.
By 2020, wind could provide one-tenth of our planet\u2019s 
electricity needs. Already today, SKF\u2019s innovative know-
how is crucial to running a large proportion of the 
world\u2019s wind turbines. 
Up to 25 % of the generating costs relate to mainte-
nance. These can be reduced dramatically thanks to our 
systems for on-line condition monitoring and automatic 
lubrication. We help make it more economical to create 
cleaner, cheaper energy out of thin air. 
By sharing our experience, expertise, and creativity, 
industries can boost performance beyond expectations. 
Therefore we need the best employees who can 
meet this challenge!
The Power of Knowledge Engineering
Brain power
Plug into The Power of Knowledge Engineering. 
Visit us at www.skf.com/knowledge
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
43 
Program control
Exam11
The sum of the positive number less than 100
As an example is shown a program which determines the sum of all positive integers less than or equal 
to 100, that is the sum 100...321 ++++ .
How to
The program can be written as follows:
class Program
{
const int N = 100;
static void Main(string[] args)
{
long s = 0;
int n = 1;
while (n <= N)
{
s += n;
++n;
}
Console.WriteLine(s);
}
}
Explanation
Note first that in the beginning of the program there is defined a constant, which is the largest number