chapter 6: problem solving concepts for the computer programming lec. ghader r. kurdi

60
CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Upload: linda-bradley

Post on 28-Dec-2015

227 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMINGLec. Ghader R. Kurdi

Page 2: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Introduction• The original C was still too limiting, and not standardized, and so in 1983 an

ANSI (American National Standard Institute) committee was established to formalize the language definition.

• It has taken until now (ten years later) for the ANSI standard to become well accepted and almost universally supported by Compilers.

• The first language developed was (BCPL).

• The next upgraded version was (B).

• The language (B) was upgraded to (C) by Dennis Ritche of AT & T laboratories, USA. (C) was first used to write UNIX OS. (C) is a general purpose language.

• Bjarne Strousstrup added object orientation to (C ) and developed (C++).

• The objective of learning C++ is to write programs using this higher level language to solve our day-to-day problems. It is a most widely used language.

Page 3: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Structure of a program

• Any C++ program consists of one or more modules called functions. One of these functions is called main.

• The program begins by executing main function and accesses other functions, if any.

• Functions are written after or before main function separately.• A function has:

• Heading consists of name with list of arguments (optional) enclosed in parenthesis

• Argument declaration (if any)• Compound statement enclosed in two braces { } such that each statement

ends with a semicolon.• Comments, which are not executable statement, of necessary can be

placed in between /* and */.

Page 4: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Structure of a program (cont.)• At this stage the programs considered will fit into the following

general format:

// Introductory comments// file name, programmer, when written or modified// what program does#include <iostream.h>void main (){ constant declarations variable declarations executable statements}

Page 5: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Displaying a message Example

// Display Welcome to C++#include <iostream> using namespace std; void main() { cout << ″Welcome to C++!″ << endl; }

Outputs:

Welcome to C++

Page 6: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Displaying more messages Example

// Extend the program to display three messages. #include <iostream> using namespace std; void main() { cout << "Programming is Useful!" << endl; cout << "Fundamentals First" << endl; cout << "Problem Driven" << endl; }

Outputs:

Programming is Useful!Fundamentals First Problem Driven

Page 7: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Comments

A C++ comment is written in one of the following ways:

• // line comment• /* block comment */

// Display a welcome message#include <iostream> using namespace std; void main(){ /* this comment can be considered as multiple line comment */ cout << "Hello C++ Programming";}

Page 8: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Problem Solving Tools

1. Edit: Program is created in the editor and stored on the disk.

2. Compile: Compiler create Object code and stores on disk.

3. Link: Linker links the Object code with Libraries, creates result and stores it on disk.

4. Load: Loader prints program in memory.

5. Execute: CPU takes each instruction and executes it. Possibly storing new data Values as program execute.

Page 9: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

The character set

• Character set includes the basic building blocks of a language.

• C++ character set consists of:

A. Numeric Character Set: 0-9

B. Alphabetic Character Set: A-Z, a-z

C. Special Character Set: # , ; , :,(comma) etc

D. Escape Character Set: they are used for formatting the output.

It always starts with a back slash ( \ ).

Some Examples are \n, \t, \b, \a

Page 10: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Tokens

The smallest individual unit in a program is known as token. C++ has the tokens, like:

• Keywords• Identifiers• Literals• Punctuators• Operators

Page 11: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Keywords

• Keywords or Reserved words are those words which are reserved for a specific purpose and part of the language.

• Examples are: auto, break, case, char,const, continue, default, do,double, else, enum, extern,float, for, goto, if,int, long, register, return,short, signed, sizeof, static,struct, switch, typedef, union,unsigned, void, volatile, while.Note that these words should not be used as identities.

Page 12: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Punctuators

The following characters are used as punctuators (also known as separators) in C++:

• *• +• ( )• ,• -• ;• :• *• …• =• #

Page 13: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Identifiers

• Words that are not reserved or keywords are called user defined words or identifiers.

• Identifier can be a name of variable, constant, and function.• Identifiers must follow the following rules:

A. Can be composed of alphabets (A-Z or a-z), numbers (0 to 9) and underscore “_”

B. The first character must be an alphabet or an underscore “_”.

