SlideShare a Scribd company logo
1
Basic terms in coding
• What is programming?
• Programming language is a tool used to create a program by writing human
like language in a specific structure (syntax) using an editor that will translate
the human language to binary language of zeros and ones, which is the
computer language.
• Programming is using a code or source code, which is a sequence of
instructions to develop a program that will provide an output solution using
several tools such as a programming language & programming language
editor.
• output: The messages printed to the user by a program.
• console: The text box onto which output is printed.
• syntax: The set of legal structures and commands that can be used in a particular
programming language. 2
What is the differences between C#, C++,
and C?
• C and C++ differences:
• C++ was built as an extension of C, which means it can run most C code.
• C++ supports reference variables, which C does not.
• C uses functions for input and output, whereas C++ uses objects for input and output.
• C does not provide error or exception handling, but C++ does.
• As an object-oriented language, C++ supports polymorphism, encapsulation, and inheritance, while C does not.
• C++ and C# differences:
• C++ compiles into machine code, while C# compiles to CLR, which is interpreted by ASP.NET.
• C++ can be used on any platform, though it was originally designed for Unix-based systems. C# is standardized but is
rarely used outside of Windows environments.
• C++ can create stand-alone and console applications. C# can create a console, Windows, ASP.NET, and mobile
applications, but cannot create stand-alone apps.
• C++ requires you to handle memory manually, but C# runs in a virtual machine which can automatically handle
memory management. 3
Structure of a C++ Program
4
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
return (0);
}
Used to group
statements
Comment line
Execution begin
Standard output stream in C++
• Select Execute> Compile,
to compile your program.
• Select Execute> Run, to
run your program.
Hello World!
Output:
Comments
• C++ support two ways to insert comments:
• // line comment
• /* block Comment */
5
/* my second program in C++
With more comment */
#include <iostream>
using namespace std;
int main (){
cout << "Hello World! "; //says Hello World
cout << "I am a C++ program "; // says I am a C++ program
}
Hello World! I am a C++ program
Example: Output:
Good Programming Practice
• Every program should begin with a comment that explains the purpose of
program, author and creation date and last update date.
• Example:
/* Created By: Maryam AlMahdi
Creation Date: 7 July 2021
Last Updated Date: 29 July 2021
Description: ................
*/
6
Output Statement: (cout)
7
cout<<expression<<expression…..;
cout<< “First sentence. ”;
cout<< “Second sentence. Third sentence.”;
Example:
After adding Escape Sequences:
cout<< “First sentence. n ”;
cout<< “Second sentence. n Third sentence.”;
cout<< “First sentence.”;
cout<< “nSecond sentence. t Third sentence.”;
cout<< 123;
cout<< “The result is =“ << 12+14;
Output Statement:
Escape Sequences
8
n New line
r Carriage return
t tabulation
v Vertical tabulation
b backspace
f Page feed
a Alert (beep)
’ Single quote(‘)
” Double quote (“)
? Question (?)
 Inverted slash
