0% found this document useful (0 votes)
12 views25 pages

Lecture 03

The document outlines a computer programming course (CSC-113) taught by Mehreen Tariq, covering concepts such as prefix and postfix notation, error types in C++ (syntax, run-time, linker, logical, and semantic errors), and decision control statements (if, if/else, if/else if, and switch statements). It includes examples of using increment/decrement operators, conditional structures, and ternary operators. The document also emphasizes the importance of constants and provides class activities for practical understanding.

Uploaded by

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

Lecture 03

The document outlines a computer programming course (CSC-113) taught by Mehreen Tariq, covering concepts such as prefix and postfix notation, error types in C++ (syntax, run-time, linker, logical, and semantic errors), and decision control statements (if, if/else, if/else if, and switch statements). It includes examples of using increment/decrement operators, conditional structures, and ternary operators. The document also emphasizes the importance of constants and provides class activities for practical understanding.

Uploaded by

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

Computer

Programming

• Course Code: CSC-113


• Course Instructor: Mehreen Tariq
• Email: mehreen.bulc@bahria.edu.pk
Postfix and Prefix
Prefix: is a notation that writes the operator before operands
Postfix: is a notation that writes the operator after the operands

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

cout << val++; // displays 12,


// val is now 13;
cout << ++val; // sets val to 14,
// then displays it
num = --val; // sets val to 13,
// stores 13 in num
num = val--; // stores 13 in num,
// sets val to 12
Class Activity
int a = 10;
a++
cout<<a;
++a;
a++;
cout<<a;
--a;
--a + 5;
cout<<a;
Notes on Increment and
Decrement
• Can be used in expressions:
result = num1++ + --num2;
Errors
1. Syntax Error
2. Run-Time Error
3. Linker Error
4. Logical Error
5. Semantic Error
Syntax Error
• This kind of errors are occurred, when it violates the rule of C++ writing
techniques or syntaxes. This kind of errors are generally indicated by the
compiler before compilation. Sometimes these are known as compile time
error.

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;

8. // checks if the number is positive


9. if (number > 0)
10. {
11. cout << "You entered a positive integer: " << number << endl;
12. }

13. cout << “You Entered a wrong number please enter a valid input.";

14. return 0;}


Syntax If/Else Statement
if (condition)
{
some code;
}
else
{
some code;
}
Example 2: if...else Statement
#include <iostream>
using namespace std;
int main() {
int Marks;
cout << "Enter Your Marks: ";
cin >> Marks;
if (Marks >= 50) {
cout << “ Your are pass" << Marks << endl;
}
else {
cout << “Your are Fail" << Marks << endl;
}
cout << “Code End “;
return 0;
}
Syntax If/Else if/Else Statement
if (condition)
{
some code;
}
else if(condition)
{
some code;
}
else
{
some code;
}
IF Else IF Example
#include <iostream> else if (marks >= 50){
using namespace std; cout<<"Your Grade is C";
int main(){ }
int marks;
else if (marks >= 40){
cout<<"Enter Your Marks: ";
cout<<"Your Grade is D";
cin>>marks;
}
if (marks >= 90){
else if (marks >= 30){
cout<<"Your Grade is A+";
cout<<"Your Grade is E";
}
}
else if (marks >= 80){
else if (marks <= 30){
cout<<"Your Grade is A";
} cout<<"Your Grade is F";
else if (marks >= 70){ }
cout<<"Your Grade is B+"; else{
} cout<<"Enter Valid Marks";
else if (marks >= 60){ }
cout<<"Your Grade is B"; return 0;
} }
Find Largest Among Three
Numbers
int main()
{
int a,b,c;
cin >> a >> b >> c;
int max = a;
if (max < b)
{
max = b;
}
if (max < c)
{
max = c;
}
cout << max << " is largest";
}
Ternary Operators
#include<iostream>
using namespace std;
int main()
{
int a=20;

string result = ( a < 18) ? "Good day." : "Good evening.";


cout << result;

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy