Sie sind auf Seite 1von 30

Reference: - Object Oriented Programming With C++ by E.

Balagurusamy

Tokens, Expressions and Control Structures


3.1 Basic Data Types
3.2 User Defined Data Types
3.3 Derived Data Types
3.4 Variables
3.5 Operators in C++
3.6 Scope Resolution Operator
3.7 Manipulators
3.8 Control Structure
Tokens:

- Smallest individual units in a program are known as tokens.


- C++ has the following tokens:
o Keywords
o Identifiers
o Constants
o Strings
o Operators
o Special Symbols
- C++ program is written using these tokens, white spaces, and the syntax of the
language.

KeyWords:

- Keywords are predefine words(identifiers), whose meaning is known to compiler.


- Many of them are common to both C (32 keywords ) and C++.
- Additional keywords have been added to the ANSI C keywords in order to enhance its
features and make it an object-oriented language.
- ANSI C++ standards committee has added some more keywords to make the
language more versatile.
- Examples
public, private, protected, class, template, try, catch, throw, this, inline, operator etc.

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.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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.

Fig: 3.1 Data Types.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

- 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.

Qualifiers or Type Modifiers


Types Qualifiers

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.

Type Size in Byte Range

char 1 byte -128 to 127

signed char 1 byte -128 to 127

unsigned char 1 byte 0 to 255

int 2 byte -32768 to 32767

signed int 2 byte -32768 to 32767

unsigned int 2 byte 0 to 65535

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

short int 1 byte -128 to 127

long int 4 byte -2147483648 to 2147483647

signed short int 1 byte -128 to 127

unsigned short int 2 byte 0 to 65535

signed long int 4 byte -2147483648 to 2147483647

unsigned long int 4 byte 0-4294967295

float 4 byte -3.4E-38 to 3.4E+38

double 8 byte -1.7E-308 to 1.7E308

long double 10 byte -3.4E-4932 to 3.4E-4932

Table 3.1 : Data Type, its size & range

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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.

Operators Nature Symbols Used


(+,-)Unary & all Binary
Arithmetic +,-,*,/,%

Assignment Binary =,+=,-=,*=,/=,&=,>>=%=,<<= etc

Relational Binary >,<,>=,<=,==,!=

Logical Binary &&,||,!

Increment &
Unary ++, --
Decrement

Bitwise Binary &, |, ~, ^, <<, >>

Conditional Ternary ?:

Other Special Unary ., ->, *, &, sizeof, (type),[ ],( ),,

- 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.

Memory Management Operator: [new & delete]

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

- 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

2. int *p = new int(25);

3. int *p = new int[10];

- delete

o When data object no longer needed, it is destroyed to release the memory


space for reuse.
o Syntax:
delete pointer_variable; //normal variable…
delete [size] pointer variable // Array variable…
o Example:
delete[ ]p; // we can skip array size…

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

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

cout << setw(5) << sum << endl;

 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>

Example program to illustrate the use of manipulators

#include <iostream.h>
#include <iomanip.h> // for setw
int main( ){
int Basic=950,Allowance=95, Total=1045;

cout «setw(10) « "Basic" « setw(10) « Basic « endl


« setw(10) « "Allowance" « setw(10) « Allowance « endl
« setw(10) « "Total" « setw(10) « Total « endl;
return 0;
}

The following is the output of above program:


Basic 950
Allowance 95
Total 1045

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Type Cast Operator


- C++ permits explicit type conversion of variables or expressions using the type cast
operator.
- Traditional C casts are augmented in C++ by a function-call notation as a syntactic
alternative.
- The following two versions(Syntax) are equivalent:
- Syntax:
(type-name) expressi on // C notation
type-name (expression) // C++ notation average
- examples :

average = sum/(float)i; // C notation


average = sum/float(i); // C++ notation
- A type-name behaves as if it is a function for converting values to a designated type.
- The function call notation usually leads to simplest expressions. However, it can be
used only if the type is an identifier.
- For example,
p = int*(q);
is illegal.

in such cases we must use c type notation


p= (int *)q;
- Alternatively, we can use typedef to create an identifier of the required type and use it
in the functional notation.
- Example:
typedef int * int_pt;
p = int_pt(q);