Variables, Constants, Data types
• Variable:
A memory location whose content may change during program execution.
• Constant:
A memory location whose content is not allowed to change during program
execution.
• An Identifier:
is a sequence of one or more letters, digits or underline symbols ( _ ), and must
begin with a letter or underscore.
9
Variables, Constants, Data types:
Identifier
• The length of an identifier is not limited, although for some compilers only the 32
first characters of an identifier are significant (the rest are not considered).
• Neither spaces nor marked letters can be part of an identifier. Only letters, digits
and underline characters are valid.
• Identifier should not match any reserved key word of the C++ language.
• The C++ language is "case sensitive", that means that an identifier written in
capital letters is not equivalent to another one with the same name but written in
small letters.
10
Variables, Constants, Data types:
Declaration of Variable
11
DataType identifier, identifier,….;
Example:
int a;
char ch,x,y;
short NumberOfSons;
int MyAccountBalance;
Variables, Constants, Data types: Data Type
12
Name Bytes* Description Range
char 1 character or integer 8 bits length. signed: -128 to 127 unsigned: 0 to 255
short 2 integer 16 bits length. signed: -32768 to 32767 unsigned: 0
to 65535
long 4 integer 32 bits length. signed:-2147483648 to 2147483647
unsigned: 0 to 4294967295
int * Integer. Its length traditionally depends on the length of the system's Word type,
thus in MSDOS it is 16 bits long, whereas in 32 bit systems (like Windows
9x/2000/NT and systems that work under protected mode in x86 systems) it is 32
bits long (4 bytes).
See short, long
float 4 floating point number. 3.4e + / - 38 (7 digits)
double 8 double precision floating point number. 1.7e + / - 308 (15 digits)
Long double 10 long double precision floating point number. 1.2e + / - 4932 (19 digits)
bool 1 Boolean value. It can take one of two values: true or false NOTE: this is a type
recently added by the ANSI-C++ standard. Not all compilers support it. Consult
section bool
type for compatibility information.
true or false
string 8 stores text, such as "Hello World". String values are surrounded by double quotes.
This is not a built-in type, but it behaves like one in its most basic usage
“text”
Variables, Constants, Data types:
Declaration of Variable
13
// operating with 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)
}
output is 4
Variables, Constants, Data types:
Initialization of variables
14
• Example:
int x=0;
int x(0);
int a=5, b=2;
DataType= initial_value;
DataType (initial_value);
// operating with variables
#include <iostream> using namespace std;
int main ()
{// declaring variables:
int result;
// process:
int a(5);
int b(2);
a = a + 1;
result = a - b;
// print out the result:
cout << result;
// terminate the program:
}
Or int a(5),b(2);
Output Statement (Cont’d)
15
• Examples
cout<<expression<<expression..;
cout<<”Results”; //prints Results on screen
cout<<120; //prints number 120 on screen
cout<<a; //prints the content of variable a on screen
cout<<“a-b”<<result;
cout<<a<<“-”<<b<<“=“<<result;
Variables, Constants, Data types:
Declared Constant (const)
• With the const prefix you can declare constants with a specific type
exactly as you would do with a variable:
• Examples:
const int width = 100;
const int zip = 12440;
16
const dataTypes identifier = value;
Operators
• The Assignment (=) operator serves to assign a value to a variable.
• Assignation operation always takes place from right to left and never
at the inverse.
17
Variable = expression ;
Operators: Assignment operator
• Examples:
18
int a,b,c;
a=10;
b=4;
a=b;
b=7;
a=2+(b=5); // is equivalent to b=5;
a=2+b;
a=b=c=5; // assigns 5 to the three variables a, b and c.
cout<<a,b,c;
Operators:
Arithmetic and Relational Operators
• Arithmetic operators ( +, -, *, /, %)
Module (%) is the operation that gives the remainder of a division of two integer values,
• Example:
• Relational operators ( ==, !=, >, <, >=, <= )
• The result of a relational operation is a Boolean value that can only be true(1) or false(0).
19
int a=11%3; //a=2
cout<<a;
int a=(11<3); //will give us 0 as false
cout<<a<<“n”;
int b=(11>3); //will give us 1 as true
cout<<b;
Operators:
Logic operators
• Logic operators ( !, &&, || ).
They correspond with Boolean logic operations NOT, AND and OR Respectively
• Examples:
20
First Operand
a
Second
Operand b
result
a && b
result
a || b
true true true true
true false false true
false true false true
false false false false
int a( (5 == 5) && (3 > 6) ); // returns false ( true && false ).
cout<<a<<”n”;
int b( (5 == 6) || (3 != 6)); // returns true ( true || false ).
cout<<b;
Operators
• Compound assignation operators
(+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
• Increase(++) and decrease (--)
• They increase or reduce by 1 the value stored in a variable.
• They are equivalent to +=1 and to -=1, respectively.
• They can be used both as a prefix or (++a) as a suffix(a++).
21
value += increase; // is equivalent to value=value + increase;
a -= 5; // is equivalent to a=a-5;
a /= b; // is equivalent to a=a/b;
price *= units + 1; // is equivalent to price = price * (units + 1);
Operators
• Example 1:
equivalent to equivalent to
• Example 2: Example 3:
22
a++; a=a+1; a+=1;
int B=3;
int A=++B;
// A is 4, B is 4
cout<<A<<“n”<<B;
int B=3;
int A=B++;
// A is 3, B is 4
cout<<A<<“n”<<B;
Operators : Priority of operators
Priority Operator Description Associativity
1 () [ ] Left
2 ++ -- increment/decrement Right
~ Complement to one (bitwise)
! unary NOT
& * Reference and Dereference (pointers)
(type) Type casting
+ - Unary sign
3 * / % arithmetical operations Left
4 + - arithmetical operations Left
5 < <= > >= Relational operators Left
6 == != Relational operators Left
7 && || Logic operators Left
10 = += -= *= /= %=
>>= <<= &= ^= |=
Assignation Right
11 , Comma, Separator Left 23
Input Statement: (cin)
• cin only process the input from the keyboard once the RETURN key
has been pressed.
• Example 1:
• Example 2:
equivalent to
24
cin>> variable >>variable .... ;
int age;
cin >> age;
cin>> a >> b;
cin>> a;
cin>> b;
Input Statement: (cin)
25
#include <iostream>
using namespace std;
/* Created By: Maryam AlMahdi
Creation Date: 29 July 2021
Description: .. */
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << "n";
return 0;
}
Please enter an integer value: 702
The value you entered is 702 and its double is 1404.
Control Statements: Single-Selection statement
• It is used to execute an instruction or block of instructions only if a
condition is fulfilled.
• If the condition is false, statement is ignored (not executed) and the
program continues on the next instruction after the conditional
structure.
• Example 1: Example2:
26
If (condition ) statement
if (x == 100)
{
cout << "x is ";
cout << x;
}
if (x == 100)
cout << "x is 100";
Control Statements: Double-Selection statement
• Example1: Example2:
27
If (condition ) statement1 else statement2
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
Let’s review what we’ve learned
• Write a program that includes the following:
x=“Hello”
Name= takes input from the user
Print the statement Hello + the user name.
28
Let’s review what we’ve learned
• Solution:
#include <iostream>
using namespace std;
int main()
{
string x="Hello";
string Name;
cout<<"What is your name?"<<"t";
cin>> Name;
cout<<x<<"t"<<Name;
return 0;
}
29
Quick note:
In C++ every data type
has a specific number of
bytes to store as shown
in this hypertext link of
slide 13.Date Types.
Let’s review what we’ve learned
• Write a program as showing below:
a=2
b=5
c=8
Use if else statement to print “a is less than c”.
30
Let’s review what we’ve learned
• Solution:
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=2;
b=5;
c=8;
if (a<c)
cout<<"a is less than c";
else
cout<<"a is greater than c";
return 0;
}
31
References
• Information Technology Infrastructure, Dr. Maryam AlOtaibi.
• A Complete guide to Programming in C++ y Ulla Kirch-Prinz & Peter
Prinz.
32
Contact info
• Maryam AlMahdi
• E-mail: mariam.almahdi@outlook.com
• Instagram: @gdgkwt_maryam
33
Variables, Constants, Data types: Data Type
34
Name Bytes* Description Range
char 1 character or integer 8 bits length. signed: -128 to 127 unsigned: 0 to 255
short 2 integer 16 bits length. signed: -32768 to 32767 unsigned: 0
to 65535
long 4 integer 32 bits length. signed:-2147483648 to 2147483647
unsigned: 0 to 4294967295
int * Integer. Its length traditionally depends on the length of the system's Word type,
thus in MSDOS it is 16 bits long, whereas in 32 bit systems (like Windows
9x/2000/NT and systems that work under protected mode in x86 systems) it is 32
bits long (4 bytes).
See short, long
float 4 floating point number. 3.4e + / - 38 (7 digits)
double 8 double precision floating point number. 1.7e + / - 308 (15 digits)
Long double 10 long double precision floating point number. 1.2e + / - 4932 (19 digits)
bool 1 Boolean value. It can take one of two values: true or false NOTE: this is a type
recently added by the ANSI-C++ standard. Not all compilers support it. Consult
section bool
type for compatibility information.
true or false
string 8 stores text, such as "Hello World". String values are surrounded by double quotes.
This is not a built-in type, but it behaves like one in its most basic usage
“text”

