Computing Fundamentals

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4) 10 September.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1 CIS Jan Overview Evolution of C++ Programming Style Syntax & Semantics Comments & White Space Data Types Variables & Declarations cout.
Basic Elements of C++ Chapter 2.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Operaciones y Variables
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
C++ Programming: Basic Elements of C++.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
Recap……Last Time [Variables, Data Types and Constants]
LESSON 3 Operators. Operators  Symbol that tells the computer to perform certain mathematical or logical manipulations.
1 CS161 Introduction to Computer Science Topic #6.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Introduction to Computer Programming
CSE 220 – C Programming Expressions.
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++ Chapter 1.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Chapter 3 Assignment and Interactive Input.
Computing Fundamentals
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Chapter 7: Expressions and Assignment Statements
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.
Basic Elements of C++ Chapter 2.
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Introduction to C++ Programming
Summary Two basic concepts: variables and assignments Basic types:
Associativity and Prescedence
Introduction to Programming – 4 Operators
CS150 Introduction to Computer Science 1
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.
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Life is Full of Alternatives
OPERATORS AND EXPRESSIONS IN C++
OPERATORS in C Programming
Operator King Saud University
OPERATORS in C Programming
Presentation transcript:

Computing Fundamentals Instructor: Engr.Romana Farhan Assistant Professor CPED

Constants Constants can not change their value during program execution. There are 4 types of constants in C++ Integer constant Floating point constant Character constant String constant The const qualifier is used to make a variable constant. Value to constant is initialized at its declaration. const float pi=3.1416;

Example: #include<iostream> using namespace std; int main() { Int r; const float p=3.14; float peri; r=2; peri=2*p*r; cout<<“result is=“<<peri; return 0; }

Example Compile-Time Error

The “define” directive #define directive is also used to define a constant quantity. Following is the syntax of #define directive #define identifier constant Example #define pi 3.1416 NOTE: The identifier does not have any data type and any value can be assigned to it. The name of identifier can not be used in the program.

Example: #include<iostream> #define p 3.14 using namespace std; int main() { int r; float peri; r=2; peri=2*p*r; cout<<“result is=“<<peri; return 0; }

Example: #include <iostream> using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; }

Arithmetic Operators

Arithmetic operators All the operators on previous slide are binary operators as they require two operands. 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 followed in algebra

Operator precedence

Arithmetic Expression Evaluation Evaluate Y = 2*5*5+3*5+7; Y=(4-(3*5))+2 What is your answer?

C++ Programming Guidelines From Deitel and Deitel

Relational Operators Relational operators specify a relationship between two operands. The operands may be variables, constants or expressions. a >= b 10 < 20 ( a + b ) > ( c * d ) The relational operators along with the operands form a relational expression. The result of a relational expression is either true or false.

Relational Operators

Note:-

Logical Operators C++ provides logical operators that are used to form complex conditions by combining simple conditions. The logical operators are && (logical AND) || (logical OR) ! (logical NOT, also called logical negation).

Logical Operators - && The && (logical AND) operator is used to ensure that two conditions are both true before we choose a certain path of execution. The simple condition to the left of the && operator evaluates first. If necessary, the simple condition to the right of the && operator evaluates next. The right side of a logical AND expression is evaluated only if the left side is true.

Logical Operators - &&

Logical Operators - || The || (logical OR) operator determines if either or both of two conditions are true before we choose a certain path of execution. If any one of the simple conditions to the left or right of || is true, the output will be true. The && operator has a higher precedence than the || operator. Both operators associate from left to right.

Logical Operators - ||

Logical Operators - ! C++ provides the ! operator (logical NOT) which is also called logical negation operator. It “reverses” a condition’s meaning, i.e., if (x>y) is true then !(x>y) will be false. The unary logical negation operator has only a single condition as an operand.

Logical Operators - !

Find out … What is short-circuit evaluation in context of && and || operators?

Assignment Operator C++ provides several assignment operators for abbreviating assignment expressions. Any statement of the form variable = variable operator expression; in which the same variable appears on both sides of the assignment operator and operator is one of the binary operators +, -, *, /, or % , can be written in the form variable operator= expression; Thus the statement c = c + 3 which adds 3 to c can be written as c += 3

Assignment Operator

Example: Sol: #include<iostream> using namespace std; int main() Write a program in c++ to assign three values to three integers a,b,c. Add variables a & b and multiply their sum to variable c by using compound assignment statements. Sol: #include<iostream> using namespace std; int main() { int a,b,c; a=3; b=6;c=9; c*= a+b; cout<<“result=“<<c; }

Increment and Decrement Operators C++ also provides two unary operators for adding 1 to value of a variable or subtracting 1 from the value of a numeric variable. These are the unary increment operator ++ unary decrement operator --

Increment and Decrement Operators

Prefix increment Operator #include<iostream> using namespace std; int main() { int a,b,c,sum; a=1,b=1,c=3; sum=a+b+(++c); cout<<"sum="<<sum; //sum=6 cout<<"c="<<c; //c=4 return 0; }

Postfix Increment Operator #include<iostream> using namespace std; int main() { int a,b,c,sum; a=1,b=1,c=3; sum=a+b+(c++); cout<<"sum="<<sum; //sum=5 cout<<"c="<<c; //c=4 return 0; }

Prefix Decrement Operator #include<iostream> using namespace std; int main() { int a,b,c,sum; a=1,b=1,c=3; sum=a+b+(--c); cout<<"sum="<<sum; //sum=4 cout<<"c="<<c; //c=2 return 0; }

Postfix decrement Operator #include<iostream> using namespace std; int main() { int a,b,c,sum; a=1,b=1,c=3; sum=a+b+(c--); cout<<"sum="<<sum; //sum=5 cout<<"c="<<c; //c=2 return 0; }

Questions