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

First Chap C++ An Idex of C++

The document discusses the history and uses of C++ programming language. It was developed in the 1970s and was influenced by the C language. It is commonly used to create applications, games, drivers and other software. The document also covers basic C++ concepts like data types, variables, operators, input/output and control flow statements.

Uploaded by

momandayaz306
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)
11 views

First Chap C++ An Idex of C++

The document discusses the history and uses of C++ programming language. It was developed in the 1970s and was influenced by the C language. It is commonly used to create applications, games, drivers and other software. The document also covers basic C++ concepts like data types, variables, operators, input/output and control flow statements.

Uploaded by

momandayaz306
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/ 40

Programming in C/C++

By Sultan
History
• C is a programming Language which is developed during 1970
by Dennis Ritchie . In the case of this language the data was
not secured.
• C Language is not an object oriented language it is a oriented
language. Now what is Object Oriented Language it will be
discuss later.
• In C programming, a program can be divided into smaller
programs called functions. And it doesn't support Function
overloading.
• C++ is also called as C with classes or we can say C++ is a C
language with more features.
History
• C++ is a middle-level programming language developed by BJARNE
STROUSTRUP starting in 1979 at Bell Labs. originally named C with Classes
but later it was renamed C++ in 1983.
• C++ runs on a variety of platforms, such as Windows, Mac OS, and the various
versions of UNIX

Creator of C++ Language


Use of C++
• C++ is being highly used to create computer programs like device drivers
Anything from art applications, music players and even video games. And etc.
• Programming, like many skills, takes time. This is something many people
Compiler and IDE for C++
What is compiler?
• Computers understand only one language and that language consists of
sets of instructions made of ones and zeros. This computer language is
called machine language.
• A single instruction to a computer could look like this: 00011101010101

If we want to give instruction to the computer for example 2+2=4 so


compiler convert our instruction to something shown bellow.
0010 + 0010 = 0100
What is IDE ?
• IDEs are some tools which are use to write code for C++ for example
DEV C++, Turbo C++, Visual Studio, Eclipse and etc.
• The Dev C++ is much famous for learning C++ that is why we use the Dev-C++
IDE (Integrated Development Environment)
• Dev-C++ has its own compiler which is responsible for the conversion of your
code into machine language.
First Program In C++

This is a preprocessor directive. It tells the


preprocessor to include the contents of
iostream header file in the program before
compilation. This file is required for input
output statements.

This will be discuss later. For now it is


required to write it

The main function is responsible to begin the


program, it mean the program start from here,
it include body of your program.
Comments in C++
• Each and every language will provide this great feature which is used to document
source code. We can create more readable and eye catching program structure
using comments. We should use as many as comments in C++ program.
Comment is non executable Statement in the C++.
• We can have two types of comment in Programming
Single Line Comment
Multiple Line Comment
Comments
Example
Data Types & Variables
• While doing programming, you need to use variables to store information.
Variables are nothing but reserved memory locations to store values. This means
that when you create a variable you reserve some space in memory.
• You may like to store information of various data types like character, wide
character, integer, floating point, double floating point, Boolean etc. Based on
the data type of a variable, the operating system allocates memory and decides
what can be stored in the reserved memory.
Data Types & Variables
• Data means characteristic of a data. Bellow are some of the data types in C++,

Type Keyword Size reservation


Boolean bool 1 byte
Character char 1 byte
Short integer short int 2 bytes
Integer int 4 bytes
Floating point float 4 byte s
Double floating point double 8 byte s
Variable and Assignment
Assignment is a process through which we store some values to a variables.
Assigning value to a variable need two process
1. Variable Declaration
2. Variable initialization
What is variable Declaration?
Variable Declaration tell the system you have allocate a space by a specific
name.
To declare a variable you need two things. Data Type and variable name
(identifier).
For example int var1; char var2; double x; string y;
Variable and Assignment
• What is variable initialization?
• Variable initialization means assigning values to variable, it is the way how you
can insert values to the variable. For example
• int x; //variable declaration
• x=90; // variable initialization
• Or
• int x=80; //variable declaration + variable initialization
Examples

We can declare and initialize a variable through different ways

One Variable You can declare


