Sie sind auf Seite 1von 13

Engineering Programming 1

We are now ready to start discussing C programming with a little more detail.

Engineering Programming 1

Topics we will cover


Variables

Datatypes

operators

Language constucts

If statement

while loops

for loops

arrays

Engineering Programming 1

Variable Names
variable Names are made up of letters (a-z, A-Z, _ ) and digits (numbers 0-9)

The first character must be a letter.

Upper and lower case letters are distinct. so x and X are two different names.

Traditional C practice is to use lower case for variable names, and all upper case for
symbolic constants.
Keywords like if , else , int , float , etc., are reserved: you can't use them as variable
names. They must be in lower case.

Engineering Programming 1
Data Types and Sizes
When you declare a variable in C you have to give it a data type.
There are only a few basic data types in C:

char
int
float
double

capable of holding one character


an integer number with no decimal point.
a number with a decimal point. (low precision)
a number with a decimal point. (igher precision)

In addition, there are a number of qualifiers that can be applied to these basic types.

Short
long

can hold a smaller integer than 'int' can


can hold a larger integer than 'int' can

Engineering Programming 1

Arithmetic Operators ( + - / * )

int result;
int a = 5;
Int b = 6;
result = a + b;
result = a b;
a = a + 1;
a = a 1;

a += 1; or a++;
a -= 1; or a--;

Engineering Programming 1
Language constructs

if statement

if ( <condition>)
Statement;

if ( <condition>) {
statements;
statements;
}

//without { } only 1 statement.

//within { } any number of statements.

Engineering Programming 1
Language constructs

if statement

if ( <condition>) {
statements;
statements;
}
-------------------------------------------------------------------------<condition> is things like:
a==1
b>2
a>b

Engineering Programming 1
Language constructs

if statement
if ( <condition>) {
statements;
statements;
}
-------------------------------------------------------------------------if (a > b)
z = a;
else
z = b;
-------------------------------------------------------------------------'Statements' are the things you want the program to do

Engineering Programming 1

Lets put what we know to together and write a program to print the biggest of 3 numbers.
Lets use something called 'pseudo code'. (That's just saying lets use english.)
Say we have variables x,y,z with some numbers stored in them, then our program would
have to be something like the following.
Compare x to y and remember the higher value (store the higher in a new variable)

now compare the value (in the new variable) with z and store the higher value again.

Engineering Programming 1
Let's write that a bit more like a program (this is yet English not 'C')
If x is_greater than y
Store x in temp_variable
Else
Store y in temp_variable
// now compare z with temp variable
If z is_greater than temp_variable
Store z in temp_variable
Else
// no need to do anything.
Print the value in temp_variable, this will be the highest value.

Engineering Programming 1
int x,y,z, temp_var;
If x is_greater than y
Store x in temp_variable
Else
Store y in temp_variable

if ( x > y)
temp_var = x;
else
temp_var =y;

// now compare z with temp variable

// now compare z with temp variable

If z is_greater than temp_variable


Store z in temp_variable
Else
// no need to do anything.

if ( z > temp_var)
temp_var = z;
// else not needed.

Print the value in temp_variable

printf( largest value of x,y,z is %d, temp_var);

Engineering Programming 1

While loop

While ( <condition> ){
Statements;
}

int i=0;
while ( i < 10) {
printf(i = %d\n, i);
i = i + 1;
}

Engineering Programming 1

for loop
int i=0;
while ( i < 10) {
printf(i = %d\n, i);
i = i + 1;
}

for ( int i=0; i < 10; i = i + 1) {


printf(i = %d\n, i);
}

Das könnte Ihnen auch gefallen