Professional Documents
Culture Documents
Introduction to Programming
Chapter 3
Syntax of C++
Introduction to Programming
Learning outcomes:
#include <iostream>
using namespace std;
int main( ) {
cout << “Welcome to C++ Programming";
}
Introduction to Programming
#include <iostream>
using namespace std;
int main( ) {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age;
}
Introduction to Programming
Standard end line (endl):
• The endl is a predefined object of ostream class. It is used to insert a new line characters.
• Let's see the simple example of standard end line (endl):
#include <iostream>
using namespace std;
int main( ) {
cout << "C++ Programming";
cout << " is easy"<<endl;
cout << “and interesting"<<endl;
}
Introduction to Programming
C++ Comments:
• The C++ comments are statements that are not executed by the compiler.
• The comments in C++ programming can be used to provide explanation of
the code, variable, method or class.
• By the help of comments, you can hide the program code also.
There are two types of comments in C++.
1. Single Line comment
2. Multi Line comment
Introduction to Programming
• The C++ multi line comment is used to comment multiple lines of code.
• It is surrounded by slash and asterisk (/∗ ..... ∗/).
Introduction to Programming
• Example of multi line comment in C++:
#include <ostream>
using namespace std;
int main()
{
/* declare and
print variable in C++. */
int x = 35;
cout<<x; }
Introduction to Programming
C++ Variable
• A variable is a name of memory location and it is used to store data. Its value
can be changed and it can be reused many times.
• It is a way to represent memory location through symbol so that it can be easily
identified.
Syntax to declare a variable:
Data_type variable_name;
Introduction to Programming
The example of declaring variable is given below:
int x;
float y;
char z;
Here, x, y, z are variables and int, float, char are data types.
We can also provide values while declaring the variables which is called as
variable initialization:
int x = 5, b = 10; //declaring 2 variable of integer type
float f = 30.8;
char c = 'A';
Introduction to Programming
• int a; (…)
• int _ab; (…)
• int 4; (…)
• int x y; (…)
• int a30; (…)
• int double; (…)
Introduction to Programming
Numeric Types:
• Use int when you need to store a whole number without decimals, like
35 or 1000.
• Use float or double when you need a floating point number (with
decimals), like 9.99 or 3.14515.
Introduction to Programming
Numeric Types example:
#include <iostream>
using namespace std;
int main () {
int myNum1 = 1000;
float myNum2 = 5.75;
cout << myNum1;
cout << myNum2;
}
Introduction to Programming
Boolean Types:
A boolean data type is declared with the bool keyword and can only
take the values true or false.
When the value is returned, true = 1 and false = 0.
Introduction to Programming
#include <iostream>
using namespace std;
int main() {
bool isCodingFun = true;
bool isDifficult = false;
cout << isCodingFun << "\n";
cout << isDifficult;
}
Introduction to Programming
Character Types:
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
#include <iostream>
using namespace std;
int main () {
char myGrade = 'B';
cout << myGrade;
}
Introduction to Programming
String Types:
Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string greeting = "Hello";
cout << greeting;
}
Introduction to Programming
C++ Constants
In C++, we can create variables whose value cannot be changed. For that,
we use the const keyword.
Example:
const int SPEED = 299792458;
• Now trying to change the value of variable SPEED result in an error in the
program.
• SPEED = 2500 // Error! SPEED is a constant.