0% found this document useful (0 votes)
15 views

Lecture 3 Introduction to C++

This document serves as an introduction to C++, highlighting its popularity and versatility as an object-oriented programming language. It covers basic concepts such as writing a simple program, using variables, data types, constants, user input, and operators, along with examples. The document also includes guidelines for naming variables and demonstrates a simple calculator program.

Uploaded by

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

Lecture 3 Introduction to C++

This document serves as an introduction to C++, highlighting its popularity and versatility as an object-oriented programming language. It covers basic concepts such as writing a simple program, using variables, data types, constants, user input, and operators, along with examples. The document also includes guidelines for naming variables and demonstrates a simple calculator program.

Uploaded by

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

1

Computer Programming
CHAPTER 1
INTRODUCTION TO C++
2
Why Use 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!
3
First Program in C++

 Prints a line of text:


#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
4
First Program in C++ (cont.)

 #include <iostream>
is a header file library that lets us work with input and output objects, such as
cout
 using namespace std;
means that we can use names for objects and variables from the standard
library.
 int main()
The main function where Any code inside its curly brackets { } will be
executed.
5
First Program in C++ (cont.)

 cout << "Hello World!";


is used to output values/print text on the output screen.
6
Prints a multi line of text

 To insert a new line, you can use the \n


#include <iostream>
using namespace std;
int main()
{
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
7
Prints a multi line of text (cont.)

 Another way to insert a 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;
}
8
Escape Sequence

Examples of other valid escape sequences are:

Description

\t Creates a horizontal tab

\\ Inserts a backslash character (\)

\” Inserts a backslash character (“)

\’ Inserts a backslash character (‘)


9
Comments

 The comments are used by C++ to explain the program


 The comments are ignored by the compiler
 Single-line Comments
Single-line comments start with two forward slashes (//)
// This is a comment
cout << "Hello World!";
Or
cout << "Hello World!"; // This is a comment
10
Comments (cont.)

 C++ Multi-line Comments


Multi-line comments start with /* and ends with */
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";
11
Variables

 Variables are containers for storing data values.


 In C++, there are different types of variables defined by the following keywords:
 int: stores integer value, without decimals, such as 175 or -175
 float: stores floating point numbers, with decimals, such as 19.99 or -19.99
 char: stores single characters, such as 'a' or 'B'. Char values are surrounded by single
quotes
 string: stores text, such as "Hello World". String values are surrounded by double quotes
 bool: stores values with two states: true or false
12
Basic data types

The data type specifies the size and type of information the variable will store:
13
Declaring Variables

 To create a variable, specify the type and assign it a value:


type variableName = value;
Example:
int myNum = 15;
cout << myNum;
Or:
int myNum;
myNum = 15;
cout << myNum;
14
Declaring Variables (cont.)

 The variable could be assigned to another value:


int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum; // Outputs 10
15
Other Types

int myNum = 5; // Integer (whole number without decimals)


double myFloatNum = 5.99; // Floating point number (with decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
16
Simple program

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 6;
int sum = x + y;
cout <<“ the sum = “<< sum;
}
17
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;
18
Rules for naming variables

 All C++ variables must be identified with unique names.


 These unique names are called identifiers.
 Names can contain letters, digits and underscores
 Names are case-sensitive (Num and num are different variables)
 Names cannot contain whitespaces or special characters like !, #, %, etc.
 Reserved words (like C++ keywords, such as int) cannot be used as names
19
Constants

 Constant means unchangeable read-only identifier:


const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable 'myNum‘

 You should always declare the variable as constant when you have values that are
unlikely to change
20
User Input

 You have already learned that cout is used to output (print) values. Now we will
use cin to get user input.
 cin is a predefined variable that reads data from the keyboard with the
extraction operator >>
 In the following example, the user can input a number, which is stored in the
variable x Then we print the value of:
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
}
21
Operators

 divides the operators into the following groups:


Arithmetic Operator
Assignment Operators
comparison Operators
Logical Operators
22
Arithmetic Operators
23
Arithmetic Operators Examples

int sum1 = 100 + 50; // 150 (100 + 50)


int sum2 = sum1 * 2; // 300 (150 * 2)
int sum3 = sum2 / 10; // 30 (300 / 10)
cout<<" sum1 = " <<sum1<<endl;
cout<<" sum2 = " <<sum2<<endl;
cout<<" sum3 = " <<sum3<<endl;
24
Assignment Operators
25
Comparison Operators
26
Logical Operators
27
Simple Calculator Program

#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
28
Simple Calculator Program

cout << "Sum of the two numbers is: " << x + y <<endl;
cout << "Subtraction of the two numbers is: is: " << x - y <<endl;
cout << "product of the two numbers is: " << x * y <<endl;
cout << "Division of the two numbers is: " << x / y <<endl;
}
29
Program Output Screen
30
End

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy