C Programming Chapter 4 Expressions and Operators PDF

Document Details

DeliciousHeliotrope4330

Uploaded by DeliciousHeliotrope4330

Universiti Tun Hussein Onn Malaysia

Tags

C programming operators expressions computer science

Summary

This document provides a summary of expressions and operators in C programming. It covers arithmetic, relational, and logical operators, and their usage in C code. The explanation is clear and includes illustrations to assist understanding.

Full Transcript

DAM23603 COMPUTER PROGRAMMING CHAPTER 4 EXPRESSIONS AND OPERATORS PART 1 INTRODUCTION By the end of this chapter, the student will be able: 1. Define an expression and operator operation in C programming language 2. Performs expression and operator operations in the C programmin...

DAM23603 COMPUTER PROGRAMMING CHAPTER 4 EXPRESSIONS AND OPERATORS PART 1 INTRODUCTION By the end of this chapter, the student will be able: 1. Define an expression and operator operation in C programming language 2. Performs expression and operator operations in the C programming language. INTRODUCTION Operators are the symbols in which tell the computer to execute certain mathematical or logical operations. In C language there are 8 classifications of operators: 1. Arithmetic operators 5. Assignment operators 2.Relational operators 6. Increment and Decrement operators 3. Logical operators 7. Bitwise operators 4.Conditional operators 8. Special operators INTRODUCTION Expressions in C language are the combination of variables, constants, and operators written in proper syntax. C language is capable of handling complex mathematical expressions easily but these expressions need to be written in proper syntax. ARITHMETIC OPERATOR In C language, basic arithmetic operation can be performed such as: + , - , * , / and %. Operators Meanings + Addition or unary plus - Subtraction or unary minus / Division % Modulo division (cannot be used on f data type) * Multiplication ARITHMETIC OPERATOR Arithmetic expressions can be written by combining one or more arithmetic operations. The precedence levels of C arithmetic expressions would determine the arithmetic operation sequence in solving the expression. If the precedence level to be avoided, you can use brackets as the expression in the bracket will be evaluated first. ARITHMETIC OPERATOR Precedence levels Precedence Level Operation High () ++, -- *, / ,% +, - +=, -=, *=, /=, %= Low >, ,= Greater than or equal to == Equal to != Not equal to Relational Operator Operator Symbol Question Asked Eg Equal == Is operand1 equal to operand2? x == y Greater than > Is operand1 greater than x>y operand2? Less than < Is operand1 less than operand2? x= Is operand1 greater than or equal x >= y equal to to operand2? Less than or 1) || (6 < 1) True (1), because one operand is true (2 == 1) && (5 == 5) False (0), because one operand is false !(5 == 4) True (1), because the operand is false ASSIGNMENT STATEMENT (Compound Assignment Statement) Modify a variable value where the variable original value is added/minus/multiply by another value and is assigned back to the original variable. e.g: price = price – discount; Operation Example Meaning += val += 5; val = val + 5 -= Val -= 5; val = val – 5 *= val *= 5; val = val * 5 /= val /= 5; val = val / 5 %= val %= 5; val = val % 5 Compound expression vs simple expression Compound expression Simple expression x *= y x=x*y x /= y x=x/y x %= y x=x%y x+=y ? ? x = x * (y+3) x /= y-1 ? THANK YOU -END-

Use Quizgecko on...
Browser
Browser