Programming for Engineers: Operators, Expressions, and Statem

Similar documents
Operators & Expressions

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Chapter 3: Operators, Expressions and Type Conversion

Informatics Ingeniería en Electrónica y Automática Industrial

Programming in C++ 6. Floating point data types

Operators and Expressions:

CS110: PROGRAMMING LANGUAGE I

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Fundamental of Programming (C)

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Fundamentals of Programming

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Unit 3. Operators. School of Science and Technology INTRODUCTION

Computer System and programming in C

LECTURE 3 C++ Basics Part 2

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Programming in C++ 5. Integral data types

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

A flow chart is a graphical or symbolic representation of a process.

JAVA OPERATORS GENERAL

Lecture 3. More About C

Arithmetic type issues

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

These are reserved words of the C language. For example int, float, if, else, for, while etc.

UNIT- 3 Introduction to C++

Lecture 3 Tao Wang 1

SECTION II: LANGUAGE BASICS

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 3 Structure of a C Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

C Programming a Q & A Approach

Arithmetic Operators. Portability: Printing Numbers

ISA 563 : Fundamentals of Systems Programming

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Structures, Operators

Information Science 1

Reserved Words and Identifiers

DEPARTMENT OF MATHS, MJ COLLEGE

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Chapter 2: Using Data

QUIZ: What value is stored in a after this

Introduction to Computer Science Midterm 3 Fall, Points

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 4: Basic C Operators

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Information Science 1

Work relative to other classes

CS313D: ADVANCED PROGRAMMING LANGUAGE

C: How to Program. Week /Mar/05

by Pearson Education, Inc. All Rights Reserved.

CMPT 125: Lecture 3 Data and Expressions

Data Types and Variables in C language

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Lecture 02 C FUNDAMENTALS

1.00 Lecture 4. Promotion

CS102: Variables and Expressions

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Programming for Engineers Iteration

ECE 122 Engineering Problem Solving with Java

C Programming

CSC 1214: Object-Oriented Programming

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Expression and Operator

Chapter 2 - Introduction to C Programming

Objectives. In this chapter, you will:

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

1.3b Type Conversion

Prof. Navrati Saxena TA: Rochak Sachan

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

4. Inputting data or messages to a function is called passing data to the function.

Chapter 2, Part III Arithmetic Operators and Decision Making

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Topic 2: C++ Programming fundamentals

Chapter 2: Data and Expressions

A Fast Review of C Essentials Part I

A First Program - Greeting.cpp

Variables and Operators 2/20/01 Lecture #

CSCE 110 PROGRAMMING FUNDAMENTALS

Data Conversion & Scanner Class

Data Types. Data Types. Integer Types. Signed Integers

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

CHAPTER 3 Expressions, Functions, Output

Chapter 12 Variables and Operators

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 4 Homework Individual/Team (1-2 Persons) Assignment 15 Points

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Transcription:

Programming for Engineers: Operators,, and 28 January 2011 31 January 2011 Programming for Engineers: Operators,, and Statem

The While Loop A test expression is at top of loop The program repeats loop as long as expression is true Example: while(shoe < 18.5) { foot=scale*shoe+adjust; printf( %10.1f %15.2f inches n shoe,foot); shoe=shoe+1.0; } The program must modify test variable, or the loop is endless. Programming for Engineers: Operators,, and Statem

The Structure of a While Loop Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators Statement Terminology Data object: This is a general term for a region of data storage that can be used to hold values. Lvalue: This a name or expression that identifies a particular data object. Rvalue: This can be a constant, variable, or any expression that can yield a value. Operand: This is an rvalue on which an operator operates. Example: shoe = shoe + 1.0; Programming for Engineers: Operators,, and Statem