More Related Content

PPTX
Unit ii
PPTX
C Programming basics
PPT
Basics of c++
PPTX
PPTX
C++ PROGRAMMING BASICS
ODP
Basic C Programming language
DOCX
C-PROGRAM
PPTX
Introduction of c programming unit-ii ppt
Unit ii
C Programming basics
Basics of c++
C++ PROGRAMMING BASICS
Basic C Programming language
C-PROGRAM
Introduction of c programming unit-ii ppt

What's hot (19)

ODP
(2) cpp imperative programming
ODP
CProgrammingTutorial
PPT
Unit 4 Foc
PPTX
C tokens
PDF
Learning c - An extensive guide to learn the C Language
PPT
Beginner C++ easy slide and simple definition with questions
PPT
Introduction to C
PPS
basics of C and c++ by eteaching
PPT
Javascript by Yahoo
PPSX
Complete C++ programming Language Course
PDF
Introduction to C programming
PPTX
Introduction to C Programming
PPTX
C++ Basics introduction to typecasting Webinar Slides 1
PPTX
C++ lecture 01
ODP
OpenGurukul : Language : C Programming
PPT
The smartpath information systems c pro
PPT
C program
PPTX
C++ programming language basic to advance level
PPTX
C++ Basics
(2) cpp imperative programming
CProgrammingTutorial
Unit 4 Foc
C tokens
Learning c - An extensive guide to learn the C Language
Beginner C++ easy slide and simple definition with questions
Introduction to C
basics of C and c++ by eteaching
Javascript by Yahoo
Complete C++ programming Language Course
Introduction to C programming
Introduction to C Programming
C++ Basics introduction to typecasting Webinar Slides 1
C++ lecture 01
OpenGurukul : Language : C Programming
The smartpath information systems c pro
C program
C++ programming language basic to advance level
C++ Basics
Ad