C. Identifier must be unique. D. Blank Spaces are not allowed. E. Keywords are not allowed.F. They are case sensitive i.e. Val and val are different.

Page 14: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Identifiers (cont.)

Valid names:• roll_number• r_no• z• a1234_bc Use the name of the identifier that is self-explanatory • employee_name• student_name• sum

Invalid names:• 3x• x*3• date-1• change to• char• y=• a’• for

Page 15: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Data type

• Data types are means to identify the type of data and associated operations of handling it.

• C++ data types are of two types:• Fundamental data types• Derived data type.

Page 16: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Fundamental data types

Page 17: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Derived data type

From the fundamental types, other types can be derived by using the declaration operators, e.g. Arrays.

Page 18: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Constants and Variables

• Identifiers can be classified into two categories constants and variables.

• Constants (Literals): Constants are identifiers which do not change its stored value during executing the program.

• Variables: is an identifier that has a changeable value during the execution of a program.

Page 19: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Constants

There are 4 basic types of constants

• Integer constants: It is an integer valued number written in three different number system types, namely; decimal, octal, and hexadecimal.

• Floating point constants: It is a decimal number with a decimal point or an exponent or both. Ex; 32.65, 0.654, 0.2E-3, 2.65E10 etc. These numbers have greater range than integer constants.

• character constants: It is a single character enclosed in single quotes like a . 3 , ? , A , etc. each character has an ASCII to identify. For ′ ′ ′ ′ ′ ′ ′ ′example A has the ASCII code 65, 3 has the code 51 and so on.′ ′ ′ ′

• string constants : it consists of any number of consecutive characters enclosed in double quotes .Ex : ″ C program″ , ″mathematics″ etc……

Page 20: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Constants (cont.)

Syntax:

const data_type name = values;

const char ch='M';

Page 21: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Example

#include <iostream> using namespace std; void main(){ const float PI = 3.149; int r = 5; float area; area = r * r * PI; cout<<“r=“<<r<<endl; cout<<“area=“<<area<<endl;}

Outputs:

r= 5Area= 78.725

Page 22: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Example

#include <iostream> using namespace std; void main(){ const float PI = 3.149; PI = 3.14; int r = 5; float area; area = r * r * PI; cout<<“r=“<<r<<endl; cout<<“area=“<<area<<endl;}

Outputs:

Page 23: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Variables

• Variable Declaration: Declare a variable means to allocate a memory location to hold the value of this variable.

• Defining a variable: each variable should have an Identifier, which is the variable name that you will use during the program, and in fact it is translated to an address of the memory location.

• Data Type: it shows the type of the value that will be stored in this variable

Page 24: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Variables (cont.)

Page 25: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Variables (cont.)

• There are different types of variables such as:• Character variable (To store characters) • Integer variable (To store whole numbers) • Float variable (To store floating point numbers) • Boolean variable (To store two values true/false) Syntax: Data_Type Identifier;

int number1;

Float a , b ;

int a,b,c,d=10;

• (you can also initialize at the time of declaration).

Page 26: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Variables (cont.)

Choose the most suitable data type for the following data• ID• Phone number• Weight• Height• Marital Status • Gender

Page 27: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Operators Operators cause the compilers to take some action and work on operands (data). Operators are classified as:- • I/O Operators• Assignment Operator• Arithmetic Operators• Unary Operators• Relational Operators• Logical Operations• Conditional Operator• Compound operators • Library Functions

Page 28: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

I/O operators

• The input operator (>>) is used to read value from standard input.

cin>>name;• The output operator(<<) is used to direct a value to

standard output.

cout << ″Welcome to C++!″;

Page 29: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Assignment Operator

• Equal sign (=) is used for assignment operator.• It is used to assign values to variables.• Syntax: VariableName = value; Or VariableName = expression;

• a=10;• a=b;• a=b + c;• In every case the value of the right hand variable

(constant) are assigned to variable in the left hand side.

Page 30: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Assignment Operator - Examples• int x; //decleration x = 5; //assignment• float y; y = 9.75;• char gender; gender = ‘M’• int a = 5 ; //decleration and assignment • int seconed = 10, third = 14;• int a,b,c,d = 10;• int a = 5;• int b,c,d; b,c,d = a; b,c,d = a + 3;

