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

m3 Ppts

Uploaded by

Jumy jaimy
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)
6 views

m3 Ppts

Uploaded by

Jumy jaimy
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/ 61

Introduction to C++

 A computer is an electronic machine that accepts data


from the user, processes the data, and generates the
desired output.

 Programs are a sequence of instructions or statements.


Structure of C++ program
The structure of C++ program is divided into four different sections:
//This is a comment and is ignored by compiler

#include <iostream.h>//Header file

void main() //main() function. Execution starts here


{
cout<<"First c++ program"; //body of main() function
}
 Each language also has its own syntax, which is a set of rules

that must be strictly followed when writing a program.

 comments are used for the documentation to understand the

code to others. These comments are ignored by the compiler.

 The individual instructions that you use to write a program in

a high-level programming language are called statements.


Key Words

 Keywords are special reserved words and each


keyword has a special meaning and a specific
operation.

 Example: cout, void, main ,include


Identifiers
Identifier is a name used to identify a variable, function,
class, module or other object.

The rules for the formation of an identifier are:

 An identifier can consist of alphabets, digits and


underscores.
 It must not start with a digit
 C++ is case sensitive that is upper case and lower case
letters are considered different from each other.
 It should not be a keyword.
Character set
Character Set means that the characters and symbols that a
C++ Program can understand and accept.

There are mainly four categories of the character set

1. Alphabets: A-Z or a-z.


2. Digits: 0-9 or by combination of these digits.
3. Special Symbols: All the keyboard keys except alphabet
and digits are the special symbols. +, -, * , >, <=, >=, ==
&&, II etc.
4. White Spaces
Data type

 A data type specifies the type of data that a variable can


store such as integer, floating, character etc.

Ex:
integers (type int),
floating-point numbers (type float), and
character(type char).
character for character storage (1 byte). Rrepresent single characters
Example:
char a = 'A'; // char type

integer for integral number (2 bytes)


represent whole numbers and their negatives
Example:
int a = 1; // int type
float single precision floating point (4 bytes)
represent real numbers with a decimal point
Example:
float a = 3.14159; // floating point type
double double precision floating point numbers (8 bytes)
Example:
double a = 6e-4;// double type (e is for exponential)
Boolean declared as bool. has only 2 values true/false
Example:
bool b = true;

void null or empty set of values


Variables
 A name that refers to a value.

Declaring Variables

Int a;

Assigning value to a Variable

 You can use the assignment operator = to assign the value to a


variable.
a=10;
Declaring and assigning a value to a variable

Assignment statement

A statement that assigns a value to a variable.

int a=10;

char a=‘b’;

float a =2.5;
Expressions
An expression is a combination of values, variables,
and operators.

If you type an expression on the command line, the


interpreter evaluates it and displays the result:

C=a+2;
Operators and operands

Operator: A special symbol that perform various


operations on data.

The values the operator uses are called operands.


OPERATORS
1) Basic Arithmetic

Basic arithmetic operators are: +, -, *, /, %

+ is for addition.
– is for subtraction.
* is for multiplication.
/ is for division.
% is for modulo.

Note: Modulo operator returns remainder, for example


20 % 5 would return 0
Example of Arithmetic Operators

#include <iostream.h>
voidmain()
{
int num1 = 240;
int num2 = 40;
cout<<"num1 + num2: "<<(num1 + num2)<<endl;
cout<<"num1 - num2: "<<(num1 - num2)<<endl;
cout<<"num1 * num2: "<<(num1 * num2)<<endl;
cout<<"num1 / num2: "<<(num1 / num2)<<endl;
cout<<"num1 % num2: "<<(num1 % num2)<<endl;
return 0;
}

Output:

num1 + num2: 280


num1 - num2: 200
num1 * num2: 9600
num1 / num2: 6
num1 % num2: 0
2) Assignment Operators
Assignments operators in C++ are: =, +=, -=, *=, /=, %=

num2 = num1 would assign value of variable num1 to the variable.


num2+=num1 is equal to num2 = num2+num1
num2-=num1 is equal to num2 = num2-num1
num2*=num1 is equal to num2 = num2*num1
num2/=num1 is equal to num2 = num2/num1
num2%=num1 is equal to num2 = num2%num1
Example of Assignment Operators

#include <iostream.h>
void main()
{
int num1 = 240;
int num2 = 40;
num2 = num1;
cout<<"= Output: "<<num2<<endl;
num2 += num1;
cout<<"+= Output: "<<num2<<endl;
num2 -= num1;
cout<<"-= Output: "<<num2<<endl;
num2 *= num1;
Output:
cout<<"*= Output: "<<num2<<endl;
num2 /= num1;
= Output: 240
cout<<"/= Output: "<<num2<<endl;
+= Output: 480
num2 %= num1;
-= Output: 240
cout<<"%= Output: "<<num2<<endl;
*= Output: 57600
}
/= Output: 240
%= Output: 0
3) Increment and decrement Operators

