Chapter 2, Part III Arithmetic Operators and Decision Making

Similar documents
by Pearson Education, Inc. All Rights Reserved. 2

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Programming for Engineers Introduction to C

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Full file at C How to Program, 6/e Multiple Choice Test Bank

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

C: How to Program. Week /Mar/05

Chapter 2, Part I Introduction to C Programming

IT 374 C# and Applications/ IT695 C# Data Structures

2.5 Another Application: Adding Integers

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Lecture 3 Tao Wang 1

Unit 3. Operators. School of Science and Technology INTRODUCTION

Chapter 2 - Introduction to C Programming

Introduction to C Programming

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Introduction to Java Applications; Input/Output and Operators

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Introduction to Python Programming

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

A Simple Syntax-Directed Translator

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

download instant at

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 1 Introduction to Computers and C++ Programming

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Operators & Expressions

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

Lecture 3. More About C

Basics of Programming

2.8. Decision Making: Equality and Relational Operators

Chapter 1 & 2 Introduction to C Language

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Selection Statements. Pseudocode

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

Fundamental of Programming (C)

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

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

Chapter 3 Structured Program Development in C Part II

JAVA OPERATORS GENERAL

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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

Chapter 17. Fundamental Concepts Expressed in JavaScript

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

Computer Programming CS F111

Structured Programming. Dr. Mohamed Khedr Lecture 9

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

Arithmetic Expressions in C

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

CS110: PROGRAMMING LANGUAGE I

Variables and Operators 2/20/01 Lecture #

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

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 Lecture 3

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Reserved Words and Identifiers

LECTURE 02 INTRODUCTION TO C++

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 2: Using Data

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

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Flow Control. CSC215 Lecture

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

Lecture 4 CSE July 1992

Introduction to Programming

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Introduction to Java Applications

SFPL Reference Manual

Fundamentals of Programming

Conditional Statement

Introduction to C# Applications

Creating a C++ Program

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Data Types and Variables in C language

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

DEPARTMENT OF MATHS, MJ COLLEGE

UNIT- 3 Introduction to C++

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

Template Language and Syntax Reference

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Chapter 2: Basic Elements of C++

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

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

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Lexical Considerations

Another Simple Program: Adding Two Integers

Chapter 3: Operators, Expressions and Type Conversion

(2-1) Numeric Expressions in C H&K Chapter 2. Instructor - Andrew S. O Fallon CptS 121 (August 27, 2018) Washington State University

Transcription:

Chapter 2, Part III Arithmetic Operators and Decision Making C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. 1

2016 Pearson Education, Ltd. All rights reserved. 2

2016 Pearson Education, Ltd. All rights reserved. 3

2.5 Arithmetic in C Most C programs perform calculations using the C arithmetic operators (Fig. 2.9). The asterisk (*) indicates multiplication and the percent sign (%) denotes the remainder operator, which is introduced below. In algebra, to multiply a times b, we simply place these singleletter variable names side by side as in ab. In C, however, if we were to do this, ab would be interpreted as a single, two-letter name (or identifier). Therefore, C requires that multiplication be explicitly denoted by using the * operator as in a * b. The arithmetic operators are all binary operators. For example, the expression 3 + 7 contains the binary operator + and the operands 3 and 7. 2016 Pearson Education, Ltd. All rights reserved. 4

2016 Pearson Education, Ltd. All rights reserved. 5

2.5 Arithmetic in C (Cont.) Integer Division and the Remainder Operator Integer division yields an integer result For example, the expression 7 / 4 evaluates to 1 and the expression 17 / 5 evaluates to 3 C provides the remainder operator, %, which yields the remainder after integer division Can be used only with integer operands The expression x % y yields the remainder after x is divided by y Thus, 7 % 4 yields 3 and 17 % 5 yields 2 2016 Pearson Education, Ltd. All rights reserved. 6

2.5 Arithmetic in C (Cont.) Parentheses for Grouping Subexpressions Parentheses are used in C expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c we write a * ( b + c ). 2016 Pearson Education, Ltd. All rights reserved. 7

2.5 Arithmetic in C (Cont.) Rules of Operator Precedence C applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those in algebra: Operators in expressions contained within pairs of parentheses are evaluated first. Parentheses are said to be at the highest level of precedence. In cases of nested, or embedded, parentheses, such as ( ( a + b ) + c ) the operators in the innermost pair of parentheses are applied first. 2016 Pearson Education, Ltd. All rights reserved. 8

2016 Pearson Education, Ltd. All rights reserved. 9

2.5 Arithmetic in C (Cont.) Figure 2.11 illustrates the order in which the operators are applied. 2016 Pearson Education, Ltd. All rights reserved. 10

2016 Pearson Education, Ltd. All rights reserved. 11

2.5 Arithmetic in C (Cont.) As in algebra, it s acceptable to place unnecessary parentheses in an expression to make the expression clearer. These are called redundant parentheses. 2016 Pearson Education, Ltd. All rights reserved. 12

2.6 Decision Making: Equality and Relational Operators Conditions in if statements are formed by using the equality operators and relational operators summarized in Fig. 2.12. If the condition is true (i.e., the condition is met) the statement in the body of the if statement is executed. If the condition is false (i.e., the condition isn t met) the body statement is not executed. 2016 Pearson Education, Ltd. All rights reserved. 13

2016 Pearson Education, Ltd. All rights reserved. 14

2.6 Decision Making: Equality and Relational Operators Figure 2.13 uses six if statements to compare two numbers entered by the user. If the condition in any of these if statements is true, the printf statement associated with that if executes. 2016 Pearson Education, Ltd. All rights reserved. 15

2016 Pearson Education, Ltd. All rights reserved. 16

2016 Pearson Education, Ltd. All rights reserved. 17

2016 Pearson Education, Ltd. All rights reserved. 18

2.6 Decision Making: Equality and Relational Operators Comparing Numbers The if statement if ( num1 == num2 ) { printf( "%d is equal to %d\n", num1, num2 ); } compares the values of variables num1 and num2 to test for equality. If the conditions are true in one or more of the if statements, the corresponding body statement displays an appropriate line of text. Indenting the body of each if statement and placing blank lines above and below each if statement enhances program readability. 2016 Pearson Education, Ltd. All rights reserved. 19

2.6 Decision Making: Equality and Relational Operators A left brace, {, begins the body of each if statement A corresponding right brace, }, ends each if statement s body Any number of statements can be placed in the body of an if statement. 2016 Pearson Education, Ltd. All rights reserved. 20

2.6 Decision Making: Equality and Relational Operators Some of the words we ve used in the C programs in this chapter in particular int and if are keywords or reserved words of the language. Figure 2.15 contains the C keywords. These words have special meaning to the C compiler, so you must be careful not to use these as identifiers such as variable names. 2016 Pearson Education, Ltd. All rights reserved. 21

2016 Pearson Education, Ltd. All rights reserved. 22