Boolean expressions (Conditions)
A boolean expression is an expression whose result is a boolean value: true or false. Boolean
expression are often called conditions.
Recall that C uses integers to represent boolean values. 0 means false, any non-zero val
Printing Integer Values
Integer values may be printed to the console window using the printf function.
answer = 42;
printf("The answer to life, the universe, and everything
is %i\n", answer);
This code fragment prints the following te
For example, say we want to figure out our average for some number of quiz grades. If we know
how many quiz grades there are, we can just store each grade in a separate variable:
double quiz1, quiz2, quiz3;
printf("enter 3 quiz grades: ");
A loop is a construct that repeats a sequence of statements.
All interesting algorithms involve repetition. Therefore, understanding loops is essential to being
able to write interesting programs.
A single execution of the statements inside a loop is call
First C Program
Here is the traditional first C program:
Once this program has been entered in a source file and translated into an executable file by the
C compiler, we can run i
So far, the operators we have seen are useful for simple arithmetic. More powerful mathematical
functions are available through the inclusion of the <math.h> header file. To use these functions,
simply add the line
to the top of
Integer Division and Modulus
Question: what output does the following code fragment print?
a = 5;
b = 2;
quotient = a / b;
print("%i / %i = %i\n", a, b, quotient);
Mathematically, 5 / 2 = 2.5. However, the output of the code fr
while ( condition ) cfw_
You can think of the whlie loop as being a simplified for loop. The for loop as built-in support
for performing initialization and updating the loop variable. In a while loop, these must be done
One significant limitation of arrays is that once defined, an array's length (number of elements) is
A vector is like an array that grows as necessary as more elements are added.
Note that vectors are a C+ feature, and are not available in plain C
Two other important numeric data types:
float - real numbers in the range (approximately) +/- 10^-46 to 10^38
double - real numbers in the range (approximately) +/- 10^-324 to 10^308
Unlike the int type, float and double are not limited to integer values,
So far, we have looked at one-dimensional arrays and vectors, where each element is accessed
using a single integer index. E.g., if quiz is an array of double elements with length 3, then there
are 3 doublevariables which belong to
Very often it is useful to nest one loop inside another.
Say we want to print the multiplication table: every product of the integers 0 . 9:
int row, col;
for (row = 0; row <= 9; row = row + 1) cfw_
for (col = 0; col <= 9; col = col + 1) cfw_