Expressions and Their Types


- An expression is a combination of operators, constants and variables arranged as per
the rules of the language.
- It may also include function calls which return values.
- An expression may consist of one or more operands, and zero or more operators to
produce a value.
- Expressions may be of the following seven types:
o Constant expressions (20+30)
o Integral expressions (a+b)
o Float expressions (3.147f*r*r)
o Pointer expressions(&ref)
o Relational expressions(a>b)
o Logical expressions(a>b&&a>c)
o Bitwise expressions(a>>1)
- An expression may also use combinations of the above expressions. Such expressions
are known as compound expressions.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Precedence of Operator or Priority.

Precedence Operator Symbol Associativity of


Operator

0. :: Left to Right

1. () [] . -> Left to Right

2. Unary +, Unary -, ++, --, ~, ! ,*, &, sizeof, Right to Left


(type), new , delete

3. */% Left to Right

4. +-

5. << >>

6. <<= >>=

7. == !=

8. &

9. ^

10. |

11. &&

12. ||

13. ?: Right to Left

14. = += -= *= /= %= Right to Left

15. , Left to Right.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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.

Decision Making Statements Loop Control Statements Jump Control Instructions


or or or
Conditional Statements Iterative Statements Branching Statement
 if  for  break
 if-else  while  continue
 Nested if-else  do-while  goto
 else if Ladder  return
 switch-case  exit( )

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.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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.

Syntax:- Flow Chart:-


1. if-else with single statement
if(condition)
true_statement;
else Condition true_statement
false_statement; true

false

false_Statement

In above syntax if condition is evaluated first, if condition is evaluated as true then


true_statement is getting executed. If condition is evaluated as false then false_statement is
getting executed.
2. if-else with multiple statement. Flow Chart:-
if(condition){
true_statement 1;
true_statement 2;
……. Condition
true_statement n; true_statements
}
else{ true
true_statement 1;
true_statement 2; false
…….
true_statement n; false_Statements
}

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.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Example:- Programming Example to show use of if-else


/* program to check entered number is even or odd. */
main( ){
int num;
cout<< “Enter one number \n”;
cin>>num;
if(num%2==0)
cout<< “Even”;
else
cout<< “odd”
}

Output:-

Enter one number


33
33 is odd

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;
}
}

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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

Enter three numbers


16 32 5
32 is greater

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;

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

}
In above format, condition is evaluated from top to down.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Example:- Programming example to show use of else-if ladder


/* program to find largest amongst five numbers */

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:

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

- equality_constant or variable or expression should be of type int or char.


- default is optional
Example:- Programming example to show use of switch-case
#include<stdio.h>
main( ){
int choice=2;
switch(choice){
case 1:
cout<<“Pune\n”;
break;
case 2:
cout<<“Shirpur\n”;
break;
case 3:
cout<<“Delhi\n”;
break;
case 4:
cout<<“Mumbai\n”;
default:
cout<<“Any other”;
}
}
Output
Shirpur

Nested switch case:


- outer switch block may contain inner switch block, i. e. switch with in switch.
- The inner and/or outer switch may contain same equality constant.

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;
}

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Loop Control Statements Or Iterative Statements


Loop???
Statements in a block are repeatedly executed for certain number of times or period.
Steps used in Loop???
1. Initialization
2. Condition or Test Expression
3. Update expression.
Initialization:-
- Initial value i.e. starting value is assigned to loop variable(i.e. index variable)
- Executed only ones in life time of loop.
Condition:-
- Every time condition is evaluated, if it is evaluated as true then control will get
entry in loop otherwise loop will be terminated.
- Condition is either true or false as we use relational and/or logical operators to
write condition.
Update Expression:-
- Any expression to update loop variable, after updating loop variable control will
check condition again & this will be iterative till condition becomes false.
Loops are categorized into
1. Entry Controlled Loop
i. for
ii. while
2. Exit Controlled Loop
i. do while

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.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Example:- Programming example to show use of for loop.


/* Write a c++ program to print first ‘n’ numbers. */
#include<stdio.h>
main( ){
int i,n;
cout<<“Enter value of n:”;
cin>>n;
for(i=1;i<=n;i++)
printf(“%d\t”,i);
}

