100% found this document useful (1 vote)
43 views31 pages

Chapter 1c++

Uploaded by

diribatolasa50
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
100% found this document useful (1 vote)
43 views31 pages

Chapter 1c++

Uploaded by

diribatolasa50
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/ 31

CHAPTER 1

Basics of C++ Programming


Outlines:
 Structure of C++ program
 Concepts of Algorithms
 Variables and Data types
 Basic Elements of C++ program
 Operators and Statements
Structure of a C++ program

 A C++ program is structured in a specific and particular manner.

 In C++, a program is divided into the following three sections:

i. Standard Libraries Section

ii. Main Function Section

iii. Function Body Section


.
Cont.…
1. Header files contain the definition of the functions and macros we are
using in our program.
 #include is a preprocessor directive using which we import header files.

 Syntax: #include <library_name>

2. Namespace in C++ is used to provide a scope or a region where we


define identifiers.
 It is used to avoid name conflicts between two identifiers as only unique
names can be used as identifiers. Syntax: using namespace std;
Cont.…
3. main function is the most important part of any C++ program.
 The program execution always starts from the main function. All the
other functions are called from the main function.
 Syntax: int main(){

return 0;

}
Cont.…
4. Blocks are the group of statements that are enclosed within { } braces.
 They define the scope of the identifiers and are generally used to enclose the
body of functions and control statements.
 Syntax: {

return 0;}

5. Semicolon each statement in the above code is followed by a ( ; ) semicolon


symbol

Syntax: any statement;


C++ Variables
 variable is a container (storage area) to hold data.
 To indicate the storage area, each variable should be given a unique name
(identifier). For example, int age = 25;
 Here, age is a variable of the int data type, and we have assigned an integer
value 25 to it.
 Note: The int data type suggests that the variable can only hold integers.
Similarly, we can use the double data type if we have to store decimals and
exponentials.
Cont..
• A demonstration of other data types:

• Example

• 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)
Cont..
// operating with variables b = 2;

#include <iostream> a = a + 1;

using namspace std; result = a - b;

int main () // print out the result:

{ cout << resut;

// declaring variables: // terminate the program:

int a, b; return 0;

int result; // process: }

a = 9;
Identifiers
• It is a name given to any program element, the elements might be
variable, function, array, structure or class.

• Rules : Only letters, digits and underlined characters are valid.

• Identifiers should always begin with a letter or an underscore.

• key words should not be used as names for identifiers .

• C++ is case sensitive


Cont..
All C++ variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive


names (age, sum, total Volume).

Note: It is recommended to use descriptive names in order to create


understandable and maintainable code:
Data Type
 It is a name given to a set of values which have common property.
 data types are declarations for variables.
 This determines the type and size of data associated with variables.
Cont..
// C++ Program to Demonstrate the correct cout << sizeof(int) << endl;
size

// of various data types on your computer. cout << sizeof(long) << endl;
#include <iostream> cout << sizeof(float) << endl;
using namespace std; cout << sizeof(double) << endl;
int main() return 0;
{ }
cout << sizeof(char) << endl;
Constant C++

 A constant is any expression that has a fixed value.

 Constant C++ provide two types

I. Literal constant : is a value typed directly into the program wherever it is


needed

E.g. Int x=22; 22 is a literal constant in this statement:

II. Symbolic constant: is a constant that is represented by a name , similar to that


of a variable

In C++ symbolic constant is declared two ways .These are using the const
keyword and #define
Literal constant
#include<iostream>
using namespace std;
int main(){
float x=3.5;
float y=7.8, sum;
x=9.5;
sum=x+y;
cout<<"The sum of x and y is\t"<<sum<<endl;
return 0;
}
Define constant
#include<iostream>

using namespace std;

#define x 9.5

int main(){

float y=7.8, sum;

sum=x+y;

cout<<"The sum of x and y is\t"<<sum<<endl;

return 0;

}
Const constant
#include<iostream>
using namespace std;
int main(){
const float x=10;
float y=9,sum;
x=20;
sum=x+y;
cout<<"The sum of x and y is\
t"<<sum<<endl;
return 0;
}
Assignment Operator (=)
 Causes the operand on the left side of the assignment statement to
have its value changed to the value on the right side of the statement
variable
 Syntax: operand1=operand2;

 Operand 1 is always a variable

 Operand 2 can be one or more combination of

 A literal constant : eg x=33;


Cont..
#include<iostream>
using namespace std;
int main(){
int a=8, b=3;
a=b;
cout<<a<<endl;
return 0;
}
Arithmetic operator(+,*,%,/,-,)
• Arithmetic operators are used to perform common mathematical
operations.
Cont..
#include <iostream> • // Modulo operator
using namespace std; • cout << (x % y) << endl;
int main() •
{ • // Division operator
int x = 10, y = 5; • cout << (x // y) << endl;
// Addition operator
cout<<(x + y) << endl; • return 0;
// Multiplication operator •}
cout << (x * y) << endl;
// Subtraction operator
cout (x - y) << endl;
Relational Operators(==, <, <= ,> ,>=, !=)

 These operators are used for the comparison of the values of two
operands. For example, ‘>’ checks if one operand is greater than the
other operand or not, etc.
 The result returns a Boolean value, i.e., true or false.
Cont..
Cont..
#include <iostream> cout << (x < y) << endl;
using namespace std; // Equal to operator
cout << (x == y) << endl;
int main() // Greater than or Equal to operator
{ cout << (x >>= y) << endl;
int x = 8, y = 2;
// Greater than operator // Lesser than or Equal to operator
cout<< (x > y) << endl; cout << (x <= y) << endl;

// not return 0;
cout << (x != y) << endl; }

// Lesser than operator


Logical Operators(&&, ||, !)
These operators are used to combine two or more conditions or constraints or
to complement the evaluation of the original condition in consideration. The
result returns a Boolean value, i.e., true or false.
 Logical AND (&&) produces 0 if one or both of its operands evaluate to 0
otherwise, it produce 1
 Logical OR(||) produce 0 if both of its operands evaluate to 0 otherwise , it
produce 1.
E.g. ((22==22)&&(8>10)) // evaluates to false (true&&false).

 ((22==22)||(8>10)) // evaluates to true (true||false).


Cont..
cont..
#include<iostream> // Logical NOT operator
using namespace std; cout << "!x is " << (!x) << endl;
int main(){
int x = 8, y = 3; return 0;
// Logical AND operator }
cout << "x && y is " << (x && y)
<< endl;
// Logical OR operator
cout << "x || y is " << (x || y) <<
endl;
Compound Assignment operators(+=,-=,/=,%=,*=)
 The combination of the assignment operators with other operators like
arithmetic and bit wise operators.
 The assignment operator has a number of variant , obtained by
combining it with other operators
 E.g. x+=8; is equivalent to x=a+8;
Cont..
#include<iostream>
using namespace std;
int main(){
int sum=4,pro=5,dif=2,quo=6,rem=3;
sum +=4;
pro *= 5;
dif -= 1;
rem %= 2;
quo /= 2;
cout<<sum<<endl<<pro<<endl<<rem<<endl<<dif<<endl<<quo<<endl;
return 0;
}
Reading Assignment
 Increment and decrement operator
 Datatype modifier
 C++ Control Statements
 Types of errors in C++

You might also like