1 CSE1301 Computer Programming Lecture 5 C Primitives 2.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Computer Programming w/ Eng. Applications
1 C Fundamentals - I Math 130 Lecture # 2 B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Sp05: With discussion.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
Types and Arithmetic Operators
CSE1301 Computer Programming Lecture 4: C Primitives I.
Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note:
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
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.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
The C Programming Lecture 24.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Chapter 2 Getting Started in C Programming
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Numeric Types, Expressions, and Output ROBERT REAVES.
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Khalid Rasheed Shaikh Computer Programming Theory 1.
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 OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
Recap……Last Time [Variables, Data Types and Constants]
Operators & Expressions
Doing math In java.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3 The New Math. C++ Data Types simple integral charshort intlong bool floating float double Long double enum address pointer reference structured.
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 Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
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.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
Lecture2.
Arithmetic Expressions
INSPIRING CREATIVE AND INNOVATIVE MINDS
Computer Programming: C language
Introduction to C Programming
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
Chapter 4 - Program Control
C Operators, Operands, Expressions & Statements
Lecture3.
EECE.2160 ECE Application Programming
Chapter 4: Expression and Operator
EECE.2160 ECE Application Programming
Arithmetic Expressions Lesson #1 Outline
Presentation transcript:

1 CSE1301 Computer Programming Lecture 5 C Primitives 2

2 Topics Expressions Precedence Function calls Comments printf(“Hello World”);

3 Expressions Combine values using operators and function calls Return a value of a known type

4 Arithmetic Expressions take arithmetic (numerical) values and return an arithmetic (numerical) value Are composed using the following operators: + (unary plus) - (unary minus) + (addition) - (subtraction) * (multiplication) / (division or quotient) % (modulus or remainder)

5 Precedence in Expressions Defines the order in which an expression is evaluated

6 Precedence in Expressions – Example * / 5 = B stands for brackets, O for Order (exponents), D for division, M for multiplication, A for addition, and S for subtraction. B.O.D.M.A.S. 1 + (2 * 3) - (4 / 5)

7 More on precedence *, /, % are at the same level of precedence +, - are at the same level of precedence For operators at the same “level”, left-to-right ordering is applied – 1 = (2 + 3) – 1 = 4 2 – = (2 – 3) + 1 = 0 2 * 3 / 4 = (2 * 3) / 4 = 6 / 4 2 / 3 * 4 = (2 / 3) * 4 = 0 * 4

8 Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)

9 Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)

10 Precedence in Expressions – Example (cont) Integer division results in integer quotient * / 5 = 1 + (2 * 3) - (4 / 5)

11 Precedence in Expressions – Example (cont) = 0 D’oh * / 5 = 1 + (2 * 3) - (4 / 5)

12 Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)

13 int -s and float -s float is a “communicable” type Example: * / 5 = 1 + (2 * 3) - (4.0 / 5) = = 6.2

14 int -s and float -s – Example 2 (1 + 2) * (3 - 4) / 5 = ((1 + 2) * (3 - 4)) / 5 = (3 * -1) / 5 = -3 / 5 = 0

15 int -s and float -s – Example 2 (cont) ( ) * (3 - 4) / 5 = (( ) * (3 - 4)) / 5 = (3.0 * -1) / 5 = -3.0 / 5 = -0.6

16 int -s and float -s – Example 3 ( ) * ((3 - 4) / 5) = ( ) * (-1 / 5) = 3.0 * 0 = 0.0

17 Unary operators Called unary because they require one operand Example i = +1;/* + used as a unary operator */ j = -i;/* - used as a unary operator */ The unary + operator does nothing (just emphasis that a numeric constant is positive) The unary – operator produces the negative of its operand

18 Increment and decrement operators ++ is the increment operator i++; is equivalent to i = i + 1; -- is the decrement operator j--; is equivalent to j = j - 1; (King, pp53-54)

19 Evaluate an expression set result to * / 5 output result #include Example – Simple Expressions

20 Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ Example – Simple Expressions (cont)

21 Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { return 0; } Example – Simple Expressions (cont)

22 Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { float result; return 0; } Example – Simple Expressions (cont)

23 Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { float result; result = * / 5; return 0; } Example – Simple Expressions (cont)

24 Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { float result; result = * / 5; printf(“%f\n”, result); return 0; } Example – Simple Expressions (cont)

25 Evaluate an expression set result to * / 5 output result Output: #include /* Evaluate an expression */ int main() { float result; result = * / 5; printf(“%f\n”, result); return 0; } Example – Simple Expressions (cont)

26 Topics Expressions Precedence Function calls Comments

27 Function Calls Tell the computer to execute a series of C commands and (maybe) return a value –In algorithm-speak: An invocation of a named sequence of instructions Example: printf, scanf, sqrt

28 Find the square root of x output ”Enter a number: " input x set myResult to result of squareRoot(x) output myResult Example – Find the square root

29 Example – Find the square root (cont) #include /* Find square root */ int main() { /* declare variables */ float x,myResult; /* output ”Enter a number: " */ printf(“Enter a number\n”); /* input x */ scanf(“%f”,&x); /* set myResult to result of squareRoot(x) */ myResult=sqrt(x); /* output myResult */ printf(“Result is %f\n”,myResult); return 0; }

30 Topics History of C Structure of a C program Values and variables Expressions Function calls Comments

31 Comments Essential for documenting programs Run from a /* to the next */ Examples: /* THIS IS A COMMENT */ /* So is this*/ /* **...and this. ** */

32 Comments (cont) Comments do not “nest” /*Comments start with a “/*” and end with a “*/” but they don’t nest! */

33 Topics Expressions Precedence Function calls Comments

34 Reading King –Chapter 2, 2.1 – 2.3 –Chapter 4, 4.1 D&D: –Chapter 2, Sections 2.1 to 2.5 Kernighan & Ritchie –Chapter 1, 1.2 –Chapter 2, 2.1 – 2.3, 2.5 – 2.7, 2.12