Unit 5 Introduction To C++
Unit 5 Introduction To C++
PROGRAM DESIGN
■ User ■ Keywords:
■ an individual who runs ■ Words that have a special meaning in
the language.
or executes, a program ■ Cannot be used for any other
purposes
■ Programmer:
■ Identifiers:
■ an individual who ■ Made up by the programmer
creates or writes, a ■ Not part of the C++ language
■ Operators ■ Punctuation:
■ Used to perform operations ■ Characters that mark the end of a
on data statement
■ Arithmetic: +, -, *, / ■ Syntax:
■ Assignment: = ■ The structure of the language
■ Punctuation: ■ Semantics:
■ Characters that mark the ■ The meanings of the constructs in
end of a statement the language
C++ Programming
cout << “You entered: “ << number << endl; Output statement
#include <iostream>
Compiler directive: Tells the compiler what to do before compiling
This one includes source code from another file
int main()
❖ The braces enclose the body of the
{ function
//Declaration ❖ They represent the start and end of a
//Statements function block
return 0;
}
Declaration Statements
int main()
❖ It specifies the value that the function is
{
expected to return
//Declaration
❖ It ends in a semi-colon(;)
//Statements
❖ Almost all declaration statements end in a
return 0; semi-colon
}
Data Types
Data types define the type of data or content a variable can hold. Examples are:
❖ int - 2, 626, 30
❖ float - 3.56, 96.999
❖ char - ‘a’, ‘b’
❖ Bool - True, False
❖ Array - fruits[apple, banana, grape]
❖ Function - do_this()
❖ String - “Hello world!”
Variables
Variables are used to store data. They are unique names that can be
referenced and modified in the program and they follow unique naming
conventions. They may be a mixture of alphabets [a-z][A-Z], numbers[0-9]
and the underscores(_).
They are words that are already in use by the program and therefore can not be
used as variables.
Examples:
❖ If
❖ For
❖ While
❖ Class
❖ Struct
Value Assignment
#include <iostream>
using namespace std; cout is drawn from the iostream header
library for outputting to the console.
int main()
{
int number;
The angle braces << indicate the direction
to the console
cout << “Enter a number” << endl;
cin >> number;
endl represents an end-of-line or an
cout << “You entered: “<<number<< endl;
addition of a new line.
}
Console input
#include <iostream>
using namespace std;
cin is drawn from the iostream header
library for inputting to the console.
int main()
{
int number; The angle braces >> indicate the direction
cout << “Enter a number” << endl; to the console.
cin >> number;
cout << “You entered: “ << number << endl;
}
Expected Output
#include <iostream>
using namespace std; Output:
int main()
{ Enter a number
int number; 7
cout << “Enter a number” << endl; You entered: 7
cin >> number;
cout << “You entered: “ << number <<
endl;
}
Without using namespace std
#include <iostream>
using namespace std;
int main()
{
cout << “Hello, World!” << endl;
return 0;
}
Arithmetic Operations
Mathematical calculations can also be done in programming with the result being
outputted right away or being stored in a variable to be used later.
Addition: 3 + 19
Multiplication: 5 * 2
Subtraction: 9-8
Division: 58 / 7
End