and initialize
You can declare in multiple
same line multiple
More then one Variable variables variable in same
line.
Keywords
• Keywords are some reserve words in C++ programming which are already taken
by the certain language and you can not assign the reserved word to any variable,
function, class and etc.
• Exapmle:
• Int, cin, cout, iostream, double, string, include and etc.
But you can assign the name of variable as
Abc, khan, sname, password, username and etc
Constant
• Constant are like variable, but once you assign a value to a content variable it is
not changeable. Where the variables are changing their values during the
execution of a program.
• Example
• const int x=90;
• x=80; // this is an error
• cout<< x;
Characters
Character is a single alphabet which takes only one byte in computer
memory and it is always define as bellow.
Syntax char variable_name;
Example char x;
x=‘Z’;
cout<<x;
Strings
String is the collection of characters, it is also called non-primitive data
type and it assume storage size depends on the character of string.
Syntax string variable_name;
Example string student_name;
student_name=“qarar khan”;
cout<<student_name;
cin/cout

cin and cout are not keywords, as you are beginner


now in this class just consider them as a keyword.
cout means console out. It is use to print
something on the screen of your system. We use
the extraction operator(<<) with cout. Reverse cin
is use for input when you want to take something
from the user through keyboard. We use the
insertion operator (>>) with cin .
Example
Operator
Operator is a special symbol that tells the compiler to perform specific
mathematical or logical Operation.

These are the overall C++


Operators, we are going to
discuss all of them one by
one with the practical
examples.
For now in this class we
covers the Arithmetic,
Unary and Relational
Operators. The others will
be debussed later
Arithmetic operator program

To understand the arithmetical


operators look at this program.
For more example see the video
of this slide..
Increment & Decrement
Operators

The increment operator (++) adds 1 to its operand, and the decrement
operator (--)subtracts 1 from its operand.
x = x+1; is the same as x++
x = x-1; is the same as x--
Both the increment and decrement operators can either Prefix or Postfix.
There is an important difference in prefix and postfix forms which will be
discuss later

++x // Prefix increment form x++ // Postfix increment form


Chapter 2
Data Flow Statements
if, if...else & nested if

The if, if...else and nested if...else statement are used to make one-time
decisions in C++ Programming, use to execute some codes and ignore some
codes depending upon the test condition.
if Statement
The if statement checks
whether the test condition is
true or not. If the test
condition is true, it executes
the code/s inside the body of
if statement. But it the test The if keyword is followed by test condition inside parenthesis
( ). If the test condition is true, the codes inside curly bracket
condition is false, it skips is executed but if test condition is false, the codes inside curly
the code/s inside the body of bracket { } is skipped and control of program goes just below
if statement. the body of if as shown in figure above
Flow Chart of if Statement
Else if Statement
• This statement is executed when if
statements become fail. So the compiler
checks for the else if statement. Same if
the first else if statement become fail
again it will check for the third else if
statement, same action will be perform. If
none of the if and else if statement
become true then the last bock (else
block) will be execute.
Else if flow Chart
Nested if Statement

• When ever an if statement


comes inside of an if statement,
it is called Nested if statement.
It means this has two test
condition for execution of a
program.
• See the example
Nested if Flow Chart
Loops

• When you need to execute a block of code several Body of loop


number of times we use some statements which
are called loops statement. In fact loop statement
allows you to execute a statement or a group of
statement multiple time. True
Condition
• following is the general form of a loop statement
in most of the programming language.
False

Exit from loop


Types of Loop
C++ provide the following types of Loop statement.
1. While loop
2. For loop
3. Do while loop
4. Nested loop
While loop
Flow Chart

• A while loop statement repeatedly


executes a target statement as
long as a given condition is true
• Syntax:

While (condition ){
Statements
}
Program
Flow Chart
For Loop
• A for loop is a repetition control structure
that allows you to efficiently write a loop
that needs to execute a specific number of
time.
• Syntax:

for(initialization ; condition ; increment){


Statements;
}
Program
Do… While loop
Flow Chart

As we studied while and for loops, they were


checking for condition first and then execute
the statement. But do…while loop execute
the statement and then it check the condition
for further execution.
Syntax:

do{
Statements;
}while (condition)
Program
Switch Statement
Programming 2

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