PF Lecture (Theory) 02-Variables
PF Lecture (Theory) 02-Variables
C++
Review
#include<iostream>
using namespace std;
int main()
• When we add endl output
{
will be
cout<<“Hello World”<<endl;
cout<<“Computer Program” <<endl; ???
cout<<“Bye” <<endl;
return 0;
}
endl
• Inserts a new-line character.
Variables
Variable
• Sal
• sal
• saL
• SAL
• SaL
Variable Declaration
int i = 10, j = 25 ;
is same as
int j = 25, i = 10 ;
However,
float a = 1.5, b = a + 3.1 ;
is alright, but
float b = a + 3.1, a = 1.5 ;
is not. This is because here we are trying to use a even before defining it.
variations of the type declaration
instruction
3. The following statements would work
int a, b, c, d ;
a = b = c = 10 ;
However, the following statement would not work
int a = b = c = d = 10 ;
Once again we are trying to use b (to assign to a) before defining it.
Output Function and Variables
#include<iostream>
using namespace std;
int main()
{
int num=5;
cout<<num;
return 0;
}
Think the output
cout<<a + b * c – d ;
Review
char letter;
cout << “Please enter the first letter of your name: “;
cin >> letter;
cout<< “Your name starts with “ << letter;
OUTPUT:
Please enter the first letter of your name: F
Your name starts with F
Output Stream
The Insertion Operator (<<)
• To send output to the screen we use the insertion
operator on the object cout
• Format: cout << Expression;
• The compiler figures out the type of the object and
prints it out appropriately
cout << 5; // Outputs 5
cout << 4.1; // Outputs 4.1
cout << “String”; // Outputs String
cout << ‘\n’; // Outputs a newline
Stream-Insertion Operator
• << is overloaded to output built-in types ( ex. int, float,…etc)
• Can also be used to output user-defined types (i.e., user-defined classes).
• cout << ‘\n’;
• Prints newline character
• cout << endl;
• endl is a stream manipulator that issues a newline character and flushes the output
buffer
• cout << flush;
• flush flushes the output buffer
a buffer is just a pre-allocated area of memory where you store your data
while you're processing it.
Cascading Stream-Insertion Operators
• << : Associates from left to right, and returns a reference to its left-operand
object (i.e. cout).
• This enables cascading
cout << "How" << " are" << " you?";
NOT
float y = 23.1415;
cout.precision(2);
cout << y << '\n'; // Outputs 23
cout.precision(3);
cout << y << '\n'; // Outputs 23.1
Using
showpoint/noshowpoint
• showpoint specify that floating-point numbers (even for whole
numbers) should be output with a decimal point, even if they’re zeros.
Following the decimal point, as many digits as necessary are written to
match the precision.
• This setting is reset with stream manipulator noshowpoint.
• When the showpoint manipulator is not set, the decimal point is only
written for non-whole numbers.
Using showpoint/noshowpoint
#include <iostream>
using namespace std; 30.000 10000. 3.1416
int main () { 30 10000 3.1416
double a, b, pi;
a=30.0;
b=10000.0;
pi=3.14165;
cout.precision (5);
cout << showpoint << a << '\t' << b << '\t' << pi << endl;
cout << noshowpoint << a << '\t' << b << '\t' << pi << endl;
return 0;
}
Formatting Text
• To print text you need to include “” around the text
• Cout <<“This is a Beautiful Day” ;
• You can add escape sequence for further options.
Escape Sequence
Examples
cout<<“The total area is “<< area<< “and the total cost is “<< cost << “ S.R.”; area = 60.2
The total area is 60.2 and the total cost is 4530 S.R. cost = 4530
Terms Meaning
• Input Stream • cin
• Output Stream • cout
• Insertion Operation • <<
• Extraction Operator • >>
• Cascading • Adding more than 1 elements
Common Programming
Errors
Errors
Types of
Error