Section notes

advertisement
Derrick Kondo
(Some examples taken from Eric
Roberts’ The Art and Science of C)
Section 10/12/1999
John Datuin’s Office Hours: Tuesday, Thursday 4:00-5:00 PM AP&M 3349D
Derrick Kondo’s Office Hours: Thursday 8:30-10:30 PM AP&M 3337D
Today’s Goals:
0.
1.
2.
Review assignments
To be able to use C’s shorthand assignments
To understand the for statement, the while statement, and the if statement.
Review of assignments
x = x+y;
Here, we are storing the value of the expressions on the right to the expression on the left. The ‘=’ sign in
C is NOT equivalent to the mathematical expression of equality.
A statement of the form
left expression = right expression;
should be interpreted as
“The value of the right expression is assigned to the left expression”
NOT as
“The right expression mathematically equals the left expression.”
So,
x = x-y;
is completely legal in C because we are assigning the sum of x and y to the variable x.
It turns out that expressions of the form
variable = variable op expression;
are very common in C programs. In order to relieve programmers’ stressful lives and to improve code
readability, C allows the expression to be written in shorthand as:
variable op= expression.
For example,
x=x+y
can be rewritten equivalently as
x+=y
and
x = x – y
can be rewritten equivalently as
x-=y
C further simplifies our programming lives by abbreviating the common expression:
variable = variable + 1;
to
variable++;
and
variable = variable – 1;
to
variable --;
For example,
k = k+1;
is equivalent to
k++;
and
k=k-1
is equivalent to
k--;
The abbreviations of these common assignment operations are called shorthand assignments in C.
Control Statements
for loops
Control statements affect how statements are executed (E.g. for, while, if statements).
Syntax of the for loop:
variable declaration;
for (variable initialization; test; variable update){
statements
}
Don’t forget the variable declaration!
The test is done immediately after the initialization.
For example,
int j;
for (j=0; j<2; j++){
printf (“a rose is”);
}
printf (“a rose\n”);
What is the value of j after the for loop?
It’s 2 because that is the condition in which we break from the for loop. That is, 2 is obviously not less
than 2. So the test j<2 fails, and we leave the for loop.
In general, to repeat a set of statements N times, we use the following for loop
int j;
for (j=0; j<N; j++){
statements…
}
It may seem odd that we begin indexing at 0 instead of 1, but you will see later in the course that array
indexing begins at 0. Since for loops are often used to perform operations on each element of an array, C
programmers usually have the index initially set to 0.
What’s wrong with the following piece of code?
Let’s say the function GetInteger is defined somewhere else in the file. Assume that GetInteger prompts
the user for an integer and then returns that integer.
void main (void){
int j, value, total;
printf (“This program adds a list of ten numbers.\n”);
for (j=0; j<10; j++){
printf (“ ? “);
value = GetInteger();
total +=value;
}
}
total has never been initialized. Don’t forget to initialize variables before they are used in a loop.
The correct code is:
void main (void){
int j, value, total;
printf (“This program adds a list of ten numbers.\n”);
total = 0;
for (j=0; j<10; j++){
printf (“ ? “);
value = GetInteger();
total +=value;
}
printf (“The total is %d\n”, total);
}
while loops
In the previous program, we wanted to get ten integers from the users and to add them up. But what if we
wanted to user to be able to enter an arbitrary number of integer, which we would then sum up.
When the number of repetitions required is not determined before we enter a loop, we should use a while
loop.
Syntax of a while statement:
while (condition) {
statements;
}
Here’s the equivalent to the program we wrote earlier with a for loop.
int j;
j = 0;
while (j<2){
printf (“a rose is \n”);
j++;
}
printf (“a rose\n”);
Using a while loop, the summation program modified to sum up an arbitrary number of integers would look
like:
void main (void){
int j, value, total;
printf (“This program adds a list of numbers.\n”);
printf (“Signal end of list with a 0.\n”);
total = 0;
while (1){ /* In C, the value 1 should be interpreted as “true”,
and the value 0 should be interpreted as” false*/
printf (“ ? “);
value = GetInteger();
if (value == 0){
/* relational operator.
would NOT work */
break;
}
total +=value;
}
printf (“The total is %d\n”, total);
}
if (value =0)
== is a relational operator used to compare two values.
There are six relational operators in C:
>
<
>=
<=
==
!=
(this is NOT equivalent to the assignment operator =. Using “if (x=1)” is completely wrong)
Download