Similar to #Code2 create c++ for beginners (20)

PPTX
POLITEKNIK MALAYSIA
PPT
M.Florence Dayana / Basics of C Language
PPSX
Esoft Metro Campus - Programming with C++
PDF
Lecture1
PPTX
Lecture 2 variables
PPT
Basics of C.ppt
PPTX
Fundamentals of computers - C Programming
PPTX
C Programming Unit-1
PPTX
C programming language
PDF
CP c++ programing project Unit 1 intro.pdf
PPT
Introduction to C Programming
PPTX
C_Programming_Language_tutorial__Autosaved_.pptx
PPTX
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
PDF
C-PPT.pdf
PPT
Ch2 introduction to c
PDF
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
PPT
Ffghhhhfghhfffhjdsdhjkgffjjjkfdghhftgdhhhggg didi ucch JFK bcom
PPTX
C programming tutorial for Beginner
PPTX
computer ppt group22222222222222222222 (1).pptx
PPTX
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
POLITEKNIK MALAYSIA
M.Florence Dayana / Basics of C Language
Esoft Metro Campus - Programming with C++
Lecture1
Lecture 2 variables
Basics of C.ppt
Fundamentals of computers - C Programming
C Programming Unit-1
C programming language
CP c++ programing project Unit 1 intro.pdf
Introduction to C Programming
C_Programming_Language_tutorial__Autosaved_.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
C-PPT.pdf
Ch2 introduction to c
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
Ffghhhhfghhfffhjdsdhjkgffjjjkfdghhftgdhhhggg didi ucch JFK bcom
C programming tutorial for Beginner
computer ppt group22222222222222222222 (1).pptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
Ad

More from GDGKuwaitGoogleDevel (11)

PDF
معسكر أساسيات البرمجة في لغة بايثون.pdf
PPTX
i/o extended: Intro to <UX> Design
PDF
#Code2Create:: Introduction to App Development in Flutter with Dart
PPTX
#Code2Create: Python Basics
PPTX
Building arcade game using python workshop
PDF
Wordpress website development workshop by Seham Abdlnaeem
PPTX
WTM/IWD 202: Introduction to digital accessibility by Dr. Zainab AlMeraj
PPTX
GDG Kuwait - Modern android development
PDF
DevFest Kuwait 2020 - Building (Progressive) Web Apps
PPTX
DevFest Kuwait 2020- Cloud Study Jam: Kubernetes on Google Cloud
PPTX
DevFest Kuwait 2020 - GDG Kuwait
معسكر أساسيات البرمجة في لغة بايثون.pdf
i/o extended: Intro to <UX> Design
#Code2Create:: Introduction to App Development in Flutter with Dart
#Code2Create: Python Basics
Building arcade game using python workshop
Wordpress website development workshop by Seham Abdlnaeem
WTM/IWD 202: Introduction to digital accessibility by Dr. Zainab AlMeraj
GDG Kuwait - Modern android development
DevFest Kuwait 2020 - Building (Progressive) Web Apps
DevFest Kuwait 2020- Cloud Study Jam: Kubernetes on Google Cloud
DevFest Kuwait 2020 - GDG Kuwait

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation theory and applications.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Big Data Technologies - Introduction.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The AUB Centre for AI in Media Proposal.docx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Spectral efficient network and resource selection model in 5G networks
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Machine learning based COVID-19 study performance prediction
Empathic Computing: Creating Shared Understanding
Encapsulation theory and applications.pdf
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx
Network Security Unit 5.pdf for BCA BBA.
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
NewMind AI Weekly Chronicles - August'25 Week I
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

