Week_04_Fundamentals_of_Programming.pptx_20230918_114455_0000
Week_04_Fundamentals_of_Programming.pptx_20230918_114455_0000
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)
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 “;