Ministry of Higher Education Computer Department
Noble Institute 1st Stage
C++ Programming
Academic Year
2018-2019
Lecturer
Zanco A. Taha (MSc.)
Outlines
• Variables and constants
• Strings and operators
• Input and output
2
Declaration of variables
In order to use a variable in C++, we must first declare it specifying
which data type we want it to be. The syntax to declare a new
variable is to write the specifier of the desired data type (like int,
bool, float...) followed by a valid
variable identifier. For example:
int a;
float mynumber;
int a, b, c;
or
int a;
int b;
int c;
Declaration of variables
#include <iostream>
using namespace std;
int main () {
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
// print out the result:
cout << result;
// terminate the program:
return 0;}
Initialization of variables
When declaring a regular local variable, its value is by default
undetermined. But you may want a variable to store a concrete
value at the same moment that it is declared. In order to do that,
you can initialize the variable. There are two ways to do this in
C++:
type identifier = initial_value ;
For example:
int a = 0;
Or
type identifier (initial_value) ;
For example:
int a (0);
Initialization of variables
// initialization of variables
#include <iostream>
using namespace std;
int main ()
{
int a=5; // initial value = 5
int b(2); // initial value = 2
int result; // initial value
undetermined
a = a + 3;
result = a - b;
cout << result;
return 0;
}
Introduction to strings
Variables that can store non-numerical values that are longer than
one single character are known as strings. The C++ language
library provides support for strings through the standard string
class. This is not a fundamental type, but it behaves in a similar
way as fundamental types do in its most basic usage.
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring = "This is a string";
cout << mystring;
return 0;
}
Constants
Constants are expressions with a fixed value.
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
const int pathwidth = 100;
const char stage = ‘1';
return 0;
}
Arithmetic operators
The five arithmetical operations supported by the C++ language
are:
+ addition
- subtraction
* multiplication
/ division
% modulo
Compound assignment
When we want to modify the value of a variable by performing an
operation on the value currently stored in that variable we can use
compound assignment operators:
value += increase; value = value + increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units + 1; price = price * (units + 1);
Increase and decrease (++, --)
Shortening even more some expressions, the increase operator
(++) and the decrease operator (--) increase or
reduce by one the value stored in a variable. They are equivalent to
+=1 and to -=1, respectively. Thus:
c++;
c+=1;
c=c+1;
are all equivalent in its functionality: the three of them increase by
one the value of c.
Relational and equality operators
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Ex:
(7 == 5) // evaluates to false.
(5 > 4) // evaluates to true.
(3 != 2) // evaluates to true.
(6 >= 6) // evaluates to true.
(5 < 5) // evaluates to false.
Logical operators ( !, &&, || )
!(5 == 5) // evaluates to false.
!false // evaluates to true.
( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ).
( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).
Conditional operator ( ? )
The conditional operator evaluates an expression returning a value
if that expression is true and a different one if
the expression is evaluated as false. Its format is:
condition ? result1 : result2
If condition is true the expression will return result1, if it is not it
will return result2.
7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5.
7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.
5>3 ? a : b // returns the value of a, since 5 is greater than 3.
a>b ? a : b // returns whichever is greater, a or b.
Conditional operator ( ? )
// conditional operator
#include <iostream>
using namespace std;
int main ()
{
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout << c;
return 0;
}
Basic Input/Output
The standard C++ library includes the header file iostream, where
the standard input and output stream are declared.
By default, the standard output of a program is the screen, and the
C++ stream object defined to access it is cout.
cout is used in conjunction with the insertion operator, which is
written as << (two "less than" signs).
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the content of x on screen
cout << "Hello"; // prints Hello
cout << Hello; // prints the content of Hello variable
cout << "Hello, " << "I am " << "a C++ statement";
cout << "Hello, I am " << age <<endl;
Basic Input/Output
The standard input device is usually the keyboard. Handling the
standard input in C++ is done by applying the overloaded operator
of extraction (>>) on the cin stream. The operator must be followed
by the variable that will store the data that is going to be extracted
from the stream
Cin>>a;
Cin>>b>>c>>d;
Cin>>MyName;