BBS 514 - Yapısal Programlama (Structured Programming)1 Lexical Elements Kinds of tokens in C : Keywords Identifiers Constants Operators Punctuators.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 01 Savitch Ch. 2 C++ Basics Flow Of Control.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Computer Programming w/ Eng. Applications
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
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.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
JavaScript, Third Edition
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
Basic Elements of C++ Chapter 2.
Expressions, Data Conversion, and Input
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
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.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
LESSON 6 – Arithmetic Operators
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 CSC103: Introduction to Computer and Programming Lecture No 6.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive 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.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Variables Symbol representing a place to store information
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chap. 2. Types, Operators, and Expressions
Tokens in C Keywords Identifiers Constants
ITEC113 Algorithms and Programming Techniques
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Computing with C# and the .NET Framework
Java Programming: From Problem Analysis to Program Design, 4e
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Lecture 3 Expressions Richard Gesick.
Numerical Data Types.
Chapter 2: Basic Elements of Java
Lectures on Numerical Methods
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Expressions and Assignment
elementary programming
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Lexical Elements & Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Presentation transcript:

BBS Yapısal Programlama (Structured Programming)1 Lexical Elements Kinds of tokens in C : Keywords Identifiers Constants Operators Punctuators

BBS Yapısal Programlama (Structured Programming)2 Keywords autobreakcasecharconst continuedefaultdodoubleelse enumexternfloatforgoto ifintlongregisterreturn shortsignedsizeofstaticstruct switchtypedefunionunsignedvoid volatilewhile

BBS Yapısal Programlama (Structured Programming)3 Identifiers Sequence of letters, digits, and the special character _. A letter or underscore must be the 1 st character of an identifier. –For this class, don’t use identifiers that begin with an underscore. C is case-sensitive: –Apple and apple are two different identifiers.

BBS Yapısal Programlama (Structured Programming)4 Identifiers (cont.) Valid variable names: n x _id num1 a_long_identifier Invalid variable names: var.1 num!2 not#this 126East +more

BBS Yapısal Programlama (Structured Programming)5 Declarations All variables must be declared before use. A declaration specifies a type, and contains a list of one or more variables of that type. int lower, upper, step; int c, line; A variable may be initialized in its declaration. int i = 0; Variables for which there is no explicit initialization have undefined (garbage) values.

BBS Yapısal Programlama (Structured Programming)6 Constants C manipulates various kinds of values. integer constants: 0, 37, 2001 floating constants: 0.8, , 1.0 character constants: ‘a’, ‘5’, ‘+’ string constants: “a”, “Monday”

BBS Yapısal Programlama (Structured Programming)7 Arithmetic Operators The binary arithmetic operators are +, -, *, /, and the modulus operator %. x % y produces the remainder when x is divided by y. 11 % 5 = 1 20 % 3 = 2 Arithmetic operators associate left to right.

BBS Yapısal Programlama (Structured Programming)8 Precedence and Associativity of Operators Operator Precedence: x = * 3; (What is the value of x?) x = 1 + (2*3); x is 7? or x= (1+2) * 3; x is 9? Associativity: (left to right) – / 5 * 2

BBS Yapısal Programlama (Structured Programming)9 Assignment Operator The expression can simply be a constant or a variable: int x, y; x = 5; y = x; x = 6; The expression can be an arithmetic expression: x = y + 1; y = x * 2; variable = expression

BBS Yapısal Programlama (Structured Programming)10 Assignment Compatibility int x = 3; double y =12.8; x = y;truncates y! y = x;it is okay. x = ;truncates the result! y = ;it is okay. x = 10/4;x is 2 y = 10/4;y is 2.0 y = 10/4.0;y is 2.5

BBS Yapısal Programlama (Structured Programming)11 Increment and Decrement Operators The increment operator ++ adds 1 to its operand. ++i; equiv. i = i + 1; i++; equiv.i = i + 1; The decrement operator -- subtracts 1 from its operand. --i; equiv. i = i - 1; i--; equiv. i = i - 1;

BBS Yapısal Programlama (Structured Programming)12 Increment and Decrement Operators Suppose n = 5. n++; /* sets n to 6 */ n = n + 1; ++n; /* sets n to 6 */ n = n + 1;

BBS Yapısal Programlama (Structured Programming)13 Increment and Decrement Operators (cont.) When ++a is used in an expression, the value of a is incremented before the expression is evaluated. When a++ is used, the expression is evaluated with the current value of a and then a is incremented. Similarly, with --a and a--.

BBS Yapısal Programlama (Structured Programming)14 Increment and Decrement Operators (cont.) Suppose n = 5. x = n++; /* sets x to 5 and n to 6 */ 1. x = n; 2. n = n + 1; x = ++n; /* sets x and n to 6 */ 1. n = n + 1; 2. x = n;

BBS Yapısal Programlama (Structured Programming)15 /* Preincrementing and postincrementing*/ #include int main(void) { int c; c = 5; printf(“%d\n”,c); printf(“%d\n”, c++); printf(“%d\n\n”,c); c = 5; printf(“%d\n”, c); printf(“%d\n”, ++c); printf(“%d\n”, c); return 0; }

BBS Yapısal Programlama (Structured Programming)16 /*** increment and decrement expressions ***/ #include int main(void) { int a =0, b = 0, c = 0; a = ++b + ++c; printf(“\n%d %d %d”, a,b,c); a = b++ + c++; printf(“\n%d %d %d”, a,b,c); a = ++b + c++; printf(“\n%d %d %d”, a,b,c); a = b c; printf(“\n%d %d %d”, a,b,c); return 0; }

BBS Yapısal Programlama (Structured Programming)17 Arithmetic Assignment Operators Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; Operator ExpressionExplanation Assigns +=c += 7 c = c to c -= d -= 4 d = d to d *= e *= 5 e = e * 5 20 to e /= f /= 3 f = f / 3 2 to f %= g %= 9 g = g % 9 3 to g

BBS Yapısal Programlama (Structured Programming)18 Exercise 1 Given int i=2, j = 3, k =4; Evaluate the following: 1.k *= i – j; 2.i += j = k; 3.j -= -k i;

BBS Yapısal Programlama (Structured Programming)19 Exercise 2 Determine the value of the following expressions: * (2 * 2 – 2) % 2 / *( ( 8 + 7) % 6) + 5 * 4 % 3 * * (float) 6 / 5

BBS Yapısal Programlama (Structured Programming)20 Exercise 3 1.Given double x; int y = 4; What is the result of the following? x = y/3+(y-1.0)/y; 2.Given double x = 2.5; What is the value of x? x = (int)x*3+x;

BBS Yapısal Programlama (Structured Programming)21 Programming Exercises 1.Write a program that reads a four digit number and finds the sum of individual digits. 2.The distance d between points (x 1, y 1 ) and (x 2, y 2 ) is given by d = ( ( x 1 – x 2 ) 2 + (y 1 – y 2 ) 2 ) ½ Write a program that reads the coordinates of the two points and calculates the distance between them