Sunteți pe pagina 1din 50

JOGINPALLY B. R.

ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

UNIT-1

Short questions and answers(2M or 3M)


----------------------------------------------------------
1) What is computer? Characteristics of computer?(2M or 3M)

Introduction to Computer : The computer is derived from the word compute. A computer is an electronic
device that takes data and instructions as an input from the user, process data, and provides useful information
known as output. This cycle of operation of a computer is known as the input-process-output.

PROCESS

Characteristics of computer :

1 : perform complex and repetitive calculations rapidly and accurately.


2 : store large amount of data and information for subsequent manipulations.
3 : Compare items and make decisions.
4 : provide information to the user in many different forms.
5 : automatically correct and modify the parameters of a system under control.
6 : draw and print graphs,
7 : receive and display audio and video signals.

Application areas may be classified into the following categories :

1 : Data processing (commercial use)


2 : Numerical computing (scientific use )
3 : Message communication (e-mail)
4 : Image processing (animation and industrial use)
5 : Voice recognition (multimedia)

2) Write computer systems?(2M or 3M)

Computer Systems : A computer system consist of Two Types

1 : Hardware components.
2 : Software components.
1
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 : Hardware Components : Hardware refers to the physical parts of the computer system.Hardware includes
the following components.

1 : Input devices.
2 : Processor.
3 : Output devices.
4 : Memory Management.

Software : A software is collection of programs. Software can be classified into 2 types

1 : System Software : A software which solve System Related problems.A System Software which interact
with hardware.

Ex : os, device drivers, pc utilities.

2 : Application Software : Application Software which solve user defined problems.

Ex : word processing (ms-word)


Spreed sheet (Excel)
Databse
Accounting package.
Programming language.

3) Write about computer languages? (2M or 3M)

Programming Languages : The language used in the communication of computer instruction is known as
programming language.

Programming language can be classified into 3 types.

1 : machine language (low level language)


2 : assembly language (Symbolic language)
3 : procedure-oriented languages (high level languges)

1 : Machine Language :

Machine languages also called as 1st generation languages (1GL) and also called as low level language.
It id diffcult to understand and remember the various combinations of 1’s and 0’s representing data and
instructions.

2 : Assembly Language :

The assembly language also referred to as the second generation language(2GL) and also called as Low level
language.
Ex : mnemonic codes are LOAD, ADD, SUB, STORE, AND

2
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3 : High level languages :

High Level Languages can be classified into 3 types.

1 : Procedure-oriented languages (third generation)


2 : Problem-oriented languages (fourth generation)
3 : Natural languages (fifth generation)

4) How to create and running program? (2M or 3M)

Creating and Running Programmes : A ‘C’ program has to pass through many phases for its
successful execution and to achieve the desired output. The various steps in the Executing a program written in
C involes a

1 : Creating the program


2 : Compilation phase
3 : Linking phase.
4 : Execution phase.

5) What is an Algorithm? Characteristics of an Algorithm? (2M or 3M)

Algorithms : Algorithm is a method of representing the step-by-step procedure for solving a problem. An
algorithm is very useful for finding the correct answer to a simple problem or a diffcult problem by breaking the
problem into simple cases.

Characteristics of Algorithms :

1 : Each and every instruction(step) should be precise and unambiguous.


2 : The algorithm should not be repeated infinitely..
3 : Ensure that the algorithm will ultimately terminate.
4 : The algorithm should be written in sequence.
5 : The desired result should be obtained only after the algorithm terminates.

Efficiency of an Algorithm :

1 : Efficiency of an algorithm means, how fast it can produce the correct result for the given problem.
2 : Efficiency of an algorithm depends upon its time complexity and space complexity.
3 : Time complexity of an algorithm refers to the amount of computer time required for an algorithm for its
execution. This time includes both compile time and run time.
4 : Space complexity of an algorithm refers to the amount of memory required by the algorithm for its execution
and generation of the final output.

6) Define flow chart?draw the flowchart symbols? (2M or 3M)


3
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Flowcharts : The pictorial representation of algorithm is called flowchart.


Uses of flow chart:

1 : flow chart helps to understand the program easily.


2 : as different symbols are used to specify the type of operation performed, it is easier to understand the
complex programs with the help of flowcharts.

Flowchart Symbols

S.NO Description Symbols


1 Flowlines : These are the left to right or top
to bottom lines connection symbols. These
lines shows the flow of control through the
program.
2 Terminal Symbol : The oval shaped
symbol always begins and ends the Start
flowchart. Every flow chart starting and End
ending symbol is terminal symbol.
3 Input / Output symbol : The parallelogram
is used for both input (Read) and Output
(Write) is called I/O symbol. This symbol is
used to denote any function of an I/O device
in the program.
4 Process Symbol : The rectangle symbol is
called process symbol. It is used for
calculations and initialization of memory
locations.

5 Decision symbol : The diamond shaped


symbol is called decision symbol. This box
is used for decision making. There will be
always two exists from a decision symbol
one is labeled YES and other labeled NO.
6 Connectors : The connector symbol is
represented by a circle. Whenever a
complex flowchart is morethan one page, in
such a situation, the connector symbols are
used to connect the flowchart.

7) Explain Computing Environments? (2M or 3M)


4
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Computing Environments : In the early days of computers, there was only one environment. In this
section we describe several different environments.

1 : Personal Computing Environment.


2 : Time-Sharing Environment.
3 : Client / Server Environment.
4 : Distruibuted Computing.

1 : Personal Computing Environment : if we are using a personal computer, all of the computer hardware
components are tied together in our personal computer. In this situation, we have the whole computer for
ourself, we can do whatever we want.

2 : Time-Sharing Environment : In the Time sharing environment, many users are connected to one or more
computers. These computers may be minicomputers or central mainframes. The terminals they use are often
nonprogrammable, although today we see more and more microcomputers being used to simulate terminals. In
the time sharing environmet, the output devices (such as printers) and secondary storage device(such as disks)
are shared by all of the user.

3 : Client / Server Environment : A client/ server computing environment splits the computing function
between a central computer and user’s computers. The users are given personal computers or workstations so
that some of the computation responsiability can be moved from the central computer and assigned to the
workstations. In the client/server environment, the user’s microcomputers or workstations are called the client.
The central computer, which may be a powerful microcomputer, minicomputer, or central mainframe systems is
called server.

4 : Distruibuted Computing : A distributed computing environment provides a seamless integration of


computing functions between servers and clients. The internet provides connectivity to different servers
throught the world.

8) What is an identifier?write some examples? (2M or 3M)

Identifiers : Identifiers refer to the names of variables, functions and arrays. These are user-defined names is
called Identifiers.

Rules :

1 : Identifier names must be a sequence of letters and digits.


2 : The first character must be a letter/character or may begin with underscore ( _ ).
3 : The underscore character ( ‘ _ ‘) is considered a letter.
4 : Both upper/lower cases are permitted although uppercase character is not equilent to corresponding
lowercase character.
5 : ‘C’ compiler recognizes only the first 31 characters of an identifiers.
6 : No space and special symbols are allowed between the identifier.
7 : Identifier cannot be a keyword.

Ex : Valid Invalid
5
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
STDNAME Return
SUB $stay
TOT_MARKS 1RECORD
_TEMP STD NAME.
Y2K

9) What is keyword? Write some examples? (2M or 3M)

Keywords : All keywords have fixed that means cannot be changed. Keywords serve as basic building
blocks for program statements. All keywords must be written in lowercase.The ‘C’ keywords are listed below.

auto break case char


const continue default do
double enum else extern
float for goto if
int long return register
signed short static sizeof
struct switch typedef union
unsigned void volatile while

10) What is constant? (2M or 3M)

3 : Constants : Constants refer to fixed values that do not change during the execution of a program.
C supports several kinds of constants.

CONSTANTS

Numeric Constants Character Constants

Integer Constants Real Constants SingleCharacter Constants String Constants

11) What is variable? Rules of variables? Write some examples? (2M or 3M)

6
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Variables : A variable is an identifier that is used to represent some specified type of information . A variable
may take different values at different times during the execution. i.e it is the named memory location.

