CSCI 130 Chapter 4 Statements, Expressions, and Operators.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Types and Arithmetic Operators
Introduction to C Programming
Conditionals – if - else Dr. Sajib Datta
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
Expressions and statements Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Introduction to C Programming
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Chapter 4: Basic C Operators
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
LESSON 6 – Arithmetic Operators
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
CPS120: Introduction to Computer Science Operations Lecture 9.
CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Operators & Expressions
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
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.
Rational Expressions relational operators logical operators order of precedence.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
Chapter 3 Control Statements
Operators And Expressions
INSPIRING CREATIVE AND INNOVATIVE MINDS
Selections Java.
EGR 2261 Unit 4 Control Structures I: Selection
Relational Operations
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.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Operators and Expressions
Chapter 3:Decision Structures
Chapter-3 Operators.
Relational and Logical Operators
Operator and Expression
OPERATORS in C Programming
Relational and Logical Operators
DATA TYPES AND OPERATIONS
Controlling Program Flow
OPERATORS in C Programming
Presentation transcript:

CSCI 130 Chapter 4 Statements, Expressions, and Operators

Statements Complete direction to carry out single task Usually one per line Whitespace ignored except in strings Ex: –x = a + b; –printf(“Hello World”);

Code Block 2 or more C statements enclosed in braces Allowed anywhere a single statement is allowed Usually only used where necessary Use indentation for readablility Ex: for (int x = 0; x < 5; x++) { printf(“The value of x is “); printf(“%d”, x); }

Expressions Anything whose evaluation yields a numeric value –PI(symbolic expression defined in program) –20literal constant –ratea variable –700 / –x = a + 10 –x = 6 + (y = 4 + 5)

Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical

Unary Mathematical Operators Increment++increases value by 1 Decrement--decreases value by 1 Ex: x = 10; y = x++; Ex 2: x = 10; y = ++x;

Binary Mathematical Operators Addition+ Subtraction- Multiplication* Division/ Modulus% Ex: –int x = 100; –int y = 9; –z = x % y;(z holds the value 1)

Precedence 1. Parenthesis 2. Multiplication, division, modulus 3. Addition and subtraction Parenthesis can be used to give priority Ex: 3 * (17 +1) * Result = 9

Relational Operators Used to compare expressions Equal= = Greater than> Greater than or equal to>= Less than< Less than or equal to<= Not equal!=

Tip Do not confuse = (assignment) with = = (logical comparison of equality) Common errors: x = = z + 2; if (x = 3)...

if statement General format 1: if (expression) statement; General format 2: if (expression) { statement 1; statement 2; …. Statement n; }

Example if scanf(“%f”, &salary) if (salary > 0) { net = salary - (salary * tax); printf(“The net salary is %f”, net); }

Sample if with else scanf(“%f”, &salary) if (salary > 0) { net = salary - (salary * tax); printf(“The net salary is %f”, net); } else printf(“Incorrect input”);

Relational Expressions Relational expression evalute to: 0 (false) 1 (true) Ex: x = (5 = = 5) x holds the value 1

Example x = (12 < 62); printf(“%d”, x); x = (5 != 3); printf(“ %d”, x); x = (12 < 62) + (5 != 3) + (5 < 3); printf(“ %d”, x); Output is as follows: 1 1 2

Precedence of Relational Operators Relational operators have lower precedence than mathematical operators if ((x + 2) > y) is the same as if (x + 2 > y)

Tip Whenever possible, avoid the not operator Ex: if (x != 5) statement1; else statement2; Is equivalent to: if (x == 5) statement2; else statement1;

Logical Operators AND&&if (exp1 && exp2) –True if both exp1 and exp2 are true OR||if (exp1 || exp2) –True if either exp1 or exp2 is true NOT!if (!exp1) –True if exp1 is false

Logical Precedence NOT is evaluated before any math operators AND is evaluated after any math operators, but before OR OR is evaluated last

Logical Operator Examples (3 != 5) || (6 < 8)true (3 != 5) || (6 > 8) && (9 = = 3)true ((3 != 5) || (6 > 8)) && (9 = = 3)false ((3 != 5) || (6 > 8)) && !(9 = = 3)true 3true 0false !3false

Compound Assignment Operators Shorthand method to combine assignment and binary math operations General form: –exp1 op= exp2 Examples: –x *= yis equivalent tox = x * y –x -= 6 + zis equivalent to x = x z –y % =3is equivalent to y = y % 3

Conditional Operator The only C ternary operator (3 operands) General form: exp1 ? exp2 : exp3; exp1 is true - entire expression evaluates as exp2 exp1 is false - entire expression evaluates as exp3

Conditional Example x = (z < 36) ? 0: 1; –If z < 36, x is set to 0, otherwise 1 z = (x < y) ? x : y; –sets z equal to the smaller of x and y

The Comma Operator An expression can be formed by separating two expressions with a comma: –Both expressions evaluated (left first) –Entire expression evaluates to right expression Ex: –x = (a++, b++) –If x = 2, a = 3, b = 4 before the expression, –x = 4, a = 4, b = 5 after the expression