Chapter 2, Part III Arithmetic Operators and Decision Making

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
Introduction to C Programming
Introduction to C++ Programming
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Ch 6: JavaScript Introduction to scripting part 2.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
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 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 6 JavaScript: Introduction to Scripting
ARITHMETIC IN C Operators.
Introduction to C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Chapter 2 Introduction to Java Applications
Chapter 2 Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
1) C program development 2) Selection structure
Chapter 2 - Introduction to C Programming
Introduction to Java Applications
Arithmetic Operations
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

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

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 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 single-letter 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.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 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.

© 2016 Pearson Education, Ltd. All rights reserved. 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.

© 2016 Pearson Education, Ltd. All rights reserved. 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.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 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.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 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.

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.

© 2016 Pearson Education, Ltd. All rights reserved.

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.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

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.

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.

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.

© 2016 Pearson Education, Ltd. All rights reserved.