Rules :

1 : A variable name can be any combination of alphabets, digits or underscore.


2 : The first character must be an alphabet or underscore ( ‘-‘ ).
3 : Variables cannot be a keyword.
4 : The length of the variable cannot exceed upto 8 characters. And some of the ‘C’ compilers can be
recognized upto 31 characters.
5 : Uppercase and lowercase are significant. That is the variable Total is not the same as total or TOTAL.

Ex : i rank1 MAX min


Student_name Mark StudentName class_mark

Invalid Variable Name :

a’s illegal character(‘)


fact recu blank not allowed
5root first character should be a letter.
Student,rec comma not allowed.

12) How to decleration of variable? (2M or 3M)

Declaration of Variables : A variable can be used to store a value of any data type. The declaration of
variables must be done before they are used in the program. The general format for declaring a variable.

Syntax : data_type variable-1,variable-2,------, variable-n;


Variables are separated by commas and declaration statement ends with a semicolon.

Ex : int x,y,z;
float a,b;
char m,n;

13) How to initializing of variable? (2M or 3M) (OR)


How to assigning of variable? (2M or 3M)

Assigning values to variables : values can be assigned to variables using the assignment operator (=). The
general format statement is :
7
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Syntax : variable = constant;

Ex : x=100;
a= 12.25;
m=’f’;

we can also assign a value to a variable at the time of the variable is declared. The general format of declaring
and assigning value to a variable is :

Syntax : data_type variable = constant;

Ex ; int x=100;
float a=12.25;
char m=’f’;

14) How to accessing of variable? (2M or 3M)

Accessing of variable: accessing means printing variable on the display screen.

Syntax : printf(“control string”, var);


Example: printf(“%d”,x);
printf(“%f”,a);
printf(“%c”,c);

15) What is data type? Write data types? (2M or 3M)

Data Types : A datatype is a keyword/predefined instruction used for allocating memory for data.In order to
store data the program must reserve space which is done using datatype.

ANSCI Csupports 3 classes of data types.


1 : Primary data types.
2 : Derived data types.
3 : User defined data types.
All C compiler supports 5 fundamental data types.
1 : Integer(int).
2 : floating point(float).
3 : character(char).
4 : double-precesion floating point(double).
5 : void.
16) Write input/output functions? (2M or 3M)

Input / Output (I/O) Functions : In ‘C’ language, two types of Input/Output functions are available, and
all input and output operations are carried out through function calls. Several functions are available for input /
output operations in ‘C’. These functions are collectively known as the standared i/o library.
8
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

All these functions are available in the header file stdio.h.

printf() : output data or result of an operation can be displayed from the computer to a standard output device
using the library function printf(). This function is used to print any combination of data.

Syntax : printf(“control string “, variable1, variable2, -----------, variablen);

Ex : printf(“%5d”,3977); // Output: 3977


printf(“%-5d”,3977); // Output:3977
printf(“%d08d”,3977); // Output:00003977
printf(“%.2f”,10.000000); //Output : 10.00

scanf() : input data can be entered into the computer using the standard input ‘C’ library function called
scanf(). This function is used to enter any combination of input.

Syntax : scanf(“control string “,&var1, &var2,----, &varn);

Ex : scanf(“%d”,&a);
scanf(“%lf”,&sum);

The scanf() function is used to read information from the standard input device (keyboard).

17) Write any two operators with examples? (2M or 3M)

1) Arithmetic Operators : The arithmetic operators perform arithmetic operations and can be classified
into unary and binary arithmetic operators.

 They are Two unary arithmetic operators, plus (+), and minus (-)
 They are Five binary arithmetic operators.

Operator Symbol Form Operation


Addition + X+Y X plus Y
Subtraction - X-Y X minus Y
Multification * X*Y X multiplied by Y
Division / X/Y X divided Y
Modulus(Rema % X%Y The remainder of X
inder) divided by Y
% operator : This operator is called modular division and operates of this operator are integers only.

Ex : a % b means the remainder when a divided by b.


2%3=2
3%3=0
7%3=1

9
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The following table shows how the division operates on various data types.

Operation Result Example


int / int Int 2/5=0
int / float Float 5 / 2.0 = 2.5
float / int Float 5.0 / 2 = 2.5
float / float Float 5.0 / 2.0 = 2.5

2) Relational Operators : are used to compare two or more operands. Operands may be variables,
constants or expressions.

Operator Meaning Example Return value


< is less than 2<9 1
<= is less than or equal to 2<=2 1
> is greater than 2>9 0
>= is greater than or equal to 3>=2 1
== is equal to 2==3 0
!= is not equal to 2!=2 0

 Each of these operators evaluates to the Boolean value. The Boolean value 0 indicates (false), 1
indicates (true).

3) Logical Operators : These operators are used to combine the results of two or more conditions. ‘C’ has
the following logical operators.

Operator Meaning Example Return value


&& Logical AND (9>2)&&(17>2) 1
|| Logical OR (9>2) || (17 = = 7) 1
! Logical NOT 29!=29 0

Logical AND : If any one condition false the complete condition becomes false.

Truth Table
Op1 Op2 Op1 && Op2
true true true
true false false
false true false
false false false
10
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Logical OR : If any one condition true the complete condition becomes true.

Truth Table
Op1 Op2 Op1 && Op2
true true true
true false true
false true true
false false false

Logical Not : This operator reverses the value of the expression it operates on i.e, it makes a true
expression false and false expression true.

Op1 Op1 !
true false
false true

4 ) Assignment Operator :Assignment operators are used to assign a value (or) an expression (or) a value of a
variable to another variable.

Syntax : variable name=expression (or) value

Ex : x=10;
y=20;

Compound assignment operator : ‘C’ provides compound assignment operators to assign a value to variable
in order to assign a new value to a variable after performing a specified operation.

Operator Example Meaning


+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y X=x%y

5 ) Increment and Decrement Operator (Unary Operators) :The increment and decrement operators are
very useful in ‘C’ language.They are extensively used in for and while loop.

The syntax of the operators is given below.

++<variable name> --<variable name>


<variable name>++ <variable name>--

11
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The operator ++ adds 1 to the operand and – subtracts 1 from the operand. These operators in two forms :
prefix (++x) and postfix(x++).

Operator Meaning
++x Pre increment
- -x Pre decrement
x++ Post increment
x-- Post decrement

Where

1 : ++x : Pre increment, first increment and then do the operation.


2 : - -x : Pre decrement, first decrements and then do the operation.
3 : x++ : Post increment, first do the operation and then increment.
4 : x- - : Post decrement, first do the operation and then decrement.

6 ) Conditional (or) Ternary Operator : conditional operator checks the condition and executes the statement
depending of the condition. Conditional operator consist of two symbols.

1 : question mark (?).


2 : colon ( : ).

Syntax : condition ? exp1 : exp2;

It first evaluate the condition, if it is true (non-zero) then the “exp1” is evaluated, if the
condition is false (zero) then the “exp2” is evaluated.

7 ) Special Operators :

1 ) Comma Operator :The comma operator is used to separate the statement elements such as variables,
constants or expressions, and this operator is used to link the related expressions together, such expressions can
be evaluated from left to right and the value of right most expressions is the value of combined expressions.

Ex : val(a=3, b=9, c=77, a+c)


First signs the value 3 to a, then assigns 9 to b, then assigns 77 to c, and finaly80(3+77) to value.

2 ) Sizeof Operator : The sizeof() is a unary operator, that returns the length in bytes o the specified variable,
and it is very useful to find the bytes occupied by the specified variable in the memory.

Syntax : sizeof(variable-name);

int a;
Ex : sizeof(a); //OUTPUT-----2bytes

3 ) Pointer Operators :

& : This symbol specifies the address of the variable.


12
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* : This symbol specifies the value of the variable.

8) Bitwise Operators : Bitwise operator s are used to manipulate the data at bit level. It operates on integers
only. It may not be applied to float.

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Shift left
>> Shift right
~ One’s complement.

18) What is an expression? Write with example? (2M or 3M)

Expressions : An expression is a combination of operators and operands which reduces to a single value. An
operator indicats an operation to be performed on data that yields a value. An operand is a data item on which
an operation is performed.

