Chapter 9 Programming Languages BrooksCole 2003 OBJECTIVES After

Chapter 9 Programming Languages ©Brooks/Cole, 2003

OBJECTIVES After reading this chapter, the reader should be able to: Have a vision of computer language evolution. Distinguish between machine, assembly, and high-level languages. Understand the process of creating and running a program. Distinguish between the different categories of languages: procedural, object-oriented, functional, declarative, and special. Become familiar with elements of the procedural language C. ©Brooks/Cole, 2003

9. 1 EVOLUTION ©Brooks/Cole, 2003

Figure 9 -1 Evolution of computer languages ©Brooks/Cole, 2003

Program 9. 1 Program in machine language 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0000000100 0101111000001100 00000000 11000010 000000010 11101111 00010110 0000000101 11101111 10011110 0000001011 11111000101011011111 00000010010 01100010 11011111 00000010101 1110111100000010 11111011 00000010111 11110100101011011111 00000011110 0000001110100010 11011111 00000100001 1110111100000010 11111011 00000100100 01111110100 10101101 1111100010101110 11000101 00000101011 0000011010100010 11111011 00000110001 1110111100000010 11111011 00000110100 0000000000111101 00000100 00000111101 ©Brooks/Cole, 2003

Note: The only language understood by a computer is machine language. ©Brooks/Cole, 2003

Program 9. 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Multiplication Program in symbolic language Entry subl 2 jsb movab main, ^m<r 2> #12, sp C$MAIN_ARGS $CHAR_STRING_CON pushal calls mull 3 pushal calls clrl ret -8(fp) (r 2) #2, read -12(fp) 3(r 2) #2, read -8(fp), -12(fp), 6(r 2) #2, print r 0 ©Brooks/Cole, 2003

Multiplication Program in C++ Program 9. 3 language 1 /* This program reads two integer numbers from the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 keyboard and prints their product. */ #include <iostream. h> int main (void) { // Local Declarations int number 1; int number 2; int result; // Statements cin >> number 1; cin >> number 2; result = number 1 * number 2; cout << result; return 0; } // main ©Brooks/Cole, 2003

9. 2 BUILDING A PROGRAM ©Brooks/Cole, 2003

Figure 9 -2 Building a program ©Brooks/Cole, 2003

9. 3 PROGRAM EXECUTION ©Brooks/Cole, 2003

Figure 9 -3 Program execution ©Brooks/Cole, 2003

9. 4 CATEGORIES OF LANGUAGES ©Brooks/Cole, 2003

Figure 9 -4 Categories of languages ©Brooks/Cole, 2003

Figure 9 -5 Function in a functional language ©Brooks/Cole, 2003

Figure 9 -6 Extracting the third element of a list ©Brooks/Cole, 2003

Table 9. 1 Common tags Beginning Tag --------<HTML> <HEAD> <BODY> <TITLE> <Hi> <B> <I> <U> <SUB> <SUP> <CENTER> <BR> <OL> <UL> <LI> <IMG> <A> Ending Tag --------</HTML> </HEAD> </BODY> </TITLE> </Hi> </B> </I> </U> </SUB> </SUP> </CENTER> </OL> </UL> </LI> </A> Meaning --------------document head document body document title different header levels boldface Italic underlined subscript superscript centered line break ordered list unordered list an item in the list an image an address (hyperlink) ©Brooks/Cole, 2003

Program 9. 4 HTML Program <HTML> <HEAD> <TITLE> Sample Document </TITLE> </HEAD> <BODY> This is the picture of a book: <IMG SRC="Pictures/book 1. gif" ALIGN=MIDDLE> </BODY> </HTML> ©Brooks/Cole, 2003

9. 5 A PROCEDURAL LANGUAGE: C ©Brooks/Cole, 2003

Figure 9 -7 Variables ©Brooks/Cole, 2003

Table 9. 2 Arithmetic operators Operator --------+ * / % -----++ -- Definition --------Addition Subtraction Multiplication Division (quotient) Division (remainder) -----------Increment Decrement Example -----------3 + 5 2 - 4 Num * 5 Sum / Count % 4 -----------Count ++ Count -- ©Brooks/Cole, 2003

Table 9. 3 Relational operators Operator --------< <= > >= == != Definition --------Less than or equal to Greater than or equal to Equal to Not equal to Example -----------Num 1 < 5 Num 1 <= 5 Num 2 > 3 Num 2 >= 3 Num 1 == Num 2 Num 1 != Num 2 ©Brooks/Cole, 2003

Table 9. 4 Logical operators Operator --------! && || Definition --------NOT AND OR Example -----------! ( Num 1 < Num 2 ) (Num 1 < 5 ) && (Num 2 > 10 ) (Num 1 < 5 ) || (Num 2 > 10 ) ©Brooks/Cole, 2003

Table 9. 5 Assignment operators Operator --------== += -= *= /= %= Example --------Num =5 Num += 5 Num -= 5 Num *= 5 Num /= 5 Num %= 5 Meaning -----------Store 5 in Num = Num + 5 Num = Num - 5 Num = Num * 5 Num = Num / 5 Num = Num % 5 ©Brooks/Cole, 2003

Figure 9 -8 Statements ©Brooks/Cole, 2003

Figure 9 -9 Side effect of a function ©Brooks/Cole, 2003

Figure 9 -10 Function declaration ©Brooks/Cole, 2003

Figure 9 -11 if-else statement ©Brooks/Cole, 2003

Figure 9 -12 switch statement ©Brooks/Cole, 2003

Figure 9 -13 while loop ©Brooks/Cole, 2003

Figure 9 -14 for loop ©Brooks/Cole, 2003

Figure 9 -15 do-while loop ©Brooks/Cole, 2003
- Slides: 32