Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Chapter 12 Variables and Operators. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Basic C Elements.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 12 Variables and Operators. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Basic C Elements.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Operators & Expressions
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.
INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
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.
Basics of ‘ C ’ By Gaikwad Varsha P. Asst. Prof. Information Technology Dept. Govt. College of Engg. Aurangabad.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CSE 220 – C Programming Expressions.
Chapter 12 Variables and Operators
BASIC ELEMENTS OF A COMPUTER PROGRAM
Tokens in C Keywords Identifiers Constants
ITEC113 Algorithms and Programming Techniques
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.
INC 161 , CPE 100 Computer Programming
Variable Declaration, Data types, Expressions
Introduction to C Programming
Operators and Expressions
Chapter 12 Variables and Operators
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
More about Numerical Computation
Introduction to C Programming
Chapter-3 Operators.
Lectures on Numerical Methods
Chapter 3 Operators and Expressions
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
OPERATORS in C Programming
Presentation transcript:

Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Wed Jul 3, 2002

Today... Comments Reserved Words: what cannot be used as a variable name. More on Data Types: sizes, ranges Type conversion Characters

more Today... if statements if - else Exercises Programming a simple calculator

Comments anything between ‘/*’ and ‘*/’ are ignored. So, you can type your comments, poems, explanations, reminders enclosed within /* and */. Thus: /*this is a comment*/ int /*hahaha!*/ x =/**/ 5; is same as: int x=5;

Reserved Words autodoubleintstruct breakelselongswitch caseenumregistertypedef charexternreturnunion constfloatshortunsigned continueforsignedvoid defaultgotosizeofvolatile doifstaticwhile

Data-typeSizeRange char1 bytes-128 to 127 unsigned char1 bytes0 to 255 short2 bytes to unsigned short2 bytes0 to int4 bytes to unsigned int4 bytes0 to long4 bytes to unsigned long4 bytes0 to float (6 digits)4 bytes e-38 to e+38 double (15 digits)8 bytes e-308 to e+308

Type Conversion conversions among different data types may occur while evaluating an expression. int x=3.4, y=4; float z = x; char c = z; Two type of conversions: –Implicit (automatic) –explicit (type-cast)

Implicit (automatic) Type Conversion If operands are of the same type, the result will be of this type too float x,y,z; z = x+y; /*x+y is also float*/ If operands are of different types, they will be converted to the “broader” type (i.e., int to float) before the calculation int x; float y, z; z = x+y; /*x+y is float*/

Beware! short s; int i; long l; float f; double d; Safe conversion (to broader type) l=i; l=s; i=s; d=i; f=i; /*100 converted to d=f; /*?*/ Unsafe conversion (loosing information) i=f; /* truncation: 3.14 to 3 */ s=l; /* dropping bits */ f=d; /* truncation/rounding */

Arithmetic Exercise int x=9; float f, k=2; x = (x/ ); /* x=? */ x += (x%5 + 1); /* x=? */ x = (x/ ); /* x=? */ f = 5/2 + 5/k + k/0.5; /*f=?*/

Explicit Conversion (type-cast) Most compiler give warnings for possibly unsafe conversions. One type of data can be explicitly forced into another data type. float f=4.3; int x, y=2; x=3.7; /*compiler warning*/ x=(int) f; /*ok*/ f = x/(float)y;

Characters Character constants are given in single quotes. ‘a’, ‘A’, ‘&’ are characters. There are special characters like: ‘\n’, ‘\0’, ‘\t’ Character constants are represented numerically from 0 to 255. char c = ‘e’; int x = c – ‘b’; /* x = ? */

Operators in C Unary Binary Assignment comma

Unary Operators sizeof(i) : the number of bytes of storage allocated to i +123 : positive : negative 123 ~i : one's complement (bitwise complement) !i : logical negation (i.e., 1 if i is zero, 0 otherwise) *i : returns the value stored at the address pointed to by i &i : returns the address in memory of i

unary Examples a = b = a | b = a &b = j=3; /* j= */ k=j<<2; /* k= */ /*k=12*/ m=j>>2; /*m= */ /*m=0*/

more Unary Operators ++i : adds one to i, and returns the new value of i --i : subtracts one from i, and returns the new value of i i++ : adds one to i, and returns the old value of i i-- : subtracts one from i, and returns the old value of i i[j] : array indexing i (j) : calling the function i with argument j i.j : returns member j of structure i i->j : returns member j of structure pointed to by i

Binary Operators + : addition - : subtraction * : multiplication / : division % : remainder (e.g., 2%3 is 2), also called 'modulo' << : left-shift (e.g., i<<j is i shifted to the left by j bits) >> : right-shift & : bitwise AND | : bitwise OR ^ : bitwise exclusive-OR