Page 31: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Assignment Operator - Examples

int a,b;a = 5;b = 8;cout<<a<<“ “<<b;

int a,b;a = 5;b = a;cout<<a<<“ “<<b;

Outputs:

Page 32: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Assignment Operator - Examples

int a,b;a = 5;b = 8;a = b;cout<<a<<“ “<<b;

int a,b;a = 5;a = b;cout<<a<<“ “<<b;

Outputs:

Page 33: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Assignment Operator - Examples

int a,b;a = 5;b = A;cout<<a<<“ “<<b;

int a;a = 5;b = a;cout<<a<<“ “<<b;

Outputs:

Page 34: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Assignment Operator - Example

#include <iostream> using namespace std; void main() {

int x;

float y=12.52;

x=y;

cout<<″value of x= ″<<x;

}

Outputs:

Page 35: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Assignment Operator - Example

#include <iostream> using namespace std; void main() {

int x=11;

float y=x;

cout<<″value of y= ″<<y;

}

Outputs:

Page 36: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Arithmetic Operators

• They are used to perform arithmetic operations. • They can take any one of the data types.

+ Addition

- Subtraction/ Division

* Multiplication

% Used to find out remainder

Page 37: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Arithmetic Operators (cont.)Result Operations Operand-2 (b) Operand-1 (a) int +, -, *, / int intfloat +, -, *, / float intfloat +, -, *, / int

float

float +, -, *, / float floatdoube +, -, *, / double floatLong double +, -, *, / double Long double

Note that the result always takes the larger data type of the operands used.

Page 38: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Arithmetic Operators (cont.)

• 4.2+3 =• 3*4 =• 3*4.0 = • 3*4.2 = • 17/3 = • 17.0/3 =• 17%3 =• 12.6 /2 =

Page 39: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Arithmetic Operators - Example

#include <iostream> using namespace std; void main() { int x=10, y=3; cout<<″value of x= ″<<x/y;}

Outputs:

Page 40: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Arithmetic Operators - Example

#include <iostream> using namespace std; void main() { int x=10; float y=3.5; cout<<″value of x= ″<<x/y;}

Outputs:

Page 41: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Precedence

• The term ``precedence'' refers to how ``tightly'' operators bind to their operands (that is, to the things they operate on).

• In mathematics, Multiplication, division, and modulus, all have higher precedence than addition and subtraction.

• Same precedence : left to right which come first• 1 + 2 * 3• 2 * 5 * 5 + 3 * 5 + 7• 8 + 5 * 7 % 2 * 4• 20 – 2 / 6 + 3 * 5 / 2• 2 * 3 / 12 * 18 / 4• 4 / 2 / 2 / 5 / 7• 3 * 5 / 2 + ( 2 * ( 3 + 7 ) ) / 5

Page 42: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Precedence (cont.)

Assume that: int amount = 1 , m = 50 , n = 10, p = 5;

Evaluate the following expressions:

• n / p + 3

• m / p + n – 10 * amount

• -m / 20

• (m + n) / p + amount

• (m + n) / (p + amount)

Page 43: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Unary operators

• An operator acts up on a single operand to produce a new value is called a unary operator.

• The decrement and increment operators ++ and -- are unary operators. They increase and decrease the value by 1.

• if x=3 ++x produces 4 and --x produces 2.

• Note: in the place of ++x , x++ can be used, but there is a slight variation. In both cases x is incremented by 1, but in the latter case x is considered before increment.

Page 44: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Unary operators - Example

#include <iostream> using namespace std; void main( ) { int x=10; ++ x; // as x = x + 1 cout << x; }

Outputs:

Page 45: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Unary operators - Example

#include <iostream> using namespace std; void main() { int x=10; x++; // as x = x + 1 cout << x; }

Outputs:

Page 46: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Unary operators - Example

#include <iostream> using namespace std; void main() { int x=10; cout << ++x; cout << x++; cout << x; }

Outputs:

Page 47: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Relational Operators

