c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I830 materiais10.236 seguidores
Pré-visualização50 páginas
to be included in the sum:
const int N = 100;
It is not necessary, but it makes it easy to modify the program if, for example instead it must determine 
the sum of the numbers from 1 to 1000.
Note the algorithm. First define a variable s to the result. Next, define a variable n to the number 
to be added to the sum and initialize it to 1, which is the first number. Then repeat the subsequent 
block as long as n is less than or equal to 100 (the constant N). In the block two things happens: 
The value of the variable n is added to s and n is incremented by 1. Note that the last statement 
means that n is 1 more for each repetition, and thus the condition for the while, sooner or later becomes 
false.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
44 
Program control
Comment
Initially it may be difficult to see through an algorithm as above and see that it does the right thing 
and here it may be a good idea to implement a desktop test where you try to manually keep track of 
what happens to the variables. For example, suppose that N = 10. The program must determine the 
sum \ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd \ufffd\ufffd\ufffd\ufffd \ufffd\ufffd. When the algorithm is performed the following happens, where the first 
column shows what happens to the variable s, and the second column shows what happens variable n:
s n
0 1 The two variables after they are initialized (created)
1 2 Variables after the 1. while statement is executed
3 3 Variables after the 2. while statement is executed
6 4 Variables after the 3. while statement is executed
10 5 Variables after the 4. while statement is executed
15 6 Variables after the 5. while statement is executed
21 7 Variables after the 6. while statement is executed
28 8 Variables after the 7. while statement is executed
36 9 Variables after the 8. while statement is executed
45 10 Variables after the 9. while statement is executed
55 11 Variables after the 10. while statement is executed
This means that the loop stops when n is 11, and at that moment, the variable s has the value 55.
Is it a large program that has many variables and iterations with many repetitions, the above is obviously 
not a viable option, but in simpler cases, it may be a good way to convince itself that an algorithm 
works \u2013 and to find a fault.
Visual Studio provides a better opportunity of using the Debugger, but I show it later.
Comment
The while statement is also called a loop, an iteration or a repetition. If the block contains only a single 
statement, you can in the same manner as for if omit the parentheses. For example the above program 
can be written as:
static void Main(string[] args)
{
long s = 0;
int n = 1;
while (n <= N) s += n++;
Console.WriteLine(s);
}
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
45 
Program control
Note it here is important that ++ stands after n, because n must be incremented by 1 after the calculation 
is done.
Again it is up to you and readability, if you will omit the parentheses or not.
Comment
The task is to determine the sum of a row of integers N++++ ...321 , and above, it is solved by means 
of a loop. Seen in relation to programming it is perhaps the most obvious solution, but there are other 
options, since the sum is an example of a difference serie, and mathematics has a formula to calculate 
this sum. I mention this only to point out that a giving problem often can be solved in several ways, and 
it is not always the most obvious solution that is the best.
for
The for statement has three parts separated by semicolons: initialization, condition and expression:
for (initialization; condition; expression)
block;
Each of the three parts of the for statement can actually be empty. When the statement is carried out, 
the following occurs:
The financial industry needs 
a strong software platform
That\u2019s why we need you
Meet Lars and other 
employees at 
simcorp.com/
meetouremployees
Mitigate risk Reduce cost Enable growth
simcorp.com
Working at SimCorp means making a difference. At SimCorp, you help create the tools 
that shape the global financial industry of tomorrow. SimCorp provides integrated 
software solutions that can turn investment management companies into winners. 
With SimCorp, you make the most of your ambitions, realising your full potential in 
a challenging, empowering and stimulating work environment.
Are you among the best qualified in finance, economics, 
computer science or mathematics?
Find your next challenge at www.simcorp.com/careers
\u201cWhen I joined 
SimCorp, I was 
very impressed 
with the introduc-
tion programme 
offered to me.\u201d
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
46 
Program control
1. Perform the initialization part (if there is one)
2. Test whether the condition is true (if the condition is empty it is true)
3. If the condition is true executes the following block
4. Otherwise the for statement is interrupted
5. Execute the expression part
6. Continue with step 2
Note that the initialization part is done only once. Note also that the block can be a single statement.
Exam12
Sum of positive integers
This example has the same purpose as above, but this time I will solve the task using a for statement.
How to
With use of the for statement, the program can be written as follows, where the constant N is as above:
static void Main(string[] args)
{
long s = 0;
for (int i = 1; i <= N; ++i) s += i;
Console.WriteLine(s);
}
Note that the code resembles the previous example.
Explanation
The for statement have a counter \u2013 here the variable i \u2013 which counts the number of iterations. For each 
repetition the following statement is executed
s += i;
Note that the counter i is incremented by 1 after each pass, and that repetition continues until i reaches 
an upper limit.
Comment
The for statement is very flexible. The initialization part is usually used to initialize variables, and it is 
permissible to have multiple initializations when separated by commas:
for (a = 2, b= 3, c = 5; ...
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
47 
Program control
When, as above a variable is declared in the initialization part, the variable is not known outside the 
for statement and the corresponding block. You can declare multiple variables in the initialization part:
for (int i = 0, j = 1; ...
but they must have the same type. As for the condition there is no other requirement than it should be a 
legal boolean expression. That can include boolean operators, function calls, etc. The expressions section 
is typically used to control a controlling variable in the condition, but generally it can be anything that 
is an expression and also a function call. The expression section can have multiple expressions if they 
are separated by a comma, for example:
for (...; ...; ++i, j += 7)
In the above cases there is no advantage in applying for instead of while, and seen from the program, it 
has seldom the great importance of which loop structure is used. The choice is usually determined by 
the nature of the task, where one of the structures may be more appropriate to the other. However, for 
is the most flexible and the most frequently used.
do
There is another variant of the while statement, called a do loop that has the form:
do
{
statement;
...
}
while (condition);
The significance is that the statements in the block are performed and then the condition is tested. Is 
it true, repeat the block and it continues until the condition becomes false. Compared with the while 
statement is the difference that do always will perform the block at least once, since