Output
Enter value of n:8
1 2 3 4 5 6 7 8

/* Write a c++ program to print sum of first n numbers. */


/* i. e. sum=1+2+3+…………+n*/
#include<stdio.h>
main( ){
int i,n,sum=0;
cout<< “Enter value of n:”;
cin>>n;
for(i=1;i<=n;i++)
sum+=i;
cout<< “ Sum of numbers is ”<<sum;
}
Output
Enter value of n:8
1 2 3 4 5 6 7 8
Sum of numbers is 36

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

/* C++ program to find factorial of a number */


main( ){
int n,fact=1,i;
cout<<“Enter number to find factorial”;
cin>>n;
if(n==0){
cout<<“Factorial of Zero is”<<fact;
}
else{
if(n>0){
for(i=0;i<=n;i++)
fact*=i;
cout<<“Factorial of <<n<< “ is ” <<fact;
}
else{
cout<<“As number is negative, factorial is undefined\n”;
}
}}

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.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Example:- Programming example to show use of while loop.


/* Write a c++ program to print first ‘n’ numbers. */
#include<stdio.h>
main( ){
int i,n;
cout<<“Enter value of n:”;
cin>>n;
i=1; //initial value of loop variable…
while(i<=n){
cout<<setw(10)<<i;
i++; //update expression to update loop variable…
}
}
Output

Enter value of n:9


1 2 3 4 5 6 7 8 9

/* Write a c++ program to print sum of digits of a given number. */


#include<stdio.h>
main( ){
int n,d,sum=0;
cout<<“Enter one number to find its sum of digits”;
cin>>n; //with this input function we will get initial value of loop variable.
while(n!=0){
d=n%10;
sum+=d;
n/=10; // Update expression.
}
cout<<“Sum of individual digits is ”<<sum;
}
Output
Enter one number to find its sum of digits 2357
Sum of individual digits is 17

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

/* 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

Enter one number to find sum of even and odd digits


2356
Sum of even digits is 8
Sum of odd digits is 8

/* Write a c++ program to print reverse of a given number. */

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

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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.

Example:- Programming example to show use of do-while loop.


/* Write a c program to print first ‘n’ numbers. */
#include<stdio.h>
main( ){
int i,n;
cout<<“Enter value of n:”;
cin>>n;
i=1; //initial value of loop variable…
do {
cout<<setw(10)<<i;
i++; //update expression to update loop variable…
} while(i<=n);
}
Output
Enter value of n:9
1 2 3 4 5 6 7 8 9

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Jump Control Instructions or Branching Statement


Followings are the jump control or branching statement supported by C++.

 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

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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.

Example:- Programming example to show use continue.


#include<stdio.h>
main( ){
int i; In this example, we have used
for(i=1;i<=5;i++){ ‘continue’ keyword to continue iteration of
if(i==4) continue; loop. And whenever condition if(i==4) is
cout<<“\t”<<i; evaluated as true & next iteration will be
} continued.
}

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.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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);

/* Programming example to show use of return. */


#include<stdio.h>
int main( ){
cout<<“Default return type of main( ) is integer”;
return 0;
}
Output
Default return type of main() is integer

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.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Example:- Programming example to show use of goto.


#include<stdio.h>
main( ){
int a,b;
char ch;
start: cout<<“Enter two number:”;
cin>>a>>b;
cout<<“Add is ”<<a+b;
cout<<“\n Do you want to continue[y/n]:”;
cin>>ch;
if(ch==’y’||ch==’Y’)
goto start;
else
goto end;
end: exit(0);
}
Output
Enter two numbers:2 3
Add is 5
Do you want to continue[y/n]:n

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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.

Using For Using While Using Do While


main( ){ main( ){ main( ){
int i , sum=0; int i=2 , sum=0; int i=2 , sum=0;
for(i=2; i<100; i=i+3){ while(i<100){ do{
sum = sum + i; sum = sum + i; sum = sum + i;
} i = i + 3; i = i + 3;
cout<<“Sum is”<<sum; } } while(i<100);
} cout<<“Sum is”<<sum; cout<<“Sum is”<<sum;
} }

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

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

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";
}

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi

Das könnte Ihnen auch gefallen