A simple expression contains only one operator.

Ex : 3+5 is a simple expression which yields a value 8, -a is also a single expression.


A complex expression contain more than one operator.

Ex : complex expression is 6+*7.

Ex ; Algeberic Expressions C-expression


1 : ax2+bx+c 1: a*x*x+b*x+c
2 : a+bx 2 : a+b*x.
3 : 4ac/b 3 : 4*a*c/b.
4 : x2/y2-1 4 : x*x/y*y-1

19) What is precedence and associativity? (2M or 3M)

Operator Precedence : Arithmetic Operators are evaluvated left to right using the precedence of operator
when the expression is written without the paranthesis.They are two levels of arithmetic operators in C.

1 : High Priority * / %
2 : Low Priority + -.
The order of evaluation can be changed by putting paranthesis is an expression.
Ex : 9-12/(3+3)*(2-1)

13
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Rules for Evaluation of Expression :

1 : Evaluate the sub-expression from left to right. If parenthesized.


2 : Evaluate the arithemetic Expression from left to right using the rules of precedence.
3 : The highest precedence is given to the expression with in paranthesis.
4 : When parantheses are used, the expressions within parantheses assume highest priority.
5 : Apply the associative rule, if more operators of the same precedence occurs.

Operator Precedence and Associativity :


Every operator has a precedence value. An expression containing more than one oerator is known
as complex expression. Complex expressions are executed according to precedence of operators.

Associativity specifies the order in which the operators are evaluated with the same precedence in a
complex expression. Associativity is of two ways, i.e left to ringht and right to left. Left to right associativity
evaluates an expression starting from left and moving towards right. Right to left associativity proceds from
right to left.

20) Define if statement with example ? (2M or 3M)

Simple if statement : The general form of a simple if statement is

Syntax : if(condition) FLOWCHART


{
true statement;
}
statement-x;

If the condition is true, then the true statement will be executed otherwise the true statement
block will be skipped and the execution will jump to the statement-x. The ‘true statement’ may be a single
statement or group of statement.
If there is only one statement in the if block, then the braces are optional. But if there is more
than one statement the braces are compulsory.

21) Define if- else statement with example ? (2M or 3M)

If-else statement : The if-else statement is an extension of the simple if statement. The general form is :

Syntax : if (condition) FlowChart


{
true statement;
14
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
else
{
false statement;
}
statement-x;

If the condition is true , then the true statement and statement-x will be executed and if the condition
is false, then the false statement and statement-x is executed.
If there are only one statement in the if block or else block, then the braces are optional. But if there is
more than one statement the braces are compulsory.

22) Define nested if-else statement with example ? (2M or 3M)

Nested if-else statement : When a series of decisions are involved, we may have to use more than on if-else
statement in nested form.

Syntax : if(condition-1) FlowChart


{
if (condition-2)
{
true statement-1;
}
else
{
false statement-2;
}
}
else
{
false statement-3;
}
statement-x;

If the condition-1 is false, the false statement-1 and statement-x will be executed. Otherwise it
continues to perform the second test. If the condition-2 is true, the true statement-2 will be executed otherwise
the false statement-2 will be executed and then the control is transferred to the statement-x

23) Define else-if ladder statement with example ? (2M or 3M)


if-else ladder : The if else ladder is used when multipath decisions are involved. A multipath decision is a chain
of ifs in which the statement associated with each else is an if.

Syntax : if( condition-1)


statement-1;
else if(condition-2)
statement-2;
15
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
else if(condition-3)
statement-3;
else if(condition-n)
statement-n;
else
default-statement;
statement-x;

24) Define switch statement with example ? (2M or 3M)

Switch statement : when there are several options and we have to choose only one option from the available
ones, we can use switch statement. Depending on the selected option, a particular task can be performed. A task
represents one or more statements.

Syntax : switch(expression)
{
case value1 : statement1;
case value2 : statement2;
case value3 : statement3;
.
.
case valueN : statementN;
default :
default-statement;

The expression following the keyword switch in any ‘C’ expression that must yield an integer value. It
must be ab integer constants like 1,2,3 .

The keyword case is followed by an integer or a character constant, each constant in each must be
different from all the other.

First the integer expression following the keyword switch is evaluated. The value it gives is searched
against the constant values that follw the case statements. When a match is found, the program executes the
statements following the case. If no match is found with any of the case statements, then the statements follwing
the default are executed.
Rules for writing switch() statement.

1 : The expression in switch statement must be an integer value or a character constant.


2 : No real numbers are used in an expression.
3 : The default is optional and can be placed anywhere, but usually placed at end.
4 : The case keyword must terminate with colon ( : ).
5 : No two case constants are identical.
6 : The case labels must be constants.

25) Define break statement with example ? (2M or 3M)


16
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

break : The keyword break allows the programmers to terminate the loop. The break skips from the loop or
block in which it is defined. The control then automatically goes to the first statement after the loop or block.
The break can be associated with all conditional statements.

26) Define continue statement with example ? (2M or 3M)

continue : The keyword continue is exactly opposite to break. The continue statement is used for continuing
next iteration of loop statements. When it occurs in the loop it does not terminate, but it skips the statements
after this statement. It is useful when we want to continue the program without executing any part of the
program.

Comparision between break and continue statements


Break continue
1 : break statement takes the control to the 1 :continue statement takes the control to
ouside of the loop the beginning of the loop..
2 : it is also used in switch statement. 2 : This can be used only in loop
statements.
3 : Always associated with if condition in 3 : This is also associated with if
loops. condition.

27) Define goto statement with example ? (2M or 3M)

goto statement : goto statement doesnot require any condition. This statement passes control anywhere in the
program i.e, control is transferred to another part of the program without testing any condition.

Syntax : goto label;


.....
.....
label:

The goto statement requires a label to identify the place to move the execution. A label is a valid variable name
and must be ended with colon ( : )

28) Define loop?write types of loops? (2M or 3M)

LOOP CONTROL STATEMENTS : Looping means a group of statements are executed repeatedly, until
some logical condition is satisfied.

A looping process, would include the following four steps.

17
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1 : Initialzation of a condition variable.


2 : Test the condition.
3 : Executing the body of the loop depending on the condition.
4 : Updating the condition variable.

C language provides three/iterative/repetitive loops.

1 : while loop
2 : do-while loop
3 : for loop

29) Define while loop with example ? (2M or 3M)

While Loop : The while loop general format is :

Syntax : while (condition)


{
..............
body of the loop;
. . . . . . . . . . . ..
}

The while loop is an entry controlled loop statement, i.e means the condition is evaluated first and it
is true, then the body of the loop is executed. After executing the body of the loop, the condition is once again
evaluated and if it is true, the body is executed once again, the process of repeated execution of the loop
continues until the condition finally becomes false and the control is transferred out of the loop.
The body of the loop may have one or more statements, the blocking with the braces are needed only
if the body contains two or more statements.

30) Define do-while loop with example ? (2M or 3M)

do-while statement : The do-while loop general format is :

Syntax : do
{
body of the loop;
}while (condition);

The do-while loop is an exit controlled loop statement The body of the loop are executed first and
then the condition is evaluated. If it is true, then the body of the loop is executed once again. The process of
execution of body of the loop is continued until the condition finally becomes false and the control is transferred
to the statement immediately after the loop. The statements are always executed at least once.

31) Define for loop with example ? (2M or 3M)


18
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

for loop : The for loop general format is :

Syntax : for(initialization; condition; increment/decrement)


{
Statements;
}
The for loop is another entry-controlled loop. The execution of the for loop is as follows.

1 : Initilization :Initialization of the control variables is done.

2 :Test-Condition :The value of the control variable tested using the condition and if it is true, the statements
are executed. When the statements are executed, the control is transferred back to the for loop after evaluating
the last statement in the loop.

3 : Increment / Decrement : Now, the control variable is incremented and the new value of the control variable
is again tested to see whether it satisfies the loop condition. If the condition is true, the statements are again
executed. The process of execution of statements is continued until the condition finally becomes false and the
control is transferred to the statement immediately after the loop.

