CSC-113 - Lecture - 02 - C++ Fundamentals
CSC-113 - Lecture - 02 - C++ Fundamentals
CSC-113 - Lecture - 02 - C++ Fundamentals
COMPUTER PROGRAMMING
3
LECTURE OUTLINE
Fundamentals of C++
Structure of C++ Program
Data types
Variables
Escape Sequences
I/O Statements (cin/cout)
STRUCTURE OF A C++ PROGRAM
Opening brace
Closing brace
Opening brace
Closing brace
PREPROCESSOR DIRECTIVES
A libraries that contain /* A program to print the string
functions and symbols that are Welcome to C++ to the screen
necessary to run a C++ */
❑ The string “hello world\n” is sent to the cout function and the
function will display the message on the screen
❑ The \n that appears in the cout function call will cause the message
to be printed on the screen then moves the printing to the next line
SYNTAX
There are rules for both syntax (grammar) and semantics (meaning).
Syntax: The formal rules governing how one writes valid
instructions in a language
The syntax rules tell us which statements (instructions) are legal, that is,
accepted by the programming language and which are not.
TOKEN
The smallest individual unit of a program written in any language is called a
token.
A token is the basic vocabulary of the language.
Tokens in C++
keywords
identifiers
constants
string constants
operators
punctuators
Language syntax specifies the valid combinations of tokens in the language
into legal strings.
TOKEN TYPES
Identifiers
main, payRate, score_1, conversion
Operators
+ - * / ( )
Keywords
int, float, double, char, void, return
Punctuators
. ; ? , {
Others
<= != == >=
Comments are not tokens (remember they are removed from the program)
IDENTIFIERS
A _a a123
ABcDf99_ sum average 1a_a a+2 a$1
Mark1 Mark_2 xy_1_4
A”bc -abc aa.bb
Sum_of_values avg_plus_sum
A abc a9!c
these are not the same
Why they are invalid ?.
Aa aa aA AA aa
KEYWORDS (reserve words)
float: The data type float is used in C++ to represent any real number between -
3.4E+38 and 3.4E+38.The memory allocated for the float data type is 4 bytes.
double: The data type double is used in C++ to represent any real number between
-1.7E+308 and 1.7E+308.The memory allocated for the double data type is 8
bytes.
On most newer compilers, the data types double and long double are the
same.
The maximum number of significant digits—that is, the number of decimal places—
in float values is 6 or 7 and double is 15.
int main ()
{
const int noOfStudents = 20;
const char blank = ' ';
const double payRate = 15.75;
return 0;
}
VARIABLES
cin>>…
cout<<…
INPUT (READ) STATEMENT: CIN Output : cout
The syntax of cout together with <<
Syntax of cin together with is
>>:
cout<< variable;
cin>>variable;
In C++, << is called the insertion
In C++, >> is called the operator.
extraction operator. expression is evaluated and its
Suppose miles is a variable value is printed at the current
of the type double. cursor position on the screen.
manipulator manipulates the
The statement output. The simplest manipulator is
cin>>miles; endl (the last character is the letter
el), which causes the cursor to
move to the beginning of the next
causes the computer to get a line.
value of the type double and
Strings and expressions involving
place it in the memory cell only one variable or a single value
miles. are evaluated to itself.
OUTPUT : COUT
Statement Output
1. cout<<29/4; 7
2. cout<<"Hello there. "; Hello there.
3. cout<<12; 12
4. cout<<"4+7"; 4+7
5. cout<<4+7; 11
6. cout<<"A"; A
7. cout<<"4 + 7 = "<<4 + 7; 4 + 7 = 11
8. cout<<2+3*5; 17
9. cout<<"Hello \nthere. "; Hello
there.
\n is called new line escape sequence.
\ (back slash) is called the escape character.
USE OF BLANKS, SEMICOLONS, BRACKETS AND COMMAS
In C++, one or more blanks are used to separate numbers when data is input.
Blanks are also used to separate reserved words and identifiers from each other
and other symbols.
int a,b,c;
int a, b, c;
The blanks between the identifiers in the second statement
are meaningless.
inta,b,c;
no blank between the t and a changes the reserved word
int and the identifier a into a new identifier inta.
x = y * a;
Consider the following
const double conversion = 2.54;
double centimeters;
double inches;
Run-together-word
inchperfoot inchPerFoot inch_per_foot.
PROMPT LINES
Escape
Description
sequence
\' single quote
\" double quote
Escape sequences are used to
\? question mark
represent certain special
\\ backslash
characters within string \a audible bell
literals and character literals. \b backspace
\n line feed - new line
\r carriage return
\t horizontal tab
Examples
34
MORE ABOUT COUT
float y = 23.1415;
cout.precision(1);
cout << y << '\n'; // Outputs 2e+01
cout.precision(2);
cout << y << '\n'; // Outputs 23
cout.precision(3);
cout << y << '\n'; // Outputs 23.1
SUMMARY
The body of the function is enclosed between { and } and has two types of
statements.
Statements enclosed in // have no effect on the execution of the program.
Identifiers allow us to name variables, constant names, functions and objects in the
program.
C++ data types fall into three categories: Simple Data Type. ,Structured Data Type.,
Pointers
Named Constant: A memory location whose content is not allowed to change
during program execution.
Variable: A memory location whose content may change during program execution.
cin reads data from the keyboard and stores it to a buffer
cout prints out data to the monitor
In C++, >> is called the extraction operator
In C++, << is called the insertion operator 36
Practise Problems
#include <iostream>
using namespace std;
int main ()
{
int x = 5;
cout << x << endl;
return 0;
}
37
Practise Problems
#include <iostream>
using namespace std;
int main ()
{
int a =4;
int b = 5;
int c = 4;
int sum = a+b+c;
cout << "sum of numbers is \t" << sum;
return 0;
}
38
Practise Problems
#include <iostream>
using namespace std;
int main ()
{
cout << "*" << endl;
cout << "**" << endl;
cout << "***" << endl;
cout << "****\n";
return 0;
}
39
Practise Problems
#include <iostream>
using namespace std;
int main ()
{
int x;
cin >> x;
cout << x << endl;
return 0;
}
40
Practise Problems
int val1;
int val2;
int val3;
cout<<"Please enter your 3 numbers:";
cin>>val1;
cin >>val2;
cin>>val3;
cout<<"\nYour numbers forward:\n";
cout<<val1<< "\n"<<val2<<"\n" <<val3<<"\n";
cout<< "Your numbers reversed:\n";
cout<<val3<<"\n"<<val2 <<"\n"<<val1<<"\n";
41
Practise Problems
42