ICS 103: Computer Programming in C

Handout-17

Topic: Arrays of Strings, the break and continue statements.

 

Instructor: M. Waheed Aslam.

 

Objective:

·       To know what are arrays of strings and how to use them

·       To know the use of break and continue statements.

 

 

Arrays of strings:

 

To represent arrays of strings we need two-dimensional arrays. The first-dimension represents the number of strings in the array and the second-dimension represents the length of each string.

Example:  #define   LEN1      5

            #define   LEN2      81  

char       strarray [ LEN1 ] [ LEN2 ] = {“This”, “is”, “ICS103”, “Course”, “LEC”} ;

char str [LEN2] = {“Instructor”} ;

 

Here the array strarray is an array of 5 strings with the size of each string being 81.

Thus:

 strarray [0] [0] = ‘T’, strarray [0] [1] = ‘h’, strarray [0] [2] = ‘i’,

              strarray [0] [3] = ‘s’, strarray[0][4] = ‘\0’;

 

Similarly,     strarray [4] [0] = ‘L’, strarray [4] [1] = ‘E’, strarray [4] [2] = ‘C’,

              strarray[4][3] = ‘\0’.

The string arrays can be read by using gets( ) and printed by using puts( ) inside a for loop as

 

#include<stdio.h>

#include<string.h>

void main()

{

#define     LEN1       5

#define   LEN2 81

 

char strarray [ LEN1 ] [ LEN2 ];

int    i;

printf("Please Enter 5 strings(Not having more than 80 characters)from Key board ");

for ( i = 0 ; i < 5 ; ++i )

{       printf ("\nEnter string number %d: ", i );

        gets ( strarray [ i ] );

        puts ( strarray [ i ] );

}

} // end of main

 

Interactive Session may be like this :

 

 

 

 


Use of break & continue statements:

The break Statement

·       We have already met break in the discussion of the switch statement.

·       It is used to exit from a loop or a switch, control passing to the first statement beyond the loop or a switch.

·       With loops, break can be used to force an early exit from the loop, or to implement a loop with a test to exit in the middle of the loop body.

·       A break within a loop should always be protected within an if statement which provides the test to control the exit condition.

 

Example:

#include<stdio.h>

void main()

{

int i;

for(i=1;i<=10;i++)

{

  if(i==5)

break;

  else

printf("%d\n",i);

}

printf("Best Wishes to you from your instructor, Najib Kofahi, ICS Dept. ");

} // end of main

 

Sample Output :

 

 

The continue Statement

·       This is similar to break but is encountered less frequently.

·       It only works within loops where its effect is to force an immediate jump to the loop control statement.

Ø       In a while loop, jump to the test statement.

Ø       In a do while loop, jump to the test statement.

Ø       In a for loop, jump to the test, and perform the iteration.

·       Like a break, continue should be protected by an if statement. You are unlikely to use it very often.

 

#include<stdio.h>

void main()

{

int i;

for(i=1;i <= 10;i++)

{

  if(i >= 5)

           continue;

  else

           printf("%d\n",i);

}

printf("Best Wishes to you from your instructor, Najib Kofahi, ICS Dept. ");

} // end of main

 

Sample Output :