32) Difference between while and do-while loops? (2M or 3M)


Comparision between while and do-while statements
While Do-while
1 : This is the top tested loop 1 : This is the bottom tested loop.
2 : The condition is first tested. If the 2 : if execute the statement once, after it
condition is true then the statement is check the condition, if it is true the statement
executed until the condition becomes false. is executed until the condition becomes false.
3 : Loop will not be executed if the condition 3 : Loop is executed atleast once even though
is false. the condition is false.
LONG QUESTIONS AND ANSWERS (10M):
------------------------------------------------------------

1) Explain about hardware and software components?

Computer Systems : A computer system consist of Two Types


1 : Hardware components.
2 : Software components.

1 : Hardware Components : Hardware refers to the physical parts of the computer system.Hardware includes
the following components.

1 : Input devices.
2 : Processor.
3 : Output devices.

19
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4 : Memory Management.
1 : Input devices : Input device can be connected to the computer system using cables. The most commonly
used input devices are

1 : Keyboard.
2 : Mouse.
3 : Scanner.

Keyboard : A standard keyboard includes Escape Key, Function Keys, Alphanumeric Keys, Modifier Keys,
Spacebar Key, Cursor Movement Keys, Numeric Keypad, Special Keys. The Function Keys are the keys that
help perform a specific task such as searching a file or refreshing a web page. The modifier keys such as Shift
and Control Keys. The cursor movement keys include up, down, left and right keys. The spacebar key shifts the
cursor to the right by one position. The numeric keypad uses separate keypads for numbers and mathematical
operators.

Mouse : The mouse allows the user to select elements on the screen, such as tools, icons and buttons by
positioning and clicking them. We can also use a mouse to draw and paint on the screen of the computer
system. The mouse is also known as a pointing device.
The mouse consist of two buttons, a wheel at the top and a ball at the bottom of the mouse. The
wheel is used to scroll down in a document or a web page. When the ball moves, the cursor on the screen moves
in the direction in which the ball rotates. The left button of the mouse is used to select an element and the right
button, when clicked displays the special options such as open and explore and shortcut menus.

Scanner : Ascanner is an input device that converts documents and images as the digitized images
understandable by the computer system. The digitized images can be produced as blank and white images, gray
images or colored images. In case of colored images, an image is considered as a collection of dots with each
dot reoresenting a combination of red, green and blue colors. The properties of red, green and blue colors
assigned to a dot are together called as color description.

2 : Processor : The processor is also known as CPU (Central Process Unit). The CPU consist of Control
Unit(CU) nad ALU. CU stores the instruction set, which specify s the operations performed by the computer.
CU transfers the data and the instructions to the ALU for an arithmetic operation. ALU performs arithmeticl or
logical operations on the data received. The CPU help form the following hardware devices to process the data.

Motherboard : it refers to a device for connecting the CPU with the input and output devices. The components
on the motherboard are connected to all parts of a computer. Some of the components of a motherboard are :

Buses : Electrical pathways that transfer data and instructions among different parts of the computer. For
example, the data bus is an electrical pathway that transfers data among the microprocessor, memory and
input/output devices connected to the computer. The address bus transfers addresses of RAM and ROM.

System Clock : it is a clock used for synchronizing the activities performed by the computer. The electrical
signals that are passed inside a computer. As a result the system clock is faster, the processing speed of the
computer is fast.
Microprocessor : CPU components that performs the processing and controls the activities performed by the
different parts of the computer. The microprocessor is plugged to the CPU socket places on the motherboard.

20
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ROM : is a Chip that contains the permanent memory of the computer that stores information, which cannot be
modified by the end use.

RAM (Random Access Memory) : is a primary memory of a computer that stores information and programs,
until the computer is used. RAM is a chip that can be connected to the RAM slots in the motherboard.

3 : OUTPUT DEVICES : The data processed by the CPU is made available to the end user by the output
devices. The most commonly used output devices are :

1 : Monitor
2 : Printer
3 : Speaker.
4 : Plotter.

Monitor : A monitor is a outputdevice that produces visual displays generated by the computer. The monitor is
also known as screen, is connected as an external device using cables or connected either as a part of the CPU.
The display device is used for visual presentation of textual and graphical information.
The monitor can be classified as cathode ray tube (CRT) monitors or liquid crystal display
(LCD). The CRT monitors are large, occupy more space in the computer, whereas LCD monitors are thin, light
weighted and occupy lesser space.

Printer : The printer is an output device that transfers the text displayed on the screen onto the paper sheets that
can be used by the end user.

different types of printers.

Dot matrix printer : are commonly used in low quality and high volume applications like invoice printing, cash
registers.

Inkjet printers : are slower than dot matrix printers and generate high quality photographic prints.

Laser printers : laser printer may or may not be connected to a computer to generate an output. These printers
consist of a microprocessor, RAM and ROM,they can produce high qualityprints in quicker time.

Speaker : The speaker is an electroechanical that converts an electrical signal into sound. They are attached to a
computer as output devices to provide audio output, such as warning sounds and internet audios.

Plotter : The plotter is an output device that is connected to a computer to print large documents, such as
engineering or constructional drawings. Plotters use multiple ink pens or inkjets with color cartridges for
printing. A computer transmits binary signals to all the print heads of the plotter. Each binary signal contains the
coordinates of where a print head needs to be positioned for printing.

4 : Memory Management : Memory Management can be classified as primary memory and secondary
memory.

21
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Primary Memory : The primary memory is represented as a set of locations with each location occupying 8
bits. Each bit in the memory is identified by a unique address. The data is stored in the machine-understandable
binay form in these memory locations. The commonly used primary memories are as follows.

ROM : ROM represents Read Only Memory that stores data and instructions, even when the computer is turned
off. It is the permanent memory of the computer where the contents cannot be modified by the end user. ROM
is a chip that is inserted into the motherboard.

RAM : RAM is the read/write memory in which the information is retained only as long as there is a regular
power supply. When the power supply is interrupted or switched off the information stored in the RAM is lost.
RAM is volatile memory.

Cache Memory : Cache memory is used to store the data and the related application that was last processed by
he CPU. When the processor performs processing, it first searches the cache memory and then the RAM.

Secondary Memory : represents the external storage devices that are connected to the computer. They provide
a non-voltatile memory. A stoage device is either located in the CPU of the computer or is connected externally
to the computer. The secondary storage device can be classified as

Magnetic storage device : The magnetic storage devices store information that can be read, erased and
rewritten a number of times. These includes floppy disk, hard disk.

Optical storage device : The optical storage device are secondary storage devices thatuse to read the stored
data. These include CD-ROM,rewritable compact disk (CD-RW), digital video disks with read only memory
(DVD-ROM).

Magneto-optical storage device : The magneto-optical devices are generally used to store information such as
large programs, files and back up data.

Software : A software is collection of programs. Software can be classified into 2 types

1 : System Software : A software which solve System Related problems.A System Software which interact
with hardware.

Ex : os, device drivers, pc utilities.

2 : Application Software : Application Software which solve user defined problems.

Ex : word processing (ms-word)


Spreed sheet (Excel)
Databse
Accounting package.
Programming language.

2) Explain programming languages with examples?

22
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Programming Languages : The language used in the communication of computer instruction is known as
programming language.

Programming language can be classified into 3 types.

1 : machine language (low level language)


2 : assembly language (Symbolic language)
3 : procedure-oriented languages (high level languges)

1 : Machine Language : The language which uses all instructions and data should be written using binary
codes 1 and 0 . The binary code is called machine code or machine language. Computers do not understand
English, Hindi and Tamil. They respond only to machine language. each computer has its own machine
language.

Disadvantages

1 : It id diffcult to understand and remember the various combinations of 1’s and 0’s representing data and
instructions.
2 : Since every machine has its own machine language.The user cannot communicate with other computers
Machine languages also called as 1st generation languages (1GL) and also called as low level language.

2 : Assembly Language :

