Instructor: Bakhyt Bakiyev
Basic things
content
Introduction: history of C and C++, and differences.
Data types
if, if – else, conditional operators,
switch
loops
C is a general-purpose computer programming
language developed in 1972 by Dennis Ritchie at the Bell
Telephone Laboratories for use with the Unix operating
system.
C++ was written by Bjarne Stroustrup at Bell Labs
during 1979-1985. C++ is an extension of C. Bjarne
Stroustrup added features to C and he called it as "C with
Classes". An then the term C++ was first used in 1983.
History of C and C++
http://www.cs.bell-labs.com/who/dmr/
http://www2.research.att.com/~bs/homepage.html
C and C++ difference
C++ is a general–purpose programming language with a bias
towards systems programming
is an extension of C;
supports data abstraction
supports object-oriented programming
First program in C++
#include<iostream>
using namespace std;
int main()
{
cout<<”salem SDU! \n”;
return 0;
}
salem SDU!
// first program in C++
This is a comment line. All lines beginning with two slash signs
(//) are considered comments and do not have any effect on the
behavior of the program. The programmer can use them to
include short explanations.
#include <iostream>
Lines beginning with a hash sign (#) are directives for the
preprocessor. #include <iostream> tells the preprocessor to
include the iostream standard file. This specific file (iostream)
and includes the declarations of the basic standard input-output
library in C++.
main ()
This line corresponds to the beginning of the definition of the
main function. The main function is the point by where all C++
programs start their execution, independently of its location
within the source code. It does not matter whether there are
other functions with other names defined before or after it - the
instructions contained within this function's definition will
always be the first ones to be executed in any C++ program.
Escape Sequence
\n - Newline. Position the screen cursor to the beginning of the
next line.
\t - Horizontal tab. Move the screen cursor to the next tab stop.
\r - Carriage return. Position the screen cursor to the beginning
of the current line.
#include<iostream>
using namespace std;
int main()
{
// first program in C++
cout<<”Salem \n”<<“SDU !”;
return 0;
}
Salem
SDU!
Memory concepts a = 5;
b = 2;
a = a + 1;
result = a - b;
Any variable names actually correspond to locations in the
computer's memory. Every variable has a name, a type, a size and
a value.
Notice, we can give any names for variables except reserved
words.
The standard reserved keywords:
asm, auto, bool, break, case, catch, char, class, const, const_cast,
continue, default, delete, do, double, dynamic_cast, else, enum,
explicit, export, extern, false, float, for, friend, goto,if, inline,
int, long, mutable, namespace, new, operator, private, protected,
public, register,reinterpret_cast, return, short, signed, sizeof,
static, static_cast, struct, switch, template, this, throw, true, try,
typedef, typeid, typename, union, unsigned, using, virtual, void,
volatile, wchar_t, while.
Fundamental data types Name Description Size Range
char Character. 1byte signed: -128 to 127 unsigned: 0 to 255
short int (short)
Short Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535
int Integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
long int (long)
Long integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
bool Boolean value. It can take one of two values: true or false.
1byte true or false
float Floating point number. 4bytes (~7 digits)
double Double precision floating point number.
8bytes (~15 digits)
long double Long double precision floating point number.
8bytes (~15 digits)
wchar_t Wide character. 2 or 4 bytes
1 wide character
Scope of Variable A variable can be either of global or local scope.
Taking an information For example:
int age;
cin >> age;
There is also usage of cin to request more than one data from the
user:
cin >> a >> b;
is equivalent to:
cin >> a;
cin >> b;
Displaying an information For example:
int age=20;
cout <<age;
There is also usage of cout to display more than one data at a time:
int a=10, b=20;
cout << a << b;
is equivalent to:
cout << a;
cout<<b;
Assignment (=) The assignment operator assigns a value to a variable.
a = 5;
a = 2 + (b = 5);
is equivalent to: b = 5; a = 2 + b;
Arithmetic operators
C++ operation
C++ arithmetic
operator
Algebraic
expression
C++
expression
Addition + x + y x + y
Subtraction - x - y x – y
Multiplication * xy or x · y x * y
Division / x/ y
Modulus % x mod y x % y
if & if-else
Nested if-else
if ( x > 5 )
if ( y > 5 ) cout << "x and y are > 5";
else cout << "x is <= 5";
if ( x > 5 )
{
if ( y > 5 ) cout << "x and y are > 5";
}
else cout << "x is <= 5";
(= and ==)?
Bool data type Bool data types can take only 2 values true or false, false is equal to 0 and true is equa
l to 1.
Char data type In C++ char data type is used for a single character. Collection of char
acters is ASCII.
Note: char c='a' works fine, but char c=“a”; is compilation error.
Scope A variable can be either of global or local scope. If there is same name for global and l
ocal variable “::” keyword is used for global.
Escape codes
In C++ there are escape codes, which are used to express more additional symbols
or actions. Its structure consists of “\” sign and letter.
Type casting There are 2 ways:
answer = (double) (numerator) / denominator
answer = static_cast<double>(numerator) / denominator
Conditional operator ? In C++ it can be used conditional operator “?” instead of if .. else structure.
Its structure is : condition ? result1 : result2;
Short writing operators For example “x = x + 5;” can be written as “x += 5;”
Also for short writing code increase (++) or decrease (--) are used.
pre – post
Comparision
relational operator meaning_________
> x is greater than y
< x is less than y
>= x is greater than or equal to y
<= x is less than or equal to y
== x is equal to y
!= x is not equal to y
Logical operators
3 main logical operators in C++: “and” (&&), “or” (||) and “not” (!).
Math library
switch switch ( integer expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
Ex: main( )
{
int i = 2 ;
switch ( i )
{
case 1 : cout<<"I am in case 1 \n" ; break ;
case 2 : cout<<"I am in case 2 \n" ; break ;
case 3 : cout<<"I am in case 3 \n" ; break ;
default : cout<<"I am in default \n";
}
}
LOOPs
while
do … while
for
WHILE The “while” loop's structure is : while (condition(s) is(are) true) { code }
In while loop there must be increment (decrement) operations or some checking to make condition false, otherwise it can infinite loop.
DO … WHILE
The “do .. while” loop's structure is do { code } while (condition[s] is[are] true); The main difference between do/while and while loops is that sometimes in do/while it can be 1 mo
re step, because firstly it run code then checks the condition.
FOR
The “for” loop's structure is: for (initialization; condition; increase)
STRUCTURE
FOR vs WHILE
BREAK
Break command is used to finish loop and exit from loop even if the loop condition is true.
CONTINUE The continue causes to pass rest of loop and go to next iteration.
EXIT
The “exit” is used to terminate program even if it is not finished. It can be used when error happens in code.
while (true) {
}
INFINITE LOOPS
Statements with “for”
for (i=0, j=1; i<=10; i=i+2, j++) {
j = j+3; i++;
}
FOR in sdutents’ dining-hall
FOR (SDUdent i = 1; i<=7; i++)