Lecture 03
Lecture 03
Programming
• If you use the ++ operator as a prefix like: ++var, the value of var is incremented
by 1; then it returns the value.
• If you use the ++ operator as a postfix like: var++, the original value of var is
returned first; then var is incremented by 1.
• ++a
• a++
• --a
• a--
Prefix vs. Postfix - Examples
int num, val = 12;
include<iostream>
int main()
{
cout>>"Hello//World";
}
Run-Time Error
• This kind of errors are occurred, when the program is executing. As
this is not compilation error, so the compilation will be successfully
done. We can check this error if we try to divide a number with 0.
#include<iostream>
Using namespace std;
int main()
{
int x = 52;
int y = 0;
cout<<x/y;
}
Linker Error
• This kind of errors are occurred, when the program is compiled successfully, and
trying to link the different object file with the main object file. When this error is
occurred, the executable is not generated, For example some wrong function
prototyping, incorrect header file etc. If the main() is written as Main(), this will
generate linked error.
#include<iostraem>
Using namespace std;
int mian()
{
int x = 52;
int y = 0;
cout<<x/y;
}
Logical Error
• Sometimes, we may not get the desired output. If the syntax and other things are
correct, then also, we may not get correct output due to some logical issues.
These are called the logical error.
//Addition of two numbers
#include<iostream>
Using namespace std;
int main()
{
int x = 52;
int y = 34;
cout<<x-y;
}
Semantic Error
• This kind of error occurs when it is syntactically correct but has no
meaning. This is like grammatical mistakes. If some expression is given at
the left side of assignment operator, this may generate semantic error.
#include<iostream>
Using namespace std;
int main()
{
int x = 52;
int y = 44;
int sum;
x+y=sum;
cout<<sum;
}
Find Errors in the Code
#include<iostream>
Using namespace std;
int mian()
{
string name=‘X-Y-Z’;
int id = "0";
cout<< "Id: ">>id
cout>>"Name: "<<name;
system(‘pasue’);
}
Decision Controls
The decision control statements are the decision-making statements that decides
the order of execution of statements based on the conditions. In the decision-
making statements the programmer specify which conditions are to be executed or
tested with the statements to be executed if the condition is true or false.
1. If statement
2. If/else statement
If
3. If/elseif/else statement Condition
4. Switch statement
5. Nested if/else statement
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations. C++ is rich in built-in operators and provide the following
types of operators:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
Example 1: if Statement
// Program to print positive number entered by the user
// If the user enters a negative number, it is skipped
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int number;
6. cout << "Enter an integer: ";
7. cin >> number;
13. cout << “You Entered a wrong number please enter a valid input.";
}
Summary:
Constants , Why use constants?, Postfix and Prefix, Postfix and Prefix
Examples, Class Activity, Errors Syntax Error, Run-Time Error, Linker
Error, Logical Error, Semantic Error, Conditional Structure (IF Condition,
IF-Else Condition, IF-Else-IF Condition)Examples, Ternary Operators.