1 : The assembly language also referred to as the second generation language(2GL) and also called as Low level
language.
2 : The assembly language the 0’s and 1’s of machine language are replaced with abbreviations or mnemonic
code.
3 : An assembly language instruction consist of a mnemonic code followed by 0 or more operands. The
mnemonic code is called operation code or opcode.
Ex : mnemonic codes are LOAD, ADD, SUB, STORE, AND

3 : High level languages : High level languages are like COBOL, Pascal, FORTRAN and C are more abstract,
easier to use, and more portable across platforms , as compared to low-level language.

High Level Languages can be classified into 3 types.

1 : Procedure-oriented languages (third generation)


2 : Problem-oriented languages (fourth generation)
3 : Natural languages (fifth generation)

1 : Procedure-oriented languages : High-level languages designed to solve general-purpose problems are


called procedure-oriented languages or third generation languages. These include C, C++, JAVA, COBOL
(Common business oriented language), BASIC (Beginners All Purpose Symbolic Instruction Code) ,
FORTRAN (FORmula TRANslation).

23
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 : Problem-oriented languages : Problem-oriented languages are used to solve specific problems and also
called as fourth generation language (4GL). These include query languages, Report Generators and Application
Generators. A single statement in a fourth-generation can perform the same task as multiple lines of a third-
generation language.

3 : Natural languages : Natural languages are designed to make a computer to behave like an expert and solve
problems. These languages are known as fifth generation languages (5GL). Natuaral languages such as LISP
and PROLOG are mainly used to develop artificial intelligence and expet systems.

3) How to creating and running program? Explain in detail.

Creating and Running Programmes : A ‘C’ program has to pass through many phases for its
successful execution and to achieve the desired output. The various steps in the Executing a program written in
C involes a
1 : Creating the program
2 : Compilation phase
3 : Linking phase.
4 : Execution phase.

Process of Compiling and Running a ‘C’ Program

24
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1 : Creating the program : The C program is entered into a file through a text editor. The program has been
written. The file is saved on the sisk with an extension of ‘.C’.

2 : Compilation phase : This phase is carried out by a program called as compiler. Compiler translates the
source code into the object code. The compilation phase cannot proceed successfully until and unless the source
code is error-free. If it encounters syntax error in the source code. The compiler generates message. The error-
frr source code is translated into the object code and stored in a separate file with an extension ‘.obj’.
25
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This can be done in ‘C language by pressing ALT +F9 or choose Compile option in the menu system.

3 : Linking the program with system library : ‘C’ language program is the collection of predefined functions.
These functions already written in some standard ‘C’ header files. Before executing a ‘C’ program we need to
link with system library. This can be done automatically at the time of execution.

4 : Executing the Program : in this phase, the executable object code is loaded into the memory and the
program execution begins. We may encounters during the execution phase.At this time there is a possibility to
show two types of errors given below.

1 : Logical Errors : These errors are Logic of the program. The programmer might be using a wrong formula
or the design of the program itself is wrong.

2 : Data Errors : These are the errors, in which the input data given is not in a proper sysntax as specified in
input statements.

Executing the program can be done pressing CTRL + F9 or choose Run option from the menu system.

4) a) Define an Algorithm and Flowchart?


b) write an Algorithm and draw the Flowchart to any one program?

Algorithms : Algorithm is a method of representing the step-by-step procedure for solving a problem. An
algorithm is very useful for finding the correct answer to a simple problem or a diffcult problem by breaking the
problem into simple cases.

Characteristics of Algorithms :

1 : Each and every instruction(step) should be precise and unambiguous.


2 : The algorithm should not be repeated infinitely..
3 : Ensure that the algorithm will ultimately terminate.
4 : The algorithm should be written in sequence.
5 : The desired result should be obtained only after the algorithm terminates.

Efficiency of an Algorithm :

1 : Efficiency of an algorithm means, how fast it can produce the correct result for the given problem.
2 : Efficiency of an algorithm depends upon its time complexity and space complexity.
3 : Time complexity of an algorithm refers to the amount of computer time required for an algorithm for its
execution. This time includes both compile time and run time.
4 : Space complexity of an algorithm refers to the amount of memory required by the algorithm for its execution
and generation of the final output.

Flowcharts : The pictorial representation of algorithm is called flowchart.

Uses of flow chart:


26
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1 : flow chart helps to understand the program easily.


2 : as different symbols are used to specify the type of operation performed, it is easier to understand the
complex programs with the help of flowcharts.

Flowchart Symbols

S.NO Description Symbols


1 Flowlines : These are the left to right or top
to bottom lines connection symbols. These
lines shows the flow of control through the
program.
2 Terminal Symbol : The oval shaped
symbol always begins and ends the Start
flowchart. Every flow chart starting and End
ending symbol is terminal symbol.
3 Input / Output symbol : The parallelogram
is used for both input (Read) and Output
(Write) is called I/O symbol. This symbol is
used to denote any function of an I/O device
in the program.
4 Process Symbol : The rectangle symbol is
called process symbol. It is used for
calculations and initialization of memory
locations.

5 Decision symbol : The diamond shaped


symbol is called decision symbol. This box
is used for decision making. There will be
always two exists from a decision symbol
one is labeled YES and other labeled NO.
6 Connectors : The connector symbol is
represented by a circle. Whenever a
complex flowchart is morethan one page, in
such a situation, the connector symbols are
used to connect the flowchart.

5) explain Software Development Life Cycle?

Software Development Life Cycle : The modern programming projects are built using a series of
interrelated phases commonly referred to as the software development life cycle.

One very popular development life cycle is the waterfall model. It describes the various phases
involved in the development.

27
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Systems Requirements

Analysis

Design

Coding

System Test

Maintenance

1 : Systems Requirements : In this phase, the systems analyst defines requirements that specify what the
proposed system is to accomplish. The requirements are usually stated in terms that the user understands.

2 : Analysis phase : In this phase of program development involves analyzing the problem in order to achieve
the objectives of the program.In this phase all the identified requirements are documented.

3 : Designing phase : This phase involves making the plan of action before actually starting the development
work. The plan is made on the basis of the program specifications identified in the previous phase. This phase
helps in framing the core structure of the program. In addition the designing phase has an added advantage of
modularity.

4 : Coding phase : This phase involves writing the instructions or code for the program on the basis of the
design document created in the previous phase. The choice of the programming language in which the program
will be developed is made on the type of program.

5 : System Test : After we have written our program, we must test it. Program testing can be a very tedious and
time-consuming part of program development. As the programmer, we are responsiable for completely testing
our program. In large development projects, there are often specialists known as system engineers who ar
responsiable for testing the system as a whole- that is for testing to make sure all the program works
together.There are two types of Testing.

1 : BlackBox Testing : is done by the system test engineer and the user.
2 : WhiteBox Testing : is the resposiability of the programmer.

6 : Maintenance : keeps the system working once it has been put into the production.

6) Write structure of C program with example?


28
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

STRUCTURE OF C PROGRAMS
Every ‘C’ programe may contain one or more sections given below.

Documentation section

Preprocessor section

Definition section

Global variable decleration section

Main()
{
Declaration part;
Executable part;
}

Sub program section


{
Body of the subprogram;
}

1 : Documentation section : It consist of a set of comment lines giving the name of the program, the author
and other details etc. Documentation section is a optional section.

Comments : means are very helpful in identifying the program features and underlaying logic of the program.
The lines begins with ‘/*’ and ending with ‘*/’ are known as comment lines. These are not executable, the
compiler is ignored any thing in between /* and */.

2 : Linker section : is also called preprocessor section. It provides instructions to the compiler to link functions
from the system library.
Ex : #include<stdio.h> ( standared input output header file ).
#include<conio.h> ( console input output header file )

3 : Definition section : This section defines all symbolic constants.

Ex : #define PI 3.14

29
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4 : Global Declaration : There are some variables that are used in more than one function. Such variables are
called global variables and are declared in the global declaration section that is outside of all the functions. This
section also declares all the user-defined functions.