++ and —

num++ is equivalent to num=num+1;

num-- is equivalent to num=num-1;

Example of increment and decrement Operators

#include <iostream.h>
void main()
{
int num1 = 240;
int num2 = 40;
num1++; num2--;
cout<<"num1++ is: "<<num1<<endl;
cout<<"num2-- is: "<<num2; Output:
}
num1++ is: 241
num2-- is: 39
4) Logical Operators

Logical Operators are used with binary variables. They are mainly used in
conditional statements and loops for evaluating a condition.

Logical operators in C++ are: &&, ||, !

Let’s say we have two boolean variables b1 and b2.

b1&&b2 will return true if both b1 and b2 are true else it would return
false.

b1||b2 will return false if both b1 and b2 are false else it would return
true.

!b1 would return the opposite of b1, that means it would be true if b1 is
false and it would return false if b1 is true.
Example of Logical Operators

#include <iostream.h>
void main()
{
bool b1 = true;
bool b2 = false;
cout<<"b1 && b2: "<<(b1&&b2)<<endl;
cout<<"b1 || b2: "<<(b1||b2)<<endl;
cout<<"!(b1 && b2): "<<!(b1&&b2);
}
Output:

b1 && b2: 0
b1 || b2: 1
!(b1 && b2): 1
5) Relational operators

We have six relational operators in C++: ==, !=, >, <, >=, <=

== returns true if both the left side and right side are equal
!= returns true if left side is not equal to the right side of
operator.
> returns true if left side is greater than right.
< returns true if left side is less than right side.
>= returns true if left side is greater than or equal to right side.
<= returns true if left side is less than or equal to right side.
6) Ternary Operator

This operator evaluates a boolean expression and assign the


value based on the result.

Syntax:

variable num1 = (expression) ? value if true : value if false

Example: num2 = (num1 == 10) ? 100: 200;

If the expression results true then the first value before the
colon (:) is assigned to the variable num1 else the second
value is assigned to the num1.
Example of Ternary Operator/ Relational operator

#include <iostream.h>
void main()
{
int num1, num2;
num1 = 99;
num2 = (num1 == 10) ? 100: 200;
cout<<"num2: "<<num2<<endl;
num2 = (num1 == 99) ? 100: 200;
cout<<"num2: "<<num2;
}
Output:

num2: 200
num2: 100
Order of operations in C++
• When more than one operator appears in an expression, the order of evaluation
depends on the rules of precedence.

• C++ follows the same precedence rules for its mathematical operators that
mathematics does.

• Operators with the same precedence are evaluated from left to right.
Enumerated data types
• This is a user-defined data type having a finite set of enumeration
constants.
• The keyword 'enum' is used to create enumerated data type.
• Enumerated type declares a new type-name along with a sequence of
values containing identifiers which has values starting from 0 and
incrementing by 1 every time.

Syntax: enum typeNam{value1, value2,..};


Eg: enum day{mon, tues, wed, thurs, fri};

void main()
{
enum colour {red, blue, green, yellow};
colour background=green;
cout<<background;
}
cout - Output statements
Variable cout is predefined to denote an output
stream that goes to the standard output device
(display screen).

Syntax: cout << data ;


or
cout<< Expression;

Example: cout<<"hello" or cout<<x ;


cin - Input statements
Variable cin is predefined to denote an input stream from
the standard input device (the keyboard)

Syntax: cin >> Variable >> Variable . . . ;

cin >> variable-name; // Meaning: read the value of the


variable called <variable-name> from the user

Example: cin >> x;


cin >> y;
OR
cin >> x >> y;
Selection statements
/conditional statements
• The selection will be based on conditions. The
conditions are formed by relational or logical
expressions.

• C++ provides the following statements for


implementing the selection control structure.

– if statement
– if else statement
– nested if statement
– if-else if statement
– switch statement
if statement

Syntax:

if(condition)
{
//Statements;
}
If Example:
#include <iostream.h>

void main ()
{
int num = 10;
if (num % 2 == 0)
{
cout<<"It is even number";
}
}
Output:
It is even number
If else statement
syntax:

if(condition)
{
//Statements
}
else
{
//Statements
}
If else Example:
#include <iostream.h>

void main ()
{
int num = 11;
if (num % 2 == 0)
{
cout<<"It is even number";
}
else
{
cout<<"It is odd number";
} Output:
}
It is odd number
Nested if statement
Syntax:

if(condition_1)
{
// Statement1;

if(condition_2)
{
// Statement2;
}

}
Nested if Example:
#include <iostream>

void main()
{
int num=90;
if( num < 100 )
{
cout<<"number is less than 100"<<endl;
if(num > 50)
{
cout<<"number is greater than 50";
}
}
Output:
}
number is less than 100
number is greater than 50
If - else if Statement
if-else-if statement is used when we need to check multiple conditions.

