Beruflich Dokumente
Kultur Dokumente
Balagurusamy
KeyWords:
Identifiers:
- Identifiers refer to the names of variables, functions, arrays, classes, etc. created by
the programmer.
- They are the fundamental requirement of any language.
- Each language has its own rules for naming these identifiers.
- The following rules are common to both C and C++:
o Only alphabetic characters, digits and underscores are permitted.
o The name cannot start with a digit.
o Uppercase and lowercase letters are distinct.
o A declared keyword cannot be used as a variable name.
Constants:
- Constants refer to fixed values that do not change during the execution of a program.
- Like C, C++ supports several kinds of literal constants.
- They include integers, characters, floating point numbers and strings.
- Literal constant do not have memory locations.
- Examples:
o 123 decimal integer
o 12.34 floating point integer
o 037 octal integer
o 0X2 hexadecimal integer
o "C++" string constant
o 'A' character constant
Data Types:
- Data types or types are used to represent the type of data stored in variables.
- Data type is an instructions or keywords given to compiler for organizing data.
- Both C and C++ compilers support all the built-in (also known as basic or
fundamental or scalar or primitive) data types.
- The basic data types may have several modifiers preceding them to serve the needs of
various situations.
- The modifiers signed, unsigned long, and short may be applied to integer
- The modifiers signed, unsigned can be applied to Char a basic data types.
- However, the modifier long may also be applied to double.
short
Size
long
signed
Signed
unsigned
- Type modifier is used to edit the meaning of the basic data type to yeild a new type.
- We can use qualifiers with int, char & double as follows:
Base
char int float double
Qualifiers
signed × ×
unsigned × ×
short × × ×
long × ×
- Size qualifiers such as “short” and “long” are used to alter/edit the size of built in data
type.
- Sign qualifiers such as “signed” and “unsigned” used to alter the sign of built in data
type.
Variable Declaration:
- Variables can be declared anywhere but must be prior to their usage.
- C++ allows the declaration of a variable anywhere in the scope.This means that a
variable can be declared right at the place of its first use. This makes the program
much easier to write and reduces the errors that may be caused by having to scan back
and forth. It also makes the program easier to understand because the variables are
declared in the context of their use.
- Dynamic initialization of variables is possible in C++.
- Variables declared within a block are local variables and are known only to that
block.
- Variables that are not declared within any block are global variables.
- Global/Outer block variables are accessed within inner block using scope resolution::
operator.
- Syntax:
:: variable
Reference Variable:
- C++ introduces a new kind of variable known as the reference variable.
- A reference variable provides an alias (alternative name) for a previously defined
variable. –
- For example,
- if we make the variable sum a reference to the variable total, then sum and total can
be used interchangeably to represent that variable.
- A reference variable is created as follows:
- Syntax:
data-type & reference-name=variable-name
- Example:
float total =100;
float &sum = total
o total is a float type variable that has already been declared;
o sum is the alternative name declared to represent the variable total.
o Both the variables refer to the same data object in the memory.
o Now, the statements cout<< total; and cout<< sum
o both print the value 100.
o The statement total = total + 10; will change the value of both total and sum to
110.
o Likewise, the assignemnt sum = 0; will change the value of both the variables
to zero.
- A reference variable must be initialized at the time of declaration.
- This establishes the correspondence between the reference and the data object which
it names.
- It is important to note that the initialization of a reference variable is completely
different from assignment to it.
- C++ assigns additional meaning to the symbol &. Here, & is not an address operator.
The notation float & means reference to float.
Symbolic Constants
- The qualifier const is used to create symbolic constants in C++.
- Syntax:
const datatype vaname = value;
- Example:
const float pi = 3.14;
Operators in C++
- C++ has a rich set of operators. All C operators are valid in C++ also.
Increment &
Unary ++, --
Decrement
Conditional Ternary ?:
- In addition, C++ introduces some new operators. We have already seen two such
operators, namely, the insertion operator <<, and the extraction operator >>.
- Other new operators are:
o : : Scope resolution
o ::* Pointer-to-member declaratory
o ->* Pointer-to-member operator
o .* Pointer-to-member operator
o delete Memory release operator
o endl Line feed operator
o new Memory allocation operator
o setw Field width operator
- In addition, C++ also allows us to provide new definitions to some of the built-in
operators. That is, we can give several meanings to an operator, depending upon the
types of arguments used. This process is known as operator overloading.
- new
o the new operator can be used to create object of any type.
o Syntax:
Pointer-variable = new datatype; //normal variable…
Pointer-variable = new datatype[size] //array variable…
o Example:
1. int *p= new int;
*p=25
- delete
Manipulators:
- Manipulators are operators that are used to format the data display.
- The most commonly used manipulators are endl and setw.
o endl
o The endl manipulator, when used in an output statement, causes a linefeed to
be inserted.
o It has the same effect as using the new line character" \ n".
o For example, the statement ......................... ..........................
cout « "m = "<< m << endl « "n = "<< n << endl « "p = “<< p << endl;
would cause three lines of output, one for each variable. If we assume the
values of the variables as 2597, 14, and 175 respectively, the output will
appear as follows: It is important to note that this form is not the ideal output.
It should rather appear under: m = 2597
n = 14
p = 175.
o Setw(w)
o setw manipulator force output to be printed right-justified.
o It is used as follows:
o suppose value of sum=345
The manipulator setw(5) specifies a field width 5 for printing the value
of the variable sum.
This value is right-justified within the field as shown below:
3 4 5
o setprecesion(P)
o We can control the precision of floating point number by using fixed &
setprecision( ) manipulators.
o Example:
cout<<fixed<<setprecision(2)<<average;
- Manipulators are available in <iomanip.h>
#include <iostream.h>
#include <iomanip.h> // for setw
int main( ){
int Basic=950,Allowance=95, Total=1045;
0. :: Left to Right
4. +-
5. << >>
6. <<= >>=
7. == !=
8. &
9. ^
10. |
11. &&
12. ||
Control Structure
C++ Control Constructs/Structure/Statements
Control statements are used to alter (edit) the flow of program execution.
Control statements evaluate the condition (uses relational and/or logical operators) &
then control the flow of execution.
C++ control constructs/statements are as follows.
if
if - is decision making statement.
“if” is keyword used to make a decision to control flow of execution.
Syntax:-
1. ‘if’ with single statement
if(condition)
true_statement;
In above syntax condition is evaluated first, if condition is evaluated as true then
true_statement is getting executed.
Default scope of ‘if’ is single statement that’s why no needed to use curly braces
2. ‘if’ with multiple statement.
if(condition){
true_statement 1;
true_statement 2;
…….
true_statement n;
}
The use of curly braces shows the scope of ‘if’ statement.
if-else
if-else is decision making statement.
else clause is extension to the if clause & contains false part.
“if” & “else” are keyword used to make a decision to control the flow of execution.
false
false_Statement
The use of curly braces shows the increased scope of ‘if’& ‘else’ clause. If condition
is evaluated as true then true_statements are exeuted otherwise false_statements are executed.
Output:-
Nested if-else
It is complex decision making statement. if clause and/or else clause can be nested
one inside another.
In nested if-else, if clause may have if-else and/or its chain. Similarly else clause may
have if-else and/or
its chain or both if & else clause have sub if-else clause/block.
Complex nested if-else (Multiple decision making) statement may cause problem to
maintain program.
Syntax:-
if(condition){
if(condition){
statements;
}
else{
statements;
}
}
else{
if(condition){
statements;
}
else{
statements;
}
}
In above syntax, inner if and/or else clause may have if-else clause in one another. It
may confuse, that’s why be careful when nesting if-else. Nested if-else is nothing but chained
with one another.
Example:- Programming example to show use of nested if-else.
/* program to find largest amongst three numbers */
main( ){
int n1,n2,n3;
cout<< “Enter three numbers\n”;
cin>>n1>>n2>>n3;
if(n1>n2&&n1>n3){
cout<<n1<< “is greater”;
}
else{
if(n2>n3){
cout<<n2<< “is greater”;
}
else{
cout<<n3<< “is greater”;
}
}
}
In above example, else block/clause contains if-else.
Output
else-if Ladder
It is common programming construct used to make multiple decision. Sometime we
may call this as if-else-if ladder.
It is different than that of nested if-else & less confusion than that of nested if-else.
Syntax:-
if(condition){
statements:
}
else if(condition){
statements;
}
else if(condition){
statements;
}
:
:
else{
statements;
}
In above format, condition is evaluated from top to down.
main( ){
int n1,n2,n3,n4,n5;
cout<<“Enter five numbers\n”;
cin>>n1>>n2>>n3>>n4>>n5;
if(n1>n2&&n1>n3&&n1>n4&&n1>n5)
cout<<n1<< “is largest”;
else if(&n2>n3&&n2>n4&&n2>n5)
cout<<n2<< “is largest”;
else if(n3>n4&&n3>n5)
cout<<n3<< “is largest”;
else if(n4>n5)
cout<<n4<< “is largest”;
else
cout<<n5<< “is largest”;
}
Output
Enter five numbers
5 7 3 9 21
21 is largest
switch case
- It is multiple branching statement.
- It checks for equality not condition.
Advantages:-
- Easy to use
- Easy to find out errors(if any) & debug.
- Complexity of program is minimized.
Syntax:
switch(equality_constant or variable or expression){
case constant1:
statement1;
break;
case constant2:
statement2;
break;
:
:
case constant_n:
statement2;
break;
default:
default_statement;
}
In above syntax:
Syntax:-
switch(equality_constant or variable or expression){
case constant1: statement1; break;
case constant2: statement2; break;
:
case constant_n: statement2; break;
default: default_statement;
switch(equality_constant or variable or expression){
case constant1: statement1; break;
case constant2: statement2; break;
:
Case constant_n: statement2; break;
default: default_statement;
}
for
- it is iterative or loop controlled statement.
- for is keyword used as loop control statement.
Syntax:-
for( initialization; condition ; update_expression ){
statements; //body of for
}
Note:- write about initialization, condition & update_expression.
Output
Enter value of n:8
1 2 3 4 5 6 7 8
Output
Enter number to find factorial 5
Factorial of 5 is 120
while
- it is loop control statement or iterative statement.
- while is keyword & used as loop control statement.
Syntax:-
Initlialization;
while(condition){
//while body
update_expression;
}
In above syntax, condition is evaluated first, if it is evaluated as true then body of loop
is executed along with update_expression, again condition will be checked & if it is true then
body of loop is executed again, otherwise execution will be terminated.
/* Read an integer through keyboard. Sort odd and even numbers(digits) by using while
loop. Write a program to perform sum of odd and even numbers(digits) separately and
display the result*/
main( ){
int n,d,esum=0,osum=0;
cout<<“Enter one number to find sum of even and odd digits\n”;
cin>>n;
while(n!=0){
d=n%10;
if(d%2==0) esum+=d;
else osum+=d;
n/=10;
}
cout<<“Sum of even digits is”<<esum << “\n Sum of odd digits is ”<< osum;
}
Output
main( ){
int n,d,rev=0;
cout<<“Enter one number\n”;
cin>>n;
while(n!=0){
d=n%10;
rev=rev*10+d;
n/=10; // Update expression.
}
cout<< “Reverse number is ”<<rev;
}
Output
Enter one number
2357
Reverse number is 7532
do-while
- it is exit controlled loop
- it is loop control statement or iterative statement.
- do & while is keyword & used as loop control statement.
Syntax:-
Initlialization;
do{
//do body
update_expression;
} while(condition);
In above syntax, first loop is executed before checking condition & then here after
condition is evaluated for every iteration, if it is evaluated as true then body of loop is
executed along with update_expression, again condition will be checked & if it is true then
body of loop is executed again, otherwise execution will be terminated.
break
continue
goto
return
exit( )
- Jump control statement transfer the control from one position to another position
in program while execution.
break:-
- ‘break’ is used to exit the execution of any loop.
- ‘break’ is used to stop execution of remaining cases in switch.
Syntax:-
break;
In above syntax, we have only break keyword followed by comma, and we
can write this in switch and/or any loops in C.
Example:- Programming example to show use break.
#include<stdio.h>
main(){
int i; In this example, we have used ‘break’
i=1; keyword to break while loop… and
while(1){ execution of while loop will be terminated
if(i==4) break; whenever condition if(i==4) is evaluated as
cout<<“\t”<<i; true & execution of while will be terminated.
i++;
}
}
Output
1 2 3 4
Continue:-
- ‘continue’ is used to continue the execution of next iteration by skipping
current iteration of any loop.
Syntax:-
continue;
In above syntax, we have only ‘continue’ keyword followed by comma, and
we can write this in any loop.
Output
1 2 3 5
exit( )
- It is a function available in <stdlib.h>
- It causes immediate termination of entire program.
Syntax:-
exit(return_code);
In above syntax, value of return code is return to the calling process, usually the
operating system.
Example:-
exit(0);
- Zero is usually used as a return code to indicate normal program termination.
- Other arguments are used to indicate some sort of errors.
- We can use the Macros EXIT_SUCCESS and EXIT_FAILURE for return
code.
return:-
- It is jump control instruction.
- return is used to return the value to calling place,(function call)
- it is keyword.
Syntax:-
return(expression);
- returning more than one value is impossible.
Example:-
i. return 0;
ii. return (a+b);
iii. return a;
iv. return (3.147*r*r);
goto:-
- It is unconditional branching statement.
Syntax
goto label;
- In above syntax, label is an identifier which identifies the statements to which
is to be passed.
- Control can be passed to any part of the current function in which goto
appears.
Write a program that will calculate the sum of every third integer beginning with i=2
and for all values of i that are less than 100 using i. for iii while iii. Do
while loop.
Write a program to generate and print the first N terms of Fibonacci series 1,2,3,5,….up
to N terms.
main( ){
int n,F,F1=0,F2=1,i=1;
Cout<<”Enter No\n”;
Cin>>n;
Cout<<"First %d of Fibonacci series is \n"<<n;
while(i<=n){
if(i<=1){
F=i;
}
else{
F = F2 + F1;
F1=F2;
F2=F;
}
Cout<<F;
i++;
}
}
Output:
Enter n
10
First 10 of Fibonacci series is
1
1
2
3
5
8
13
21
34
55
Write a C++ program to check whether given number is prime number or not.
main( ){
int i=2,n,flag=0;
cout"Enter Number\n";
cin>>n;
while(i<n) {
if(n%i==0){
flag=1;
break;
}
i++;
}
if(flag==0)
cout<<"Number is Prime";
else
cout<<"Not Prime No";
}