#Code2 create c++ for beginners

  • 1. 1
  • 2. Basic terms in coding • What is programming? • Programming language is a tool used to create a program by writing human like language in a specific structure (syntax) using an editor that will translate the human language to binary language of zeros and ones, which is the computer language. • Programming is using a code or source code, which is a sequence of instructions to develop a program that will provide an output solution using several tools such as a programming language & programming language editor. • output: The messages printed to the user by a program. • console: The text box onto which output is printed. • syntax: The set of legal structures and commands that can be used in a particular programming language. 2
  • 3. What is the differences between C#, C++, and C? • C and C++ differences: • C++ was built as an extension of C, which means it can run most C code. • C++ supports reference variables, which C does not. • C uses functions for input and output, whereas C++ uses objects for input and output. • C does not provide error or exception handling, but C++ does. • As an object-oriented language, C++ supports polymorphism, encapsulation, and inheritance, while C does not. • C++ and C# differences: • C++ compiles into machine code, while C# compiles to CLR, which is interpreted by ASP.NET. • C++ can be used on any platform, though it was originally designed for Unix-based systems. C# is standardized but is rarely used outside of Windows environments. • C++ can create stand-alone and console applications. C# can create a console, Windows, ASP.NET, and mobile applications, but cannot create stand-alone apps. • C++ requires you to handle memory manually, but C# runs in a virtual machine which can automatically handle memory management. 3
  • 4. Structure of a C++ Program 4 // my first program in C++ #include <iostream> using namespace std; int main () { cout << "Hello World! "; return (0); } Used to group statements Comment line Execution begin Standard output stream in C++ • Select Execute> Compile, to compile your program. • Select Execute> Run, to run your program. Hello World! Output:
  • 5. Comments • C++ support two ways to insert comments: • // line comment • /* block Comment */ 5 /* my second program in C++ With more comment */ #include <iostream> using namespace std; int main (){ cout << "Hello World! "; //says Hello World cout << "I am a C++ program "; // says I am a C++ program } Hello World! I am a C++ program Example: Output:
  • 6. Good Programming Practice • Every program should begin with a comment that explains the purpose of program, author and creation date and last update date. • Example: /* Created By: Maryam AlMahdi Creation Date: 7 July 2021 Last Updated Date: 29 July 2021 Description: ................ */ 6
  • 7. Output Statement: (cout) 7 cout<<expression<<expression…..; cout<< “First sentence. ”; cout<< “Second sentence. Third sentence.”; Example: After adding Escape Sequences: cout<< “First sentence. n ”; cout<< “Second sentence. n Third sentence.”; cout<< “First sentence.”; cout<< “nSecond sentence. t Third sentence.”; cout<< 123; cout<< “The result is =“ << 12+14;
  • 8. Output Statement: Escape Sequences 8 n New line r Carriage return t tabulation v Vertical tabulation b backspace f Page feed a Alert (beep) ’ Single quote(‘) ” Double quote (“) ? Question (?) Inverted slash
  • 9. Variables, Constants, Data types • Variable: A memory location whose content may change during program execution. • Constant: A memory location whose content is not allowed to change during program execution. • An Identifier: is a sequence of one or more letters, digits or underline symbols ( _ ), and must begin with a letter or underscore. 9
  • 10. Variables, Constants, Data types: Identifier • The length of an identifier is not limited, although for some compilers only the 32 first characters of an identifier are significant (the rest are not considered). • Neither spaces nor marked letters can be part of an identifier. Only letters, digits and underline characters are valid. • Identifier should not match any reserved key word of the C++ language. • The C++ language is "case sensitive", that means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters. 10
  • 11. Variables, Constants, Data types: Declaration of Variable 11 DataType identifier, identifier,….; Example: int a; char ch,x,y; short NumberOfSons; int MyAccountBalance;
  • 12. Variables, Constants, Data types: Data Type 12 Name Bytes* Description Range char 1 character or integer 8 bits length. signed: -128 to 127 unsigned: 0 to 255 short 2 integer 16 bits length. signed: -32768 to 32767 unsigned: 0 to 65535 long 4 integer 32 bits length. signed:-2147483648 to 2147483647 unsigned: 0 to 4294967295 int * Integer. Its length traditionally depends on the length of the system's Word type, thus in MSDOS it is 16 bits long, whereas in 32 bit systems (like Windows 9x/2000/NT and systems that work under protected mode in x86 systems) it is 32 bits long (4 bytes). See short, long float 4 floating point number. 3.4e + / - 38 (7 digits) double 8 double precision floating point number. 1.7e + / - 308 (15 digits) Long double 10 long double precision floating point number. 1.2e + / - 4932 (19 digits) bool 1 Boolean value. It can take one of two values: true or false NOTE: this is a type recently added by the ANSI-C++ standard. Not all compilers support it. Consult section bool type for compatibility information. true or false string 8 stores text, such as "Hello World". String values are surrounded by double quotes. This is not a built-in type, but it behaves like one in its most basic usage “text”
  • 13. Variables, Constants, Data types: Declaration of Variable 13 // operating with 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) } output is 4
  • 14. Variables, Constants, Data types: Initialization of variables 14 • Example: int x=0; int x(0); int a=5, b=2; DataType= initial_value; DataType (initial_value); // operating with variables #include <iostream> using namespace std; int main () {// declaring variables: int result; // process: int a(5); int b(2); a = a + 1; result = a - b; // print out the result: cout << result; // terminate the program: } Or int a(5),b(2);
  • 15. Output Statement (Cont’d) 15 • Examples cout<<expression<<expression..; cout<<”Results”; //prints Results on screen cout<<120; //prints number 120 on screen cout<<a; //prints the content of variable a on screen cout<<“a-b”<<result; cout<<a<<“-”<<b<<“=“<<result;
  • 16. Variables, Constants, Data types: Declared Constant (const) • With the const prefix you can declare constants with a specific type exactly as you would do with a variable: • Examples: const int width = 100; const int zip = 12440; 16 const dataTypes identifier = value;
  • 17. Operators • The Assignment (=) operator serves to assign a value to a variable. • Assignation operation always takes place from right to left and never at the inverse. 17 Variable = expression ;
  • 18. Operators: Assignment operator • Examples: 18 int a,b,c; a=10; b=4; a=b; b=7; a=2+(b=5); // is equivalent to b=5; a=2+b; a=b=c=5; // assigns 5 to the three variables a, b and c. cout<<a,b,c;
  • 19. Operators: Arithmetic and Relational Operators • Arithmetic operators ( +, -, *, /, %) Module (%) is the operation that gives the remainder of a division of two integer values, • Example: • Relational operators ( ==, !=, >, <, >=, <= ) • The result of a relational operation is a Boolean value that can only be true(1) or false(0). 19 int a=11%3; //a=2 cout<<a; int a=(11<3); //will give us 0 as false cout<<a<<“n”; int b=(11>3); //will give us 1 as true cout<<b;
  • 20. Operators: Logic operators • Logic operators ( !, &&, || ). They correspond with Boolean logic operations NOT, AND and OR Respectively • Examples: 20 First Operand a Second Operand b result a && b result a || b true true true true true false false true false true false true false false false false int a( (5 == 5) && (3 > 6) ); // returns false ( true && false ). cout<<a<<”n”; int b( (5 == 6) || (3 != 6)); // returns true ( true || false ). cout<<b;
  • 21. Operators • Compound assignation operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) • Increase(++) and decrease (--) • They increase or reduce by 1 the value stored in a variable. • They are equivalent to +=1 and to -=1, respectively. • They can be used both as a prefix or (++a) as a suffix(a++). 21 value += increase; // is equivalent to value=value + increase; a -= 5; // is equivalent to a=a-5; a /= b; // is equivalent to a=a/b; price *= units + 1; // is equivalent to price = price * (units + 1);
  • 22. Operators • Example 1: equivalent to equivalent to • Example 2: Example 3: 22 a++; a=a+1; a+=1; int B=3; int A=++B; // A is 4, B is 4 cout<<A<<“n”<<B; int B=3; int A=B++; // A is 3, B is 4 cout<<A<<“n”<<B;
  • 23. Operators : Priority of operators Priority Operator Description Associativity 1 () [ ] Left 2 ++ -- increment/decrement Right ~ Complement to one (bitwise) ! unary NOT & * Reference and Dereference (pointers) (type) Type casting + - Unary sign 3 * / % arithmetical operations Left 4 + - arithmetical operations Left 5 < <= > >= Relational operators Left 6 == != Relational operators Left 7 && || Logic operators Left 10 = += -= *= /= %= >>= <<= &= ^= |= Assignation Right 11 , Comma, Separator Left 23
  • 24. Input Statement: (cin) • cin only process the input from the keyboard once the RETURN key has been pressed. • Example 1: • Example 2: equivalent to 24 cin>> variable >>variable .... ; int age; cin >> age; cin>> a >> b; cin>> a; cin>> b;
  • 25. Input Statement: (cin) 25 #include <iostream> using namespace std; /* Created By: Maryam AlMahdi Creation Date: 29 July 2021 Description: .. */ int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << "n"; return 0; } Please enter an integer value: 702 The value you entered is 702 and its double is 1404.
  • 26. Control Statements: Single-Selection statement • It is used to execute an instruction or block of instructions only if a condition is fulfilled. • If the condition is false, statement is ignored (not executed) and the program continues on the next instruction after the conditional structure. • Example 1: Example2: 26 If (condition ) statement if (x == 100) { cout << "x is "; cout << x; } if (x == 100) cout << "x is 100";
  • 27. Control Statements: Double-Selection statement • Example1: Example2: 27 If (condition ) statement1 else statement2 if (x == 100) cout << "x is 100"; else cout << "x is not 100"; if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0";
  • 28. Let’s review what we’ve learned • Write a program that includes the following: x=“Hello” Name= takes input from the user Print the statement Hello + the user name. 28
  • 29. Let’s review what we’ve learned • Solution: #include <iostream> using namespace std; int main() { string x="Hello"; string Name; cout<<"What is your name?"<<"t"; cin>> Name; cout<<x<<"t"<<Name; return 0; } 29 Quick note: In C++ every data type has a specific number of bytes to store as shown in this hypertext link of slide 13.Date Types.
  • 30. Let’s review what we’ve learned • Write a program as showing below: a=2 b=5 c=8 Use if else statement to print “a is less than c”. 30
  • 31. Let’s review what we’ve learned • Solution: #include <iostream> using namespace std; int main() { int a,b,c; a=2; b=5; c=8; if (a<c) cout<<"a is less than c"; else cout<<"a is greater than c"; return 0; } 31
  • 32. References • Information Technology Infrastructure, Dr. Maryam AlOtaibi. • A Complete guide to Programming in C++ y Ulla Kirch-Prinz & Peter Prinz. 32
  • 33. Contact info • Maryam AlMahdi • E-mail: [email protected] • Instagram: @gdgkwt_maryam 33
  • 34. Variables, Constants, Data types: Data Type 34 Name Bytes* Description Range char 1 character or integer 8 bits length. signed: -128 to 127 unsigned: 0 to 255 short 2 integer 16 bits length. signed: -32768 to 32767 unsigned: 0 to 65535 long 4 integer 32 bits length. signed:-2147483648 to 2147483647 unsigned: 0 to 4294967295 int * Integer. Its length traditionally depends on the length of the system's Word type, thus in MSDOS it is 16 bits long, whereas in 32 bit systems (like Windows 9x/2000/NT and systems that work under protected mode in x86 systems) it is 32 bits long (4 bytes). See short, long float 4 floating point number. 3.4e + / - 38 (7 digits) double 8 double precision floating point number. 1.7e + / - 308 (15 digits) Long double 10 long double precision floating point number. 1.2e + / - 4932 (19 digits) bool 1 Boolean value. It can take one of two values: true or false NOTE: this is a type recently added by the ANSI-C++ standard. Not all compilers support it. Consult section bool type for compatibility information. true or false string 8 stores text, such as "Hello World". String values are surrounded by double quotes. This is not a built-in type, but it behaves like one in its most basic usage “text”