Module-2 PPS Notes
Module-2 PPS Notes
a) Goto Statement: This statement is used to transfer the control from one point to another
point anywhere in the program. The goto requires a label in order to identify the place where
the branch is to be made.
• The general forms of goto statement:
• Example:
#include<stdio.h>
void main()
{
int a=1;
SHOW:
if(a>10)
{
printf("%d",a);
a+=2;
}
goto SHOW;
}
}
b) Break Statement: The break statement is used to break any type of loop as well as
switch statement. Breaking a loop means terminating the loop.
• The general forms of break statement:
break;
• Example:
switch(opr)
{
case '+':
printf("Sum : %f",(a + b));
break;
c) Continue Statement: The continue statement is used to transfer the control to the
beginning of a statement block in a loop. In other words, a break statement breaks the entire
loop, but a continue statement breaks the current iteration. The continue statement breaks the
current execution of a loop condition and then continue the loop with next condition.
• The general forms of continue statement:
continue;
• Example:
#include<stdio.h>
int main()
int i=1;
for(i=1;i<=10;i++){
if(i==5)
continue;
printf("%d \n",i);
return 0; }
d) Return Statement: It is used to return the control to the calling function with/without a
value. For example, if a function is not returning any value, use the return keyword
return;
If a function is returning a value, then
return value;
• Example:
int sumDigits()
int sum = 0;
int digit;
• Way of Looping:
1) Entry Controlled Loop or pretest loop or top test loop
2) Exit Controlled Loop or posttest loop
Example: Program to find the sum of n natural numbers using while loop
#include<stdio.h>
void main()
int i,n,sum=0;
scanf("%d",&n);
i=0;
while(i<=n)
{
sum = sum + i;
i++;
The syntax and flow chart of Do-While looping statement is as illustrated below:
void main()
int i,n,sum=0;
printf("Enter the value of n\n");
scanf("%d",&n);
i=0;
do
sum = sum + i;
i++;
while(i<=n);
Syntax:
{
body of the loop;
The first expression contains any initialization statements, the second contains the limit-test
expression, and the third contains the updating expression.
1) The initialization set the initial value of the loop control variable.
2) The test condition test the value of the loop control variable.
void main()
int i, n;
scanf(“%d”,&n);
printf(“%d\t”,i);
• Nested for-loop: Nested loops consist of an outer loop with one or more inner loops.
for (i=1;i<=100;i++)
{
for(j=1;j<=50;j++)
{
…
}
}
The above loop will run for 100*50 iterations.
ARRAYS: “An array is a sequential collection/group of homogeneous (same type) elements
which are referred by the same name”
The type refers to the data types like int, char, float etc. All the elements/values in the array are
of the same type (data type). We cannot store values of different data types in the same array.
It is normal in programming to work with a list of values or a group of values at once. For such
purposes, variables cannot be used. So, C language provides the construct array for holding
multiple values at once.
TYPES OF ARRAYS:
1) One-dimensional Arrays: To maintain a list of values.
a) One-dimensional Array Declaration: For using arrays in C programs, just like we are
declaring variables before using them, we should declare arrays before using them.
Example:
int a[10];
In the above example ’a’ is an integer array, which can hold 10 integer values.
In static initialization, the elements of an array are assigned values when the program is
compiled.
Examples:
In dynamic initialization, the values are assigned to the elements of the array during the
execution of the program.
Linear search also called sequential search is a simple searching technique. In this technique we
search for a given key item in linear order i.e. one after the other from first element to last
element. If key item is present, the search is successful, otherwise unsuccessful search.
1] LINEAR SEARCH:
• Algorithm:
Linear_search (list, value)
1] BUBBLE SORT: Bubble sort is a simple sorting algorithm. This sorting algorithm is
comparison-based algorithm in which each pair of adjacent elements is compared and the
elements are swapped if they are not in order.
• Algorithm:
for(int i = 0; i < n-1 ; i++) // reduces the comparison size by one in each iteration.
{
minimum = i ; // assuming the first element to be the minimum.
for(int j = i+1; j < n ; j++ ) // gives the effective size of the unsorted array
{
if(A[ j ] < A[ minimum ]) { //finds the minimum element
minimum = j ;
}
}
swap ( A[ minimum ], A[ i ]) ; // putting minimum element on its proper position.
}
}
a) Two-dimensional Array Declaration: For using arrays in C programs, just like we are
declaring variables before using them, we should declare arrays before using them.
Example:
int x[3] [3];
In the above example ’x’ is an integer array, which can hold 9 integer values.
In static initialization, the elements of an array are assigned values when the program is
compiled.
The memory representation of the two dimensional array will be as shown below:
In dynamic initialization, the values are assigned to the elements of the array during the
execution of the program.
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”, a[i][j]);
}
c) Example Program on Two-dimensional Array: C Program to find Transpose of a matrix
#include <stdio.h>
void main()
{
int a[10][10], t[10][10], m, n, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
printf("\nEnter elements of matrix:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
t[j][i] = a[i][j];
}
}
printf("\nTranspose of Matrix:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("%d ",t[i][j]);
}
}
2) C Program to print all numbers between a given range using while loop.
#include<stdio.h>
void main( )
{
int min, max;
printf(“Enter min and max values for range: ”);
scanf(“%d %d”, &min, &max);
while(min<=max)
{
printf(“%d\t”, min);
min++;
}
}
3) C Program to find sum and average of N natural numbers using for loop.
#include<stdio.h>
void main( )
{
int n, i, sum=0;
float avg;
printf(“Enter n value: ”);
scanf(“%d”, &n);
for(i=0;i<n;i++)
{
sum=sum+i;
}
average=sum/n;
printf(“Sum=%d\t Average=%f”,sum, avg);
}