5 : main() function : Every C program must have one main(0 function. This section contains two parts.

1 : Declaration part
2 : Executable part.

1 : Declaration part : This part is used to declare all the variables that are used in the executable part of the
program and these are called local variables.

2 : Executable part : It contains atleast one valid ‘C’ statement. The execution of a program begins with
opening brace ‘{‘ and ends with closing brace ‘}’. The closing brace of the main() function is the logical end of
the program.
All the statements in the program ends with a semicolon except conditional and control statements.

6 : sub program section : This contains all the user defined functions that are called in the main() function.
User defined functions are generally placed immediately after the main() function.

Ex :
/* program name : sample program
Author : jbrec
Language : C */
#include<stdio.h>
main()
{
printf(“This is First Programme”);
}

Programming Rules : The following rules should follow while writing a ‘C’ program.
1 : All statements in ‘C’ program should be written in lower case letters. Upper case letters are only used for
symbolic constants.
2 : The program statement can write anywhere between the two braces following the declaration part.
3 : The user can also write one or more statements in one line separating them with a semicolon.
4 : Blank spaces may be inserted between the words. It is not used while declaring a variable, keyword,
constant, and function.

7) Explain C tokens with examples?

C TOKENS : The smallest individual units are known as tokens. C has six types of tokens.
1 : Identifiers
2 : Keywords
3 : Constants
30
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4 : Strings
5 : Special Symbols
6 : Operators.

1 : Identifiers : Identifiers refer to the names of variables, functions and arrays. These are user-defined
names is called Identifiers.

Rules :

1 : Identifier names must be a sequence of letters and digits.


2 : The first character must be a letter/character or may begin with underscore ( _ ).
3 : The underscore character ( ‘ _ ‘) is considered a letter.
4 : Both upper/lower cases are permitted although uppercase character is not equilent to corresponding
lowercase character.
5 : ‘C’ compiler recognizes only the first 31 characters of an identifiers.
6 : No space and special symbols are allowed between the identifier.
7 : Identifier cannot be a keyword.

Ex : Valid Invalid
STDNAME Return
SUB $stay
TOT_MARKS 1RECORD
_TEMP STD NAME.
Y2K

2 : Keywords : All keywords have fixed that means cannot be changed. Keywords serve as basic building
blocks for program statements. All keywords must be written in lowercase.The ‘C’ keywords are listed below.

auto break case char


const continue default do
double enum else extern
float for goto if
int long return register
signed short static sizeof
struct switch typedef union
unsigned void volatile while

3 : Constants : Constants refer to fixed values that do not change during the execution of a program.

31
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C supports several kinds of constants.

CONSTANTS

Numeric Constants Character Constants

Integer Constants Real Constants SingleCharacter Constants String Constants

Integer Constants : An integer constants can be specified in 3 ways

1 : Decimal Integer
2 : Octal
3 : Hexadecimal

1 : Decimal Integer : the rules for represent decimal integer.

a) Decimal Integer value which consist of digits from 0-9.


b) Decimal Integer value with base 10.
c) Decimal Integer should not prefix with 0.
d) It allows only sign (+,-).
e) No special character allowed in this integer.

Ex : valid invalid
7 $77
77 077
+77 7,777
-77

2 : Octal : An integer constants with base 8 is called octal. These rules are :

a) it consist of digits from 0 to 7.


b) It should prefix with 0.
c) It allows sign (+,-).
d) No special character is allowed.

EX : VALID INVALID
0123 123 -> it because no prefix with 0
+0123 0128 -> because digits from 0 to 7.
32
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-0123

3 : Hexadecimal : An integer constant with base value 16 is called Hexadecimal.

a) It consist of digits from 0-9,a-f(capital letters & small leters.

Ex : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
b) it should prefix with 0X or 0x.
c) it allows sign (+,-).
d) No special character is allowed.

EX : OX1a, ox2f

Real Constants : Real constant is base 10 number, which is represented in decimal 0r scientific/exponential
notation.

Real Notation : The real notation is represented by an integer followed by a decimal point and the
fractional(decimal) part. It is possible to omit digits before or after the decimal point.

Ex : 15.25
.75
30
-9.52
-92
+.94

Scientific/Exponential Notation: The general form of Scientific/Exponential notation is

mantisha e exponent

The mantisha is either a real/floating point number expressed in decimal notation or an integer and the
exponent is an integer number with an optional sign. The character e separating the mantisha and the exponent
can be written in either lowercase or uppercase.

Ex : 1.5E-2
100e+3
-2.05e2

Single Character Constant : A character constant is either a single alphabet, a single digit, a single special
symbol enclosed within single inverted commas.

a) it is value represent in ‘ ‘ (single quote).


b) The maximam length of a character constant can be 1 character.
EX : VALID INVALID
‘a’ “12”
‘A’ ‘ab’
33
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

String constant : A string constant is a sequence of characters enclosed in double quote, the characters may be
letters, numbers, special characters and blank space etc

EX : “rama” , “a” , “+123” , “1-/a” .

Escape characters or backslash characters:

a) \n newline
b) \r carriage return
c) \t tab
d) \v vertical tab
e) \b backspace
f) \f form feed (page feed)
g) \a alert (beep)
h) \’ single quote(‘)
i) \” double quote(“)
j) \? Question mark (?)
k) \\ backslash (\)

Variables : A variable is an identifier that is used to represent some specified type of information . A variable
may take different values at different times during the execution. i.e it is the named memory location.

Rules :

1 : A variable name can be any combination of alphabets, digits or underscore.


2 : The first character must be an alphabet or underscore ( ‘-‘ ).
3 : Variables cannot be a keyword.
4 : The length of the variable cannot exceed upto 8 characters. And some of the ‘C’ compilers can be
recognized upto 31 characters.
5 : Uppercase and lowercase are significant. That is the variable Total is not the same as total or TOTAL.

Ex : i rank1 MAX min


Student_name Mark StudentName class_mark

Invalid Variable Name :

a’s illegal character(‘)


fact recu blank not allowed
5root first character should be a letter.
Student,rec comma not allowed.
Declaration of Variables : A variable can be used to store a value of any data type. The declaration of
variables must be done before they are used in the program. The general format for declaring a variable.

Syntax : data_type variable-1,variable-2,------, variable-n;


Variables are separated by commas and declaration statement ends with a semicolon.
34
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Ex : int x,y,z;
float a,b;
char m,n;

Assigning values to variables : values can be assigned to variables using the assignment operator (=). The
general format statement is :

Syntax : variable = constant;

Ex : x=100;
a= 12.25;
m=’f’;

we can also assign a value to a variable at the time of the variable is declared. The general format of declaring
and assigning value to a variable is :

Syntax : data_type variable = constant;

Ex ; int x=100;
float a=12.25;
char m=’f’;

Operators : An operator is a Symbol that performs an operation. An operators acts some variables are called
operands to get the desired result.

Ex : a+b;
Where a,b are operands and + is the operator.
 Based on the operands on which operates it has been classified into 3 categories.
1) Unary Operator.
2) Binary Operator.
3) Ternary Operator.

1) Unary Operator : An operator which operands one operand is called Unary Operator.

EX : ++ , -- .

2) Binary Operator : An operator which operands two operands is called Binary Operator.

Ex : +, -, *.

3) Ternary Operator : An operator which operands three operands is called Ternary Operator.

Types of Operator :

35
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1) Arithmetic Operators.
2) Relational Operators.
3) Logical Operators.
4) Assignment Operators.
5). Unary Operators.
6) Conditional Operators.
7) Special Operators.
8) Bitwise Operators.

8) Write data types and it’s sizes and ranges?

Data Types : A datatype is a keyword/predefined instruction used for allocating memory for data.In order to
store data the program must reserve space which is done using datatype.

ANSCI Csupports 3 classes of data types.

1 : Primary data types.


2 : Derived data types.
3 : User defined data types.
All C compiler supports 5 fundamental data types.
1 : Integer(int).
2 : floating point(float).
3 : character(char).
4 : double-precesion floating point(double).
5 : void.
1 : Integer Type : Integer type occupies 2 bytes. The size of the integer value is limited the range -32768 to
32767 .

C has 3 classes of integer storage namely

1 : short int.
2 : int.
3 : long int.
In both signed and unsigned forms.

Size and Ranges of Data Types with Type Qualifiers