Less than < Greater than > Less than or equal to <=

Greater than or equal to >= Equal to == Not equal to !=

Page 48: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Relational Operators (cont.)

• 3 > 4

• 2.0 > 3.75

• ‘A’ > ‘C’

• ‘D’ <= ‘Z’

• ‘B’ != ‘C’

• 30 => 50

• x = = y

• k = 12, j=7, i=5

k + 3 <= -j + 3 * i

Page 49: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Relational Operators - Example

#include <iostream> using namespace std; void main() { cout<<“The value of 3<4 is "<<(3<4); cout<<“The value of 2.0>3.5 is “<<(2.0>3.5); int a = 5; int b = 8; cout<<“The value of a>=b is "<<(a>=b);}

Outputs:

Page 50: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Logical Operations

• The relational operators work with arbitrary numbers and generate true/false values.

• You can also combine true/false values by using the Boolean operators, which take true/false values as operands and compute new true/false values.

• The three Boolean operators are:

Page 51: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Logical Operations (cont.)

• (a>b) && (b>c) is true if (a>b) is true and (b>c) is true

• (a>b) && (b>c) is false if one of them is false or both are false

• (a>b) || (b>c) is true if either (a>b) is true or (b>c) is true or both are true

• (a>b) || (b>c) is false when both are false

• (a>b) & (b>c) cause an Error

• (a>b) & & (b>c) cause an Error

Page 52: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Logical Operations (cont.)

Assume that: int a = 12, b = 2, i = 15, j = 15, c = 0;Evaluate the following expressions?

Result Expression

1 ( a == 12 )

0 ( b == 3 )

1 ( a >= 12 ) && ( b < 3 )

0 ( a / b > 6 ) && ( b < i )

1 ( j == 20 ) || ( i == 15 )

0 ( I < j )||( a < b )||c

1 ! ( i != j )

1 (!i – j )

0 !! ( i – j )

1 !(!i * !!j)

Page 53: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Compound operators

• C++ allows the assignment to combine with other operators.

• Examples:

• Z+=5; //is equal to Z=Z+5;

• Z-=5; //is equal to Z=Z-5

• x*=y; //is equal to x=x*y

• x/=5; //is equal to x=x/5

• X%=5;//is equal to x=x%5

Page 54: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Compound operators (cont.)

Assume that: int c = 3, d = 5, e = 4, f = 6, g = 12;

Evaluate the following expressions:

• c +=7

• d -= 4

• e *= 5

• f /= 3

• g %= 9

Page 55: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Compound operators - Example

#include <iostream> using namespace std; void main() { int x=10, y=30; y /= x; x = y++; cout << x<<“\t”<<y; }

Outputs:

Page 56: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Compound operators - Example

#include <iostream> using namespace std; void main() { int x=10, y=20; y*= x; x += y; cout << x<<“\t”<<y; }

Outputs:

Page 57: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Compound operators - Example

#include <iostream> using namespace std; void main() { int x=10, y=40; y* = x; x = x++; cout <<x<<“\t”<<y; }

Outputs:

Page 58: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Precedence

Page 59: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Computing with C++ // Sample program// IEA September 2013// Reads values for the length and width of a rectangle and returns the perimeter and area#include <iostream> using namespace std; void main() { int length, width; int perimeter, area; // declarations cout << "Length = "; // prompt user cin >> length; // enter length cout << "Width = "; // prompt user cin >> width; // input width perimeter = 2*(length+width); // compute perimeter area = length*width; // compute area cout << endl << "Perimeter is " << perimeter; cout << endl << "Area is " << area << endl; // output results} // end of main program

Page 60: CHAPTER 6: PROBLEM SOLVING CONCEPTS FOR THE COMPUTER PROGRAMMING Lec. Ghader R. Kurdi

Exercises Q1: Write a single C++ statement to accomplish each of the following:

• Declare variables sum and x to be of type int

• Set variable x to 1

• Set variable sum to 0

• Add variable x to variable sum and assign the result to variable sum

• Print “the sum is: “ followed by the value of variable sum

Q2: Write four different C++ statement that each add 1 to variable x.