Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Fundamentals

Similar presentations


Presentation on theme: "Computing Fundamentals"— Presentation transcript:

1 Computing Fundamentals
Instructor: Engr.Romana Farhan Assistant Professor CPED

2 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;

3 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; }

4 Example Compile-Time Error

5 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 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.

6 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; }

7 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; }

8 Arithmetic Operators

9 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

10 Operator precedence

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

12

13 C++ Programming Guidelines
From Deitel and Deitel

14 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.

15 Relational Operators

16 Note:-

17 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).

18 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.

19 Logical Operators - &&

20 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.

21 Logical Operators - ||

22 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.

23 Logical Operators - !

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

25 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

26 Assignment Operator

27 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; }

28 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 --

29 Increment and Decrement Operators

30 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; }

31 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; }

32 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; }

33 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; }

34

35

36 Questions


Download ppt "Computing Fundamentals"

Similar presentations


Ads by Google