Type Size (bytes) Range Control String
char or signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
int or signed int 2 -32768 to 32767 %d or %i
unsigned int 2 0 to 65535 %u
short int or signed short 1 -128 to 127 %d or %i
int
unsigned short int 1 0 to 255 %d or %i
long int or signed long 4 -2147483648 to 2147483647 %ld
36
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int
unsigned long int 4 0 to 4294967295 %lu
float 4 3.4E-38 to 3.4E+38 %f or %g
double 8 1.7E-308 to 1.7E+308 %lf
long double 10 3.4E-4932 to 1.1E+4932 %Lf

Floating Point Types : Floating point numbers are stored in 32 bits, with 6 digits of precesion. Floating point
numbers are defined in C by the keyword float.
A double data type number uses 64 bits giving a precision of 14 digits. These are known as double precision
numbers
Long double data type which uses 80 bits.

Character Type : A single character can be defined as a character type data(char). Characters are usually
stored in 8 bits. The qualifier signed or unsigned may be explicitly applied to char.

Void Type : The void type has no values. The type of a function is said to be void. When it does not return any
value.

9) Write input/output functions with examples?

Input / Output (I/O) Functions : In ‘C’ language, two types of Input/Output functions are available, and
all input and output operations are carried out through function calls. Several functions are available for input /
output operations in ‘C’. These functions are collectively known as the standared i/o library.

Input / Output functions are classified into two types.

37
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I / O Functions

Formated I/O Functions Unformated I/O Functions

Input Output Input Output

scanf() print() getc() putc()


fscanf() fprintf() getchar() putchar()
gets() puts()
getch()
getche()

All these functions are available in the header file stdio.h.

Formated I/O Functions : formatted I/O functions operates on various types of data.

1 : printf() : output data or result of an operation can be displayed from the computer to a standard output
device using the library function printf(). This function is used to print any combination of data.

Syntax : printf(“control string “, variable1, variable2, -----------, variablen);

Control String : The formate in which the values are printed.

Ex : printf(“%5d”,3977); // Output: 3977


printf(“%-5d”,3977); // Output:3977
printf(“%d08d”,3977); // Output:00003977
.Precesion : printf(“%.2f”,10.000000); //Output : 10.00

printf() statement another syntax :


Syntax : printf(“fomating string”);

Formating string : it prints all the character given in doublequotes (“ “) except formatting specifier.

Ex : printf(“ hello “);-> hello


printf(“a”); -> a
printf(“%d”, a); -> a value

38
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
printf(“%d”); -> no display

scanf() : input data can be entered into the computer using the standard input ‘C’ library function called
scanf(). This function is used to enter any combination of input.

Syntax : scanf(“control string “,&var1, &var2,----, &varn);

The scanf() function is used to read information from the standard input device (keyboard).

Control String : same as printf.

Each variable name (argument) must be preceeded by an ampersand (&). The (&) symbol gives the meaning
“address of “ the variable.

Unformated I/O Functions:

1 : getchar() function : A single character can be given to the computer using ‘C’ input library
function getchar().

Syntax : char variable=getchar();

The getchar() function is written in standared I/O library. It reads a single character from a standared
input device. This function do not require any arguments, through a pair of parantheses, must follow the
statements getchar().

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("Enter any character/digit:");
ch=getchar();
if(isalpha(ch)>0)
printf("it is a alphabet:%c\n",ch);
else if(isdigit(ch)>0)
printf("it is a digit:%c\n",ch);
else
printf("it is a alphanumeric:%c\n",ch);
getch();
}.

OUTPUT : Enter any character/Digit : abc


it is a alphabet:a

39
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 : putchar() function :The putchar() function is used to display one character at a time on the standared output
device. This function does the reverse operation of the single character input function.

Syntax : putchar(character varaiable);

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
printf("Enter any alphabet either in lower or uppercase:");
ch=getchar();
if(islower(ch))
putchar(toupper(ch));
else
putchar(tolower(ch));
getch();
}

OUTPUT :Enter any alphabet either in lower or uppercase :a


A

3 : gets() : The gets() function is used to read the string (String is a group of characters) from the standard input
device (keyboard).

Syntax : gets(char type_of_array_variable);

Ex :#include<stdio.h>
#include<conio.h>
void main()
{
char str[40];
clrscr();
printf("Enter String name:");
gets(str);
printf("Print the string name%s:",str);
getch();
}

OUTPUT : Enter the string : jbrec


Print the string :jbrec

4 : puts() :The puts() function is used to display the string to the standared output device (Monitor).

Syntax : puts(char type_of_array_variable);


40
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program using gets() function and puts() function.

#include<stdio.h>
#include<conio.h>
void main()
{
char str[40];
puts("Enter String name:");
gets(str);
puts("Print the string name:");
puts(str);
getch();
}
OUTPUT :Enter string name :
jbrecit
Print the string name
jbrecit

10) Explain about operators with examples?

Operators : An operator is a Symbol that performs an operation. An operators acts some variables are called
operands to get the desired result.

Ex : a+b;
Where a,b are operands and + is the operator.
 Based on the operands on which operates it has been classified into 3 categories.
1) Unary Operator.
2) Binary Operator.
3) Ternary Operator.

1) Unary Operator : An operator which operands one operand is called Unary Operator.

EX : ++ , -- .

2) Binary Operator : An operator which operands two operands is called Binary Operator.

Ex : +, -, *.

3) Ternary Operator : An operator which operands three operands is called Ternary Operator.

Types of Operators :

1) Arithmetic Operators.
2) Relational Operators.
3) Logical Operators.
4) Assignment Operators.
41
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5). Unary Operators.
6) Conditional Operators.
7) Special Operators.
8) Bitwise Operators.
9) Shift Operators.

1) Arithmetic Operators : The arithmetic operators perform arithmetic operations and can be classified
into unary and binary arithmetic operators.

 They are Two unary arithmetic operators, plus (+), and minus (-)
 They are Five binary arithmetic operators.

Operator Symbol Form Operation


Addition + X+Y X plus Y
Subtraction - X-Y X minus Y
Multification * X*Y X multiplied by Y
Division / X/Y X divided Y
Modulus(Rema % X%Y The remainder of X
inder) divided by Y
% operator : This operator is called modular division and operates of this operator are integers only.

Ex : a % b means the remainder when a divided by b.


2%3=2
3%3=0
7%3=1

The following table shows how the division operates on various data types.

Operation Result Example


int / int Int 2/5=0
int / float Float 5 / 2.0 = 2.5
float / int Float 5.0 / 2 = 2.5
float / float Float 5.0 / 2.0 = 2.5

2) Relational Operators : are used to compare two or more operands. Operands may be variables,
constants or expressions.

Operator Meaning Example Return value


< is less than 2<9 1
<= is less than or equal to 2<=2 1
> is greater than 2>9 0
>= is greater than or equal to 3>=2 1
== is equal to 2==3 0
!= is not equal to 2!=2 0

42
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 Each of these operators evaluates to the Boolean value. The Boolean value 0 indicates (false), 1
indicates (true).

3) Logical Operators : These operators are used to combine the results of two or more conditions. ‘C’ has
the following logical operators.

Operator Meaning Example Return value


&& Logical AND (9>2)&&(17>2) 1
|| Logical OR (9>2) || (17 = = 7) 1
! Logical NOT 29!=29 0

Logical AND : If any one condition false the complete condition becomes false.

Truth Table
Op1 Op2 Op1 && Op2
true true true
true false false
false true false
false false false

Logical OR : If any one condition true the complete condition becomes true.

Truth Table
Op1 Op2 Op1 && Op2
true true true
true false true
false true true
false false false

Logical Not : This operator reverses the value of the expression it operates on i.e, it makes a true
expression false and false expression true.

Op1 Op1 !
true false
false true

4 ) Assignment Operator :Assignment operators are used to assign a value (or) an expression (or) a value of a
variable to another variable.

43
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Syntax : variable name=expression (or) value

Ex : x=10;
y=20;

Compound assignment operator : ‘C’ provides compound assignment operators to assign a value to variable
in order to assign a new value to a variable after performing a specified operation.

Operator Example Meaning


+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y X=x%y

5 ) Increment and Decrement Operator (Unary Operators) :The increment and decrement operators are
very useful in ‘C’ language.They are extensively used in for and while loop.

The syntax of the operators is given below.

++<variable name> --<variable name>


<variable name>++ <variable name>--

The operator ++ adds 1 to the operand and – subtracts 1 from the operand. These operators in two forms :
prefix (++x) and postfix(x++).

Operator Meaning
++x Pre increment
- -x Pre decrement
x++ Post increment
x-- Post decrement

Where

1 : ++x : Pre increment, first increment and then do the operation.


2 : - -x : Pre decrement, first decrements and then do the operation.
3 : x++ : Post increment, first do the operation and then increment.
4 : x- - : Post decrement, first do the operation and then decrement.

6 ) Conditional (or) Ternary Operator : conditional operator checks the condition and executes the statement
depending of the condition. Conditional operator consist of two symbols.

1 : question mark (?).


2 : colon ( : ).

44
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Syntax : condition ? exp1 : exp2;

It first evaluate the condition, if it is true (non-zero) then the “exp1” is evaluated, if the
condition is false (zero) then the “exp2” is evaluated.

7 ) Special Operators :

1 ) Comma Operator :The comma operator is used to separate the statement elements such as variables,
constants or expressions, and this operator is used to link the related expressions together, such expressions can
be evaluated from left to right and the value of right most expressions is the value of combined expressions.

Ex : val(a=3, b=9, c=77, a+c)


First signs the value 3 to a, then assigns 9 to b, then assigns 77 to c, and finaly80(3+77) to value.

2 ) Sizeof Operator : The sizeof() is a unary operator, that returns the length in bytes o the specified variable,
and it is very useful to find the bytes occupied by the specified variable in the memory.

Syntax : sizeof(variable-name);

int a;
Ex : sizeof(a); //OUTPUT-----2bytes

3 ) Pointer Operators :

& : This symbol specifies the address of the variable.


* : This symbol specifies the value of the variable.

8) Bitwise Operators : Bitwise operator s are used to manipulate the data at bit level. It operates on integers
only. It may not be applied to float.

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Shift left
>> Shift right
~ One’s complement.

11) Explain conditional statements with examples? (OR)


Explain decision making statements with examples?

Conditional statements (Decision making Statements) : moving execution control from one place/line to
another line based on condition. ‘C’ language supports two conditional statements.
45
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1 : if
2 : switch.

1 : if Statement : The if Statement may be implemented in different forms.

1 : simple if statement.
2 : if –else statement
3 : nested if-else statement.
4 : else if ladder.

Simple if statement : The general form of a simple if statement is

Syntax : if(condition) FLOWCHART


{
true statement;
}
statement-x;

If the condition is true, then the true statement will be executed otherwise the true statement
block will be skipped and the execution will jump to the statement-x. The ‘true statement’ may be a single
statement or group of statement.
If there is only one statement in the if block, then the braces are optional. But if there is more
than one statement the braces are compulsory.

Rules of if statement :
1 : The condition is always enclosed within a pair of parenthesis.
2 : The conditional statement should not be terminated with semi-colons ( ; ).
3 : The curly braces indicates the scope of the if statement.
4 : The default scope is one statement. But it is a good practice to use curly braces even with a single statement.
If-else statement : The if-else statement is an extension of the simple if statement. The general form is :

Syntax : if (condition) FlowChart


{
true statement;
}
else
{
false statement;
}
statement-x;

If the condition is true , then the true statement and statement-x will be executed and if the condition is
false, then the false statement and statement-x is executed.
If there are only one statement in the if block or else block, then the braces are optional. But if there is
more than one statement the braces are compulsory.
46
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Nested if-else statement : When a series of decisions are involved, we may have to use more than on if-else
statement in nested form.

Syntax : if(condition-1) FlowChart


{
if (condition-2)
{
true statement-1;
}
else
{
false statement-2;
}
}
else
{
false statement-3;
}
statement-x;

If the condition-1 is false, the false statement-1 and statement-x will be executed. Otherwise it
continues to perform the second test. If the condition-2 is true, the true statement-2 will be executed otherwise
the false statement-2 will be executed and then the control is transferred to the statement-x

if-else ladder : The if else ladder is used when multipath decisions are involved. A multipath decision is a chain
of ifs in which the statement associated with each else is an if.

Syntax : if( condition-1)


statement-1;
else if(condition-2)
statement-2;
else if(condition-3)
statement-3;
else if(condition-n)
statement-n;
else
default-statement;
statement-x;

Switch statement : when there are several options and we have to choose only one option from the available
ones, we can use switch statement. Depending on the selected option, a particular task can be performed. A task
represents one or more statements.

Syntax : switch(expression)
{
47
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
case value1 : statement1;
case value2 : statement2;
case value3 : statement3;
.
.
case valueN : statementN;
default :
default-statement;
}

The expression following the keyword switch in any ‘C’ expression that must yield an integer value. It
must be ab integer constants like 1,2,3 .

The keyword case is followed by an integer or a character constant, each constant in each must be
different from all the other.

First the integer expression following the keyword switch is evaluated. The value it gives is searched
against the constant values that follw the case statements. When a match is found, the program executes the
statements following the case. If no match is found with any of the case statements, then the statements follwing
the default are executed.

Rules for writing switch() statement.

1 : The expression in switch statement must be an integer value or a character constant.


2 : No real numbers are used in an expression.
3 : The default is optional and can be placed anywhere, but usually placed at end.
4 : The case keyword must terminate with colon ( : ).
5 : No two case constants are identical.
6 : The case labels must be constants.
12) Explain loops with examples? (OR)
Explain iteration statements with examples?

LOOP CONTROL STATEMENTS : Looping means a group of statements are executed repeatedly, until
some logical condition is satisfied.

A looping process, would include the following four steps.

1 : Initialzation of a condition variable.


2 : Test the condition.
3 : Executing the body of the loop depending on the condition.
4 : Updating the condition variable.

C language provides three/iterative/repetitive loops.

1 : while loop
2 : do-while loop
3 : for loop
48
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1 : While Loop : The while loop general format is :

Syntax : while (condition)


{
..............
body of the loop;
. . . . . . . . . . . ..
}

The while loop is an entry controlled loop statement, i.e means the condition is evaluated first and it
is true, then the body of the loop is executed. After executing the body of the loop, the condition is once again
evaluated and if it is true, the body is executed once again, the process of repeated execution of the loop
continues until the condition finally becomes false and the control is transferred out of the loop.
The body of the loop may have one or more statements, the blocking with the braces are needed only
if the body contains two or more statements.

2 : do-while statement : The do-while loop general format is :

Syntax : do
{
body of the loop;
}while (condition);

The do-while loop is an exit controlled loop statement The body of the loop are executed first and
then the condition is evaluated. If it is true, then the body of the loop is executed once again. The process of
execution of body of the loop is continued until the condition finally becomes false and the control is transferred
to the statement immediately after the loop. The statements are always executed at least once.

Comparision between while and do-while statements


While Do-while
1 : This is the top tested loop 1 : This is the bottom tested loop.
2 : The condition is first tested. If the 2 : if execute the statement once, after it
condition is true then the statement is check the condition, if it is true the statement
executed until the condition becomes false. is executed until the condition becomes false.
3 : Loop will not be executed if the condition 3 : Loop is executed atleast once even though
is false. the condition is false.

3 : for loop : The for loop general format is :

Syntax : for(initialization; condition; increment/decrement)


{
Statements;
}

The for loop is another entry-controlled loop. The execution of the for loop is as follows.

49
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 : Initilization :Initialization of the control variables is done.

2 :Test-Condition :The value of the control variable tested using the condition and if it is true, the statements
are executed. When the statements are executed, the control is transferred back to the for loop after evaluating
the last statement in the loop.

3 : Increment / Decrement : Now, the control variable is incremented and the new value of the control variable
is again tested to see whether it satisfies the loop condition. If the condition is true, the statements are again
executed. The process of execution of statements is continued until the condition finally becomes false and the
control is transferred to the statement immediately after the loop.

50

S-ar putea să vă placă și