C Programming Part 10
C Programming Part 10
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
for(i=1;i<=10;i++){
C PROGRAMMING Page 91
printf("%d \n",i);
break;
getch();
Output
12345
Continue Statement
Continue is keyword 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
some statements inside the loop / the statements after this statement. . The continue statement is
used/ associated with decision making statement such as if ,if-else.
continue;
C PROGRAMMING Page 92
How continue statement works?
Example
C PROGRAMMING Page 93
1. #include <stdio.h>
2. #include <conio.h>
3. void main(){
4. int i=1;//initializing a local variable
5. clrscr();
6. //starting a loop from 1 to 10
7. for(i=1;i<=10;i++){
8. if(i==5){//if value of i is equal to 5, it will continue the loop
9. continue;
10. }
11. printf("%d \n",i);
12. }//end of for loop
13. getch();
14. }
Output
1234678910
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..
ARRAYS
Using Arrays in C
C PROGRAMMING Page 94
C supports a derived data type known as array that can be used to handle large amounts of data
(multiple values) at a time.
Definition:
Or
An array is a collection of data that holds fixed number of values of same type.
Or
Or
An array is a data structured that can store a fixed size sequential collection of elements of same
data type.
Suppose you have to store marks of 50 students, one way to do this is allotting 50
variables. So it will be typical and hard to manage. For example we can not access the
value of these variables with only 1 or 2 lines of code.
Another way to do this is array. By using array, we can access the elements easily. Only
few lines of code is required to access the elements of array.
Advantage of C Array
2) Easy to traverse data: By using the for loop, we can retrieve the elements of an array easily.
C PROGRAMMING Page 95
3) Easy to sort data: To sort the elements of array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of Array
Fixed Size: Whatever size, we define at the time of declaration of array, we can't exceed the
limit. So, it doesn't grow the size dynamically like LinkedList
Declaration of an Array
data-type variable-name[size/length of array];
For example:
int arr[10];
Here int is the data type, arr is the name of the array and 10 is the size of array. It means
array arr can only contain 10 elements of int type. Index of an array starts from 0 to size-1 i.e
first element of arr array will be stored at arr[0] address and last element will occupy arr[9].
Initialization of an Array
C PROGRAMMING Page 96
After an array is declared it must be initialized. Otherwise, it will contain garbage value(any
random value). An array can be initialized at either compile time or at runtime.
Example
int age[5]={22,25,30,32,35};
4 : String initialization.
1 : Initilizing all specified memory locations : If the number of values to be initialized is equal
to size of array. Arrays can be initialized at the time of declaration. Array elements can be
initialized with data items of type int,float,char, etc.
int a[5]={10,20,30,40,50};
C PROGRAMMING Page 97
During compilation, 5 contiguous memory locations are reserved by the compiler for the
variable a and all these locations are initialized.
10 20 30 40 50
If the size of integer is 2 bytes, 10 bytes will be allocated for the variable a.
C O M P U T E R
//error : number of initial values are more than the size of array.
C PROGRAMMING Page 98
//error : Number of initial values are more than the size of array.
int a[5]={10,15};
10 15 0 0 0
In general array_name[n-1] can be used to access nth element of an array. where n is any integer
number.
Example
float mark[5];
Suppose you declared an array mark as above. The first element is mark[0], second element
is mark[1] and so on.
C PROGRAMMING Page 99
Few key notes:
As you can see, in above example that I have used „for loop‟ and „scanf statement‟ to enter data
into array. You can use any loop for data input.
Code:
scanf("%d", &num[x]);
For example you want to read and display array elements, you can do it just by using any
loop. Suppose array is mydata[20].
printf("%d\n", mydata[x]);