Syntax: if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
Else
{
//code to be executed if all the conditions are false
}
#include <iostream.h>

if-else-if void main()


{

Example: int num;


cout<<"Enter an integer number between 10 & 99999: ";
cin>>num;
if(num <100 && num>=10)
{
cout<<"Its a two digit number";
}
else if(num <1000 && num>=100)
{
cout<<"Its a three digit number";
}
else if(num <10000 && num>=1000)
{
cout<<"Its a four digit number";
}
else if(num <100000 && num>=10000)
{
cout<<"Its a five digit number";
}
else
{
cout<<"number is not between 1 & 99999";
}
}
Switch statement
Syntax:
• Switch case statement is
used when we have multiple switch(variable)
conditions and we need to {
perform different action case value1:
based on the condition. //code to be executed;
break;
case value2:
• It evaluates the value of //code to be executed;
variable, then based on the break;
outcome it executes the ......
corresponding case. default:
/*code to be executed if all cases
are not matched;*/
break;
}
Switch statement Example:
#include <iostream.h>
void main ()
{
int num;
cout<<"Enter a number to check case:";
cin>>num;
switch (num)
{
case 10: cout<<"It is 10";
break;
case 20: cout<<"It is 20";
break;
case 30: cout<<"It is 30";
break;
default: cout<<"Not 10, 20 or 30";
break; Output:
Enter a number:
}
10
} It is 10
For Loop
• The C++ for loop is used to iterate a part of the program several
times.
• We can initialize variable, check condition and increment/
decrement value.

Syntax:

for(initialization; condition; incr/decr)


{
//code to be executed
}
#include <iostream>
void main()
{
for(int i=1;i<=10;i++)
{
cout<<i <<"\n";
}
}

Output:
1
2
3
4
5
6
7
8
9
10
While loop
• while loop is used to iterate a part of the program several times.
If the number of iteration is not fixed, it is recommended to use
while loop than for loop.

• It is an entry-controlled loop. The condition is checked first and if


it is True the body of the loop will be executed. That is the body
will be executed as long as the condition is True.

Syntax:

initialize variable;
while(condition)
{
body of the loop;
increment/ decrement variable
}
#include <iostream.h>
void main()
{
int i=1;
while(i<=10)
{
cout<<i <<"\n";
i++;
}
}

Output:
1
2
3
4
5
6
7
8
9
10
Do-While Loop
• do-while loop is used to iterate a part of the program several
times. If the number of iteration is not fixed and you must have to
execute the loop at least once, it is recommended to use do-while
loop.

• It is an exit-controlled loop. do-while loop is executed at least


once because condition is checked after loop body.

Syntax:

initialize variable;
do
{
body of the loop;
increment/ decrement variable
} while(condition);
#include <iostream.h>
void main()
{
int i=1;
do
{
cout<<i <<"\n";
i++;
} while(i<=10) ;
}

Output:

1
2
3
4
5
6
7
8
9
10
Jump statements
break
• The break statement is used to terminate loop or switch
statements. Control of the program flows to the statement
immediately after the body of the loop or switch.

• If break statement is inside a nested loop (loop inside another


loop), break will terminate the innermost loop.

Syntax: break;
#include <iostream.h>
void main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
cout<<i<<"\t";
}
}

Output:
1 2 3 4
continue
The continue statement is used to skip the rest of the code
inside a loop for the current iteration only. Loop does not
terminate but continues on with the next iteration.

Syntax: continue;
#include <iostream.h>
void main()
{
for(int i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
cout<<i<<"\t";
}
}

Output:
1 2 3 4 6 7 8 9 10
goto
The C++ goto statement is also known as jump statement. It is
used to transfer control to the other part of the program. It
unconditionally jumps to the specified label.

Syntax: goto labelName;


labelName: statement;
#include <iostream.h>
void main()
{
int age;
ineligible:
cout<<"Enter your age:\n";
cin>>age;
if (age < 18)
{
cout<<"You are not eligible to vote!\n";
goto ineligible;
}
else Output:
Enter your age:
{ 7
cout<<"You are eligible to vote!"; You are not eligible to vote!
} Enter your age:
22
} You are eligible to vote!
exit ()
• exit() is a standard library function, which causes
immediate termination of the entire program when it is
called.

• stdlib.h needs to be included in order to use exit().

Syntax: exit(0);
#include <iostream.h>
#include<stdlib.h>
void main()
{
char choice;
cout<<"Do you want to study C++?(y/n)\n";
cin>>choice;
if (choice=='n')
{
exit(0);
}
else
{
cout<<"Welcome to c++!";
}
} Output:
Do you want to study C++?(y/n)
y
Welcome to c++!

You might also like