more Binary Operators && : logical AND (returns 1 if both operands are non-zero; else 0) || : logical OR (returns 1 if either operand is non-zero; else 0) < : less than (e.g., i<j returns 1 iff i is less than j) > : greater than <= : less than or equal >= : greater than or equal == : equals != : does not equal ? : conditional operator

Assignment Operators x = is same as x = x = assignment += addition assignment -= subtraction assignment *= multiplication assignment /= division assignment %= remainder/modulus assignment &= bitwise AND assignment |= bitwise OR assignment ^= bitwise exclusive OR assignment <<= left shift assignment >>= right shift assignment

Comma operator multiple expressions can be separated by comma. The expressions are evaluated in right to left order, and the value of the overall expression is equal to that of the rightmost expression. x = ((y=3, 2)); /* y=3; x=2; */ x = ((y=3, y+1)); /* y=3; x=4; */

evaluation order of Operators precedence: the order in which operators are applied. the higher precedence operators are applied first. associativity: the order in which operators of the same precedence are applied.

OperatorAssociativity () [] ->>.left-to-right -+ (unary) ! ~ * & sizeof (type)right-to-left * / %left-to-right + - (arithmetic)left-to-right >left-to-right >=left-to-right == !=left-to-right & ^ | &&left-to-right ||left-to-right ?:right-to-left = += -= *= /= %= &= ^= |= >=right-to-left,left-to-right highest precedence lowest precedence

int x, y=0; x = ++y; /* x = ?, y = ? */ x = y++; /* x = ? y = ? */ x = (++y); /* x = ? y = ? */ x = (y++); /* x = ? y = ? */ Question...

Expressions, Statements Expression: consists of operands (variables or constants) and operators: –Relational: x==0, x<y, x!=10 –Arithmetic: x+2, x++, 2*5 –Assignment: x=5, y=x Statements: –declarative: int x; –simple: x=3; –compound: { x*=4; printf(“%d”, x); } –conditional: if, if-else, switch –controlling (loops): while, do-while, for, (goto)

Programming Goal Simple Calculator: Write a program that reads two integers separated with one of the four arithmetic operators, and prints out the result of this arithmetic expression.

If Statement if( ) –test can be a relational expression, or any integer-valued expression. –statement can be simple: one-statement ending with one-semicolon. compound: block of statements surrounded with {...} or, the statement can be another if-statement.

A note on relational statements 0 (zero) means ‘false’, and any number other than zero means ‘true’. also, a conditional truth evaluates to a numerical 1, whereas falsity evaluates to 0. x = (3==2); /* x = ? */ x = (4>-5); /* x = ? */

if examples if(sayi==10) printf(“sayi 10’a esittir.”); if(!(sayi<10)) printf(“sayi >=10 dur.”); if(sayi) printf(“sayi 0 degildir.”);

int x=3; if(x != 3) printf(“t”); printf(“a”); if(x % 2){ printf(“hm”); if(x>0){ printf(“e”); } if(x-3) if(x==3) printf(“s”); printf(“t”); Find the output of the following:

if-else statement if ( ) else if(x==y) printf(“x is equal to y”); else print(“x is NOT equal to y”);

you already know: (if... else if...) if(x==y) printf(“x is equal to y”); else if(x>y) printf(“x is greater than y”); else printf(“x is smaller than y”);.

Tip for if statements: if ( ) To solidify the scope of the if statements, follow the procedure. –Look at right after the ‘)’ that closes the. if there’s a ‘{‘, then the scope of the ‘if’ ends at the corresponding ‘}’. if not, –search for the first semicolon ‘;’ after the ‘)’. The semicolon will determine the end of the if- statement.

exercises (a) #include int main() { int gender = 2; gender--; if (Gender == 1) printf("The student is male\n"); else printf("The student is female\n"); return 0; }

exercises (b) #include int main() { int gender = 2; gender--; if (gender == 1) printf("The student is male\n"); else; printf("The student is female\n"); return 0; }

exercises (c) #include int main() { int gender = 2; if (--gender == 1) printf("The student is male\n"); else printf("The student is female\n"); return 0; }

exercises (d) #include int main() { int gender = 2; if (gender-- == 1) printf("The student is male\n"); else printf("The student is female\n"); return 0; }

exercises (e) #include int main() { int gender = 2; gender--; if (gender == 1); printf("The student is male\n"); else printf("The student is female\n"); return 0; }

exercises (f) #include int main() { int gender = 2; gender = ((gender / 2) % 2) + 0.9; if (gender == 1) printf("The student is male\n"); else printf("The student is female\n"); return 0; }

Programming Homework Simple Calculator: Write a program that reads two integers separated with one of the four arithmetic operators, and prints out the result of this arithmetic expression.