Arithmetic Operators Arithmetic Operators Precedence and Associativity More Arithmetic Operators Assignment Operator: = shoe = shoe +1.0; Addition Operator: + shoe = shoe + 1.0; Subtraction Operator: - DownCount = DownCount - 1; Sign Operators: + or - Lower = -5+Count; Multiplication Operator: * foot = SCALE *shoe + ADJUST; Division Operator: / shoe = shoe/0.356; Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators Precedence and Associativity Precedence Operator Associativity Highest () Left to Right + (unary) Right to Left / Left to Right + (binary) Left to Right Lowest = Right to Left Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators Precedence and Associativity Example One Consider the following line of code: y=6*12+5*20; The computer makes the calculation as follows: y=6*12+5*20; y=72+5*20; y=72+100; y=172; Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators Precedence and Associativity Example Two Consider the following line of code: z=12/3*2; The computer makes the calculation as follows: z=12/3*2; z=4*2; z=8; Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators Precedence and Associativity Example Three Consider the following line of code: top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); The computer makes the calculation as follows: top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); top = score = -(7) * 6 + (4 + 3 * (2 + 3)); top = score = -(7) * 6 + (4 + 3 * (5)); top = score = -(7) * 6 + (4 + 15); top = score = -(7) * 6 + 19; top = score = -7 * 6 + 19; top = score = -42 + 19; top = score = -23; top = -23; Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators The sizeof() Operator sizeof( ) is operator and not a function. sizeof( ) returns number of bytes storage for operand. sizeof return value of type size t Example 1: size t n; n=sizeof(long double); // n is 10 Example 2: float pi = 3.14159; n=sizeof(pi); // n is 4 Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators More Arithmetic Operators Modulus operator: % remainder = number%10; Increment operator: ++ number = shoe++*5 number = ++shoe*5 Decrement operator: number = shoe *5 number = shoe*5 Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators Precedence and Associativity Revisited Precedence Operator Associativity Highest (), expr ± ± Left to Right +,, sizeof(),(typecast), ± ± expr Right to Left + + expr, expr, /, % Left to Right +, (binary) Left to Right Lowest = Right to Left Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators Confusing Code with Increment Operators Consider the following code num = 5; while(num < 21) printf( %10d %10d n,num,num*num++); There are three options for output displayed: 1. 2. 3. 5 25 6 25 6 30 Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators Confusing Code with Increment Operators Consider the following code num = 5; while(num < 21) printf( %10d %10d n,num,num*num++); There are three options for output displayed: 1. 2. 3. 5 25 6 25 6 30 Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators More Confusing Code with Increment Operators Consider the following code n = 3; y= n++ + n++ Here, there are two options for the variables after this code: 1. 2. y=6 n=5 y=7 n=5 Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators More Confusing Code with Increment Operators Consider the following code n = 3; y= n++ + n++ Here, there are two options for the variables after this code: 1. 2. y=6 n=5 y=7 n=5 Programming for Engineers: Operators,, and Statem

Arithmetic Operators Precedence and Associativity More Arithmetic Operators ANSI-C and ++ or Sometimes ANSI-C standard fails to define what the result should be so that compiler writer may increase efficiency. Dont use increment and decrement operators on a variable that is part of more than one argument of a function. Dont use increment or decrement operators on a variable that appears more than once in an expression. Programming for Engineers: Operators,, and Statem

Expression consists of a combination of operators and operands. Every expression has a value Examples: Expression Value 4 + 6 2 C = 3 + 8 11 5 > 3 1 6 + (C = 3 + 8) 17 Programming for Engineers: Operators,, and Statem

are the building blocks of program. Each simple statement is ended with a semicolon. Any expression is a statement. Examples: Nonsensible Sensible 8; x=25; 3+4; ++x; y=sqrt(x); x=6+(y=5); Programming for Engineers: Operators,, and Statem

Statement Types The assignment statement is the workhorse of many programs; it assigns a value to a variable. The function statement returns what ever the function does to a variable. The while statement is a structured statement. Programming for Engineers: Operators,, and Statem

Side Effects and Sequence Points A side effect is the modification of a data object or file. states = 50; A sequence point is a point in program execution at which all sides effects are evaluated before going on to the next step. Programming for Engineers: Operators,, and Statem

Full A full expression is one thats not a subexpression of a larger expression. A test condition is an example of a full expression. Programming for Engineers: Operators,, and Statem

Compund Two or more statements grouped together by braces which is called a block. Example: while(index ) { sam = 10* index +2; printf( sam = %d n,sam); } Programming for Engineers: Operators,, and Statem

char and short promoted to int if necessary. Float automatically promoted to double (K&R only). With operation of two types, both values are promoted to higher rank. The final result is converted to the variable being assigned a value. char and short are converted to int, and float is converted to double when passed as argument to function, though this may be overridden. Programming for Engineers: Operators,, and Statem

Rank from Highest Rank to Lowest Rank long double double float unsigned long long long long unsigned long long unsigned int int Programming for Engineers: Operators,, and Statem

The Cast Operator The typecast operator, (type), is used to demand a particular type conversion Consider the following code: int mice; mice=1.6+1.7; mice=(int) 1.6+ (int)1.7; Programming for Engineers: Operators,, and Statem

The Cast Operator The typecast operator, (type), is used to demand a particular type conver Consider the following code: int mice; mice=1.6+1.7;=3 mice=(int) 1.6+ (int)1.7;=2 Programming for Engineers: Operators,, and Statem

Three Parts of a Function 1. Prototype void pound(int n); 2. Invocation int times = 5; char ch =!; pound(times); pound(ch); 3. Declaration void pound(int n) { while (n > 0) printf( # ); printf( n); } Programming for Engineers: Operators,, and Statem

Programming for Engineers: Operators,, and Statem

Complete Chapter 5 Exercise 2 Complete Chapter 5 Exercise 8 Due Date: 09 February 2009 Programming for Engineers: Operators,, and Statem