0% found this document useful (0 votes)
3 views

Week_04_Fundamentals_of_Programming.pptx_20230918_114455_0000

The document covers fundamental concepts of C++ programming, including the structure of a C++ program, types of errors, data types, and variable handling. It explains expressions, operators (arithmetic, unary, binary), and operator precedence, along with examples of arithmetic operations and increment/decrement operators. Additionally, it discusses input and output operations using 'cin' and 'cout' for console interaction.

Uploaded by

m.faizanwebdev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Week_04_Fundamentals_of_Programming.pptx_20230918_114455_0000

The document covers fundamental concepts of C++ programming, including the structure of a C++ program, types of errors, data types, and variable handling. It explains expressions, operators (arithmetic, unary, binary), and operator precedence, along with examples of arithmetic operations and increment/decrement operators. Additionally, it discusses input and output operations using 'cin' and 'cout' for console interaction.

Uploaded by

m.faizanwebdev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Programming Fundamentals

Week 4
We discussed in last lecture
Basic Structure of C++ program
Preprocessor directive, main function and program
body
C++ Statements
Debugging in C++, Different Types of Errors
Identifier & types of identifiers
Data types
Variable, variable declaration and initialization
Constant and its types
In this lecture we will discuss

Fundamentals of Programming
Constructs
Expression
Operators
Arithmetic operators
Unary / Binary operator
Increment/Decrement Operator
Operator Precedence
Input and Output
What is an expression?
An expression is any computation C⁺⁺ statement which yields a
certain value.
All expressions have a value. The process of determining the
expression’s value is called evaluation.
An expression evaluates a single value.
An expression consists of operators and operands.
An operator is a symbol that performs some operation.
Operand is the value on which the operator performs a
function.
Operand can be a constant, variable or function.
It may consist of any number of operators and operands.
Examples are A+B, X/Y, num*37,
Expressions
Operators
C++ provides a rich operator environment.
An operator is a symbol that tells the compiler to
perform a specific mathematical or logical manipulation.
C++ has four general classes of operators: arithmetic,
bitwise, relational, and logical.
C++ also has several additional operators that handle
certain special situations.
Operators are categorized as Unary or Binary
operators.
Unary that require one operand to perform operation.
E.g. ++ and - -. a++and --x are examples.
Binary that requires two operands to perform operation.
E.g +, -, *, /, %. A+B, X/Y and 15%3 are examples.
Arithmetic Operators
C++ provides five basic arithmetic operators.
All arithmetic operators work with numeric values.
C++ defines the following arithmetic operators with
examples:

Note that modulus or remainder operator only works with integer values
Generally, if both operands are integers then the result will be an integer.
However, if one or both of the operands are reals then the result will be a
real (or double to be exact).
Example of Arithmetic Operators
#include <iostream>
using namespace std;
int main(void)

Output will be as:


{
int a = 21;
int b = 10;
int c ;
c = a + b;
cout << "Line 1 - Value of c is :" << c << endl ;
c = a - b;
cout << "Line 2 - Value of c is :" << c << endl ;
c = a * b;
cout << "Line 3 - Value of c is :" << c << endl ;
c = a / b;
cout << "Line 4 - Value of c is :" << c << endl ;
c = a % b;
cout << "Line 5 - Value of c is :" << c << endl ;
c = a++;
cout << "Line 6 - Value of c is :" << c << endl ;
c = a--;
cout << "Line 7 - Value of c is :" << c << endl ;
return 0; }
% = Remainder or modulus

The remainder operator (%) expects integers for


both of its operands. It returns the remainder of
integer-dividing the operands.
For example 13%3 is calculated by integer dividing
13 by 3 to give an outcome of 4 and a remainder
of 1; the result is therefore 1.
19 % 2 = 1
20 % 5 = 0
40 % 7 = ?
Increment/Decrement Operator
The auto increment (++) and auto decrement (--) operators
provide a convenient way of, respectively, adding and
subtracting 1 from a numeric variable.
Both operators can be used in prefix and postfix form. The
difference is significant. When used in prefix form, the
operator is first applied and the outcome is then used in
the expression. When used in the postfix form, the
expression is evaluated first and then the operator applied.
Both operators may be applied to integer as well as real
variables.
e.g. k = 5
Increment/Decrement Operator
Increment Operator
Used to increase the value of variable by 1.
Prefix form (before variable)
Postfix form (after variable)
Example
A++
++B
Decrement Operator
Used to Decrease the value of a variable by 1.
Prefix Form (before variable)
Postfix Form (after variable
Example
A- -
- -B
Operator Precedence and
associativity
When different operators are used in the same expression, the
normal rules of arithmetic apply. These rules divide the C++
operators into a number of precedence levels. All C++
operators have a precedence and associativity:
Precedence—when an expression contains two different
kinds of operators, which should be applied first?
Associativity—when an expression contains two operators
with the same precedence, which should be applied first?
Operators in higher levels take precedence over operators in
lower levels.
Highest: ()
Next: %, / , * ,
Lowest: +,-
Same Precedence
For addition, subtraction, division and multiplication:
Left-to-right rule applies
A+B+C means (A+B)+C
The multiplicative operators (*, /, and %) have equal
precedence with each other, and the additive operators
(binary + and -) have equal precedence with each other.
The multiplicative operators have precedence over the
additive operators.

For exponentiation
Right-to-left rule applies
Operator Precedence and associativity
Levels
Quadratic Equation
In algebra
Quadratic = ax² + bx + c

In C++
Quadratic = a*x*x + b*x + c
Activity

10+2*3+5*4+12/6+5*20/2

88
Activity

Question 1:
5 *4+16/16+5*2
5 *4+16/(16+5*2)

Question 2:
10 * (24/(5-2))+13
10 * 24/5-2+13
Input and Output

Standard Input
cin stands for ‘console input’
>> extraction operator or get from operator
Variable/constant/expression
cin>>”Enter your name=”;
Standard Output
cout stands for ‘console output’
<< insertion operator or put to operator
Variable/constant/expression
cout<<“Stay Blessed “;

You might also like