0% found this document useful (0 votes)
44 views29 pages

Lec 6 PF

This document discusses programming fundamentals including input/output streams, comments, errors, expressions, and assignment statements in C++. It defines common terms like cin, stream extraction operator, line comments, block comments, syntax errors, runtime errors, logic errors, operator precedence, and augmented assignment operators. It provides examples of code demonstrating various concepts and expected outputs.

Uploaded by

wajeehadeveloper
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)
44 views29 pages

Lec 6 PF

This document discusses programming fundamentals including input/output streams, comments, errors, expressions, and assignment statements in C++. It defines common terms like cin, stream extraction operator, line comments, block comments, syntax errors, runtime errors, logic errors, operator precedence, and augmented assignment operators. It provides examples of code demonstrating various concepts and expected outputs.

Uploaded by

wajeehadeveloper
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/ 29

Programming Fundamental

Lecture 6
Few Terminologies
cin (pronounced see-in) stands for console input.
The >> symbol, referred to as the stream extraction
operator, assigns an input to a variable.
The user then enters number, which is assigned to variable
radius.
cin object causes a program to wait until data is entered at
the keyboard and the Enter key is pressed.
Comments

A comment is preceded by two slashes (//) on a line,


called a line comment.
Enclosed between /* and */ on one or several lines, called
a block comment or paragraph comment.
Types of Errors in Programming
Syntax Error

 Errors that are detected by the compiler are called


syntax errors or compile errors.
 Syntax errors result from errors in code construction.
 such as mistyping a keyword, omitting necessary
punctuation, or using an opening brace without a
corresponding closing brace.
 These errors are usually easy to detect, because the
compiler tells you where they are and what caused them.
#include <iostream>
using namespace std
int main()
{
cout << "Programming is fun << endl;
return 0;
}
Runtime Errors
 Runtime errors cause a program to terminate
abnormally.
 They occur while an application is running if the
environment detects an operation that is impossible to
carry out.
 Input mistakes typically cause runtime errors. An input
error occurs when the program is waiting for the user to
enter a value, but the user enters a value that the
program cannot handle.
 For instance, if the program expects to read in a number,
but instead the user enters a string, this causes data-type
errors to occur. Another common source of runtime
errors is division by zero. This happens when the divisor
is zero for integer divisions.
Example

#include <iostream>
using namespace std;
int main()
{
int i = 4;
int j = 0;
cout<< i / j << endl;
return 0;
}
Logic Errors

 Logic errors occur when a program does not


perform the way it was intended.
 Errors of this kind occur for many different
reasons.
 For example, suppose you wrote the following
program:
#include <iostream>
using namespace std;
int main()
{
cout << "Celsius 35 is Fahrenheit degree :" ;
cout << (9 / 5) * 35 + 32 << endl;
return 0;
}
Expression Evolution
Converting Expression in C++

(3 + 4 * x) / 5 – (10 * (y - 5) * (a + b + c) )/ x + (9 *
(4 / x + (9 + x) / y))
Precedence of Operators

 Highest: ()
 Next: *,/,%
 Lowest: +,-
Expression Evolution

 If an expression contains several same


precedence operators, they are applied from left
to right.
Example
What will be the output?
#include <iostream>
using namespace std;
int main()
{
cout << "(10.5 + 2 * 3) / (45 - 3.5) = ";
cout << (10.5 + 2 * 3) / (45 - 3.5) << endl;
return 0;
}
Output:
(10.5 + 2 * 3) / (45 - 3.5) = 0.39759
What will be the output?
#include <iostream>
using namespace std;
int main()
{
cout << "3.5 * 4 / 2 – 2.5 = " << (3.5 * 4 / 2 - 2.5) ;
return 0;
}
Output:
3.5 * 4 / 2 û 2.5 = 4.5
 No expression on the left hand side
of the assignment
 Integer division truncates fractional
part
 Liberal use of brackets/parenthesis
What will be the output?
cout << "25 / 4 is " << 25 / 4 << endl;
cout << "25 / 4.0 is " << 25 / 4.0 << endl;
cout << "3 * 2 / 4 is " << 3 * 2 / 4 << endl;
cout << "3.0 * 2 / 4 is " << 3.0 * 2 / 4 << endl;
Output:
25 / 4 is 6
25 / 4.0 is 6.25
3 * 2 / 4 is 1
3.0 * 2 / 4 is 1.5
Assignment Statements and
Assignment Expressions

An assignment statement designates a value for


a variable. An assignment statement can be
used as an expression in C++.
variable = expression;
Example
int y = 1; // Assign 1 to variable y
// Assign 1.0 to variable radius
double radius = 1.0;
// Assign the value of the expression to x
int x = 5 * (3 / 2);
x = y + 1; // Assign the addition of y and 1 to x
area = radius * radius * 3.14159; // Compute area
Example

int i = j = k = 1;
Augmented Assignment Operators /
Compound assignment Operators
Example

x /= 4 + 5.5 * 1.5;
is same as
x = x / (4 + 5.5 * 1.5);
What will be the output?

int a = 6;
a -= a + 1;
cout << a << endl;
a *= 6; cout << a << endl;
a /= 2; cout << a << endl;
Show the output of the following code:
 #include <iostream>
 using namespace std;
 int main() { cout << "3.5 * 4 / 2 – 2.5 = " << (3.5 * 4 / 2 – 2.5)
<< endl;
 return 0; }
Show the output of the following code:
 #include <iostream>
 using namespace std;
 int main() { cout << "C++" << "Java" << endl; cout << "C+
+" << endl << "Java" << endl; cout << "C++, " << "Java, " <<
"and C#" << endl;
 return 0; }
Show the printout of the following code:
int a = 6;
a -= a + 1;
cout << a << endl;
a *= 6; cout << a << endl;
a /= 2; cout << a << endl

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