0% found this document useful (0 votes)
22 views25 pages

Code Blocks

The document provides an introduction to learning the C++ programming language. It covers getting started with C++, the syntax of a basic C++ program, and key concepts like variables, data types, operators, and strings. C++ is described as an object-oriented, cross-platform language used to create high-performance applications. The document gives examples of printing output, using comments, declaring variables of different data types, and taking user input.

Uploaded by

Mutaawe MMark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views25 pages

Code Blocks

The document provides an introduction to learning the C++ programming language. It covers getting started with C++, the syntax of a basic C++ program, and key concepts like variables, data types, operators, and strings. C++ is described as an object-oriented, cross-platform language used to create high-performance applications. The document gives examples of printing output, using comments, declaring variables of different data types, and taking user input.

Uploaded by

Mutaawe MMark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

CODE BLOCKS

LEARNING C++

Email: [email protected] Tel: +256 702 424 794/780 440 875


Getting started
• C++ is a popular language
• C++ is used to create computer programs
• C++ is an object oriented language.
• Example:
• 1.#include <iostream>
• 2. using namespace std;
• 3. int main() {
• 4._______ <<“Hello world!”;
• 5.return 0;
• 6. }
Email: [email protected] Tel: +256 702 424 794/780 440 875
Introduction
• What is C++?
• C++ is a cross-platform language that can be used to create high-performance applications.
• Why use is C++?
• C++ is one of the world's most popular programming languages.
• C++ can be found in today's operating systems, Graphical User Interfaces, and embedded
systems.
• C++ is an object-oriented programming language which gives a clear structure to programs and
allows code to be reused, lowering development costs.
• C++ is portable and can be used to develop applications that can be adapted to multiple
platforms.
• C++ is fun and easy to learn!
• As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice versa

Email: [email protected] Tel: +256 702 424 794/780 440 875


Syntax
• #include <iostream>
• Is a header file library that lets us work with input and output objects
• Using namespace std (std::)
• Means we can use names for objects and variables from the standard
library
• Int main(): This is called a function
• Cout: is an object used together with an insertion operator (<<)
• Return 0: ends the main function

Email: [email protected] Tel: +256 702 424 794/780 440 875


Output
• The cout object, together with the << operator is used to output
values/print text.
• #include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
• Note that this doesn’t insert a new line.
Email: [email protected] Tel: +256 702 424 794/780 440 875
New Lines
• To insert a new line, you can use \n or endl
• #include <iostream>
using namespace std;

int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
• Two \n characters after each other will create a
blank line
Email: [email protected] Tel: +256 702 424 794/780 440 875
New Lines
• Another way to insert new line is with the endl manipulator.
• #include <iostream>
using namespace std;

int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}

Email: [email protected] Tel: +256 702 424 794/780 440 875


Comments
Comments are used to explain code and make it more readable.
They can also be used to prevent execution when testing alternative code
Comments can be single lined or multi-lined

// single line comment /* and */ Block comment

• // This is a comment • /* The code below will print


cout << "Hello World!"; the words Hello World!
to the screen, and it is
amazing */
cout << "Hello World!";

Email: [email protected] Tel: +256 702 424 794/780 440 875


Variables
• Variables are containers storing data values
• Declaring variables
• The equal sign is used to assign values to a variable
• type variable =value;
• int myNum = 15; int myNum;
cout << myNum; or myNum = 15;
cout << myNum
• Note: If you assign a new value to an existing variable, it will overwrite the previous value
• int myAge = 35;
cout << "I am " << myAge << " years old.“;
• Write a code adding two variables and outputting the sum

Email: [email protected] Tel: +256 702 424 794/780 440 875


Variables
• Declare multiple variables
• To declare more than one variable of the same type, use a comma-
separated list
• int x = 5, y = 6, z = 50;
cout << x + y + z;

Email: [email protected] Tel: +256 702 424 794/780 440 875


Variables
• Identifiers
• All code variables must be identified with unique name called
identifiers.
• Identifiers can be short names or more descriptive names (age, sum)
• // Good
int minutesPerHour = 60;

/* OK, but not so easy to understand


what m actually is*/
int m = 60;
Email: [email protected] Tel: +256 702 424 794/780 440 875
Variables
• Constants
• Use const keyword, this will declare the variable as constant
• Eample 1
• const int myNum = 15; // myNum will always be 15

myNum = 10; // error: assignment of read-only
variable 'myNum’
• Example 2.
• const int minutesPerHour = 60;
const float PI = 3.14;
Email: [email protected] Tel: +256 702 424 794/780 440 875
User Input
• We use cin (see-in) to get user input
• Cin is a predefined variable that reads data from the keyboard with
the extraction operator (>>)
• int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value

Email: [email protected] Tel: +256 702 424 794/780 440 875


Data Types
• A variable must be a specified data type
• int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String

Email: [email protected] Tel: +256 702 424 794/780 440 875


Data Types
Basic data Types
Data Type Size Description
Int 4 bytes Stores whole numbers, without decimals
Float 4bytes Stores fractional numbers, containing one or more decimals.
Sufficient for storing 7 decimal digits
Double 8bytes Stores fractional numbers, containing one or more decimals.
Sufficient for storing 15 decimal digits
Boolean 1 byte Stores true or false values
Char 1 byte Stores a single character/letter/number, or ASCII values

Email: [email protected] Tel: +256 702 424 794/780 440 875


Data types
Numbers
• Int
• Float
• Double
• Int i1=1055
• float f1 = 35e3;
double d1 = 12E4;
cout << f1;
cout << d1;
• cout <<i1;

Email: [email protected] Tel: +256 702 424 794/780 440 875


Data types
Boolean

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.
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)

Email: [email protected] Tel: +256 702 424 794/780 440 875


Data Types
Character
• The Char data type is used to store a single character.
• ‘A’, ‘B’, ‘C’

• char a = 65, b = 66, c = 67;


cout << a;
cout << b;
cout << c;

Email: [email protected] Tel: +256 702 424 794/780 440 875


Data types
String
• The string type is used to store a sequence of characters (Text)
To use strings, you should add an additional header file in the source
code (the <string> library)
string greeting = "Hello";
cout << greeting;

Email: [email protected] Tel: +256 702 424 794/780 440 875


Operators
Arithmetic Assignment
Operator Name Operator Example Same as
Addition = X=5 X=5
Subtraction += X+=3 X=x+3
Multiplication -=
Division *=
Modulus /=
Increment %=
Decrement &=
|=
^=
>>=
<<=
Email: [email protected] Tel: +256 702 424 794/780 440 875
Operators Logical
Logical operators are used to determine the
Comparison logic between variables or values

Operator Name Example Operator Name Example


== Equal to X==y && Logical AND X<5&&X<10
!= Not equal to X!=y || Logical OR X<5||X<4
> Greater than
! Logical NOT !(X<5&&X<10)
< Less than
>=
<=

Email: [email protected] Tel: +256 702 424 794/780 440 875


Strings
• Strings are used for storing text
• A string variable contains a collection of characters surrounded by
double quotes.
• Example: String greeting=“Hello”;
• To use strings, you must include an additional header file in the
source code. #include <string>

Email: [email protected] Tel: +256 702 424 794/780 440 875


String Concatenation

• Example 1 • Example 2
• String firstName =“Mark”; • String firstName =“Mark”;
• String lastName =“Mutaawe”; • String lastName =“Mutaawe”;
• String fullName= firstName+ “ ”+lastName; • String
fullName=firstName.append(lastName);
• Cout << fullName;
• Cout << fullName;
WARNING!
C++ uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.

Email: [email protected] Tel: +256 702 424 794/780 440 875


User Inpur Strings
• String firstName;
• cout<< “Type your first name: ”;
• Cin>>firstName;
• Cout<<“Your name is: “<<firstName;
• //Type your fist name: Mark
• //Your name is: Mark

Email: [email protected] Tel: +256 702 424 794/780 440 875


• String fullName;
• cout<< “Type your full name: ”;
• getline (Cin, fullName);
• Cout<<“Your name is: “<<fullName;
• //Type your fist name: Mark Mutaawe
• //Your name is: Mark Mutaawe

Email: [email protected] Tel: +256 702 424 794/780 440 875

You might also like