1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Chapter 2: Basic Elements of C++
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
1 Lexical Elements Chapter 2 in ABC. 2 Tokens Token = word / symbol, does not contain white spaces. Tokens in C are either: –keywords –identifiers –constants.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Chapter 2: Basic Elements of C++
JavaScript, Fourth Edition
JavaScript, Third Edition
Chapter 2: Basic Elements of C++
1 Structured Programming in C Welcome to CPSC 206.
String Escape Sequences
Basic Elements of C++ Chapter 2.
Variable & Constants. A variable is a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
CS2311 Computer Programming Dr. Yang, Qingxiong (with slides borrowed from Dr. Xu, Henry) Lecture 2: Basic Syntax – Part I: Variables and Constants.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
LESSON 6 – Arithmetic Operators
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Chapter 2 Variables.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
Chapter 2: Basic Elements of C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
Chapter 2: Basic Elements of C++. Objectives In this chapter, you will: – Become familiar with the basic components of a C++ program, including functions,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
C++ First Steps.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 3: Operators, Expressions and Type Conversion
Tokens in C Keywords Identifiers Constants
Wel come.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Data Types, Identifiers, and Expressions
Java Programming: From Problem Analysis to Program Design, 4e
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to C++ Programming
Basics of ‘C’.
Chapter 2: Basic Elements of Java
elementary programming
Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’t be nested! Be careful. /* /* /* Hi */ is an example.
Lexical Elements & Operators
Chapter 12 Variables and Operators
Presentation transcript:

1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.

2 Syntax of C  Like any language, C has an alphabet and rules for putting together words and punctuation to make legal program; that is called syntax of the language  C compilers detect any violation of the syntactic rule within the program  C compilers collect the characters of the program into tokens, which form the basic vocabulary of the language  Tokens are separated by space

3 Tokens  Tokens in C can be categorized into:  keywords, e.g., return, int  identifiers, e.g., user-defined variables, identifiers used in preprocessing statements and function names  character constants, e.g. ‘c’  string constants, e.g., “Hello”  numeric constants, e.g., 7, 11, 3.14  operators, e.g., = (assignment operator), ++ (increment operator)  punctuators, e.g., ; and,

4 Keywords

5 Keywords ( cont’d )  There are only 32 keywords (C is small)  Each keyword in C has a reserved meaning and cannot be used as identifiers  Most of the keywords given in the previous page will be covered in this course; some keywords are deliberately omitted  Note: main is NOT a keyword. However, every program will use it as a function name. Therefore you should not use it as variable names.

6 Identifiers  An identifier is composed of a sequence of letters, digits and underscore  An identifier must begin with either a letter or an underscore (not recommended)  Identifiers give unique names to various objects in a program; reserved keywords such as float and int cannot be used as identifiers  In ANSI C, at least the first 31 characters of an identifier are discriminated  Always use meaningful names for identifiers!!!

7 Constants  Constants can be numeric-, character- or string- based, e.g., 13, 7.11, ‘\n’, “Tuesday”  Numbers are represented in decimal system but octal (preceded by 0) or hexadecimal integers (preceded by 0x) can also be represented, e.g., the following number are equal to a decimal /* an octal integer */ 0x1a /* an hexadecimal integer */

8 Constants ( cont’d )  Character constants are enclosed by single quotation marks, e.g., ‘a’, ‘\n’, ‘\t’  String constants are delimited by double quotes, e.g. “Hello world\n”  String is treated as an array of characters in C  If “ or newline characters are to be used within a string, it has to be preceded by a backslash \

9 Operators  Some types of operators:  Arithmetic, e.g. +, -, *, /, %, ++, --  Assignment, e.g., =  Relational  Boolean (logical) and a few more …  Some symbols have meaning that depends on context, e.g., printf(“%d”, 40%7);

10 Punctuators  Punctuators such as, and ; are used to separate language elements, e.g., int a, b = 4, c = 4; a = b + c;

11 The / operators  The “ division ” operator, /, is used to compute the quotient when one number is divided by another  Example 1: float a = 20, b = 7, c; c = a/b; printf(“%f”, c); /* */  Example 2: int a = 20, b = 7; float c; c = a/b; printf(“%f”, c); /* */

12 The % operators  The “ modulus ” operator, %, is used to compute the remainder when one integer is divided by another  Example: int a = 41, b = 7; printf(“%d”, a%b); /* 6 */ printf(“%d”, 49%b); /* 0 */  Question: What is the value of -30%7 ?  Cannot be applied on floating point values

13 Expressions & statements  An expression is a meaningful combination of operators, variables and/or constants  E.g. a + b - 10  Every expression has a value  A valid expression followed by a semicolon is a statement.  E.g. a + b - 10; ( although this is not very useful)  There are other forms of statements. In general, a statement specifies some actions.

14 Assignment operator  The following is an assignment expression: a = 1 involving variable a, constant 1 and the assignment operator =  The value of the above expression is defined as 1.  The expression followed by a semicolon: a = 1; is an assignment statement.  Further examples: a = (b=32) + (c=23); a = b = c = 0;

15 Efficient assignment operators  Generic form of efficient assignment operators variable op= expression; where op is operator; the meaning is variable = variable op (expression);  Efficient assignment operators include += -= *= /= %= >>= <<= &= ^= |=

16 Efficient assignment operators  Examples: a += 5; is same as a = a + 5; a -= 5; is same as a = a - 5; a += b*c; is same as a = a + (b*c); a *= b+c; is same as a = a * (b+c);

17 Increment & decrement operators  In terms of the actions done on variable k,  k++ and ++k are equivalent to k=k+1  k-- and --k are equivalent to k=k-1  Post-increment and post-decrement: k ++ and k--  k ’ s value is altered AFTER evaluating the expression  int k=1, j; j=k++;/* result: j==1, k==2 */  Pre-increment and pre-decrement: ++ k and --k  k ’ s value is altered BEFORE evaluating the expression  int k=1, j=0; j=++k;/* result: j==2, k==2 */

18 Precedence & associativity of operators  An expression may have more than one operator and its precise meaning depends on the precedence and associativity of the involved operators  What is the value of variable d after executing each of the following statements int a = 1024, b = 8, c = 2, d; d = a*b+c; d = a–b–c; // (a-b)-c or a-(b-c)? d = a/b/c; // (a/b)/c or a/(b/c)?

19 Table of Precedence & Associativity  See p.397 of [Kelly & Pohl 2001] for a complete table with all operators in C.

20 Some good programming habits  Always use brackets ( ) to express your exact intention if you are not sure about the precedence/associativity  Try to avoid very complex expressions

21 Using #define directives  Two purposes: to define  symbolic constants, and  macros  The directives #define MAX_SCORE 100 causes all occurrences of MAX_SCORE in the C program to be replaced by 100 BEFORE the program is compiled.  It is a convention to name a symbolic constant with upper case letters.

22 Example use of #define /* To convert length in inches to cm */ #include #define RATIO 2.54 int main(void) { float inches, cm; printf(“Enter length in inches: ”); scanf(“%f”, &inches); cm = RATIO*inches; printf(“approximately %f in cm\n”, cm); return 0; }