Class 6 Exam  >  Class 6 Notes  >  C Programming for Beginners  >  C Break/Continue & Arrays

C Break/Continue & Arrays | C Programming for Beginners - Class 6 PDF Download

Break

  • You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch statement.
  • The break statement can also be used to jump out of a loop.
  • This example jumps out of the loop when i is equal to 4:

Example

int i;


for (i = 0; i < 10; i++) {

  if (i == 4) {

    break;

  }

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

}

Continue

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
This example skips the value of 4:

Example

int i;


for (i = 0; i < 10; i++) {

  if (i == 4) {

    continue;

  }

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

}

Break and Continue in While Loop

You can also use break and continue in while loops:

Break Example

int i = 0;


while (i < 10) {

  if (i == 4) {

    break;

  }

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

  i++;

}

Continue Example

int i = 0;


while (i < 10) {

  i++;

  if (i == 4) {

    continue;

  }

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

}

C Arrays

Arrays

  • Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
  • To create an array, define the data type (like int) and specify the name of the array followed by square brackets [].
  • To insert values to it, use a comma-separated list, inside curly braces:

int myNumbers[] = {25, 50, 75, 100};

We have now created a variable that holds an array of four integers.

Access the Elements of an Array

  • To access an array element, refer to its index number.
  • Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
  • This statement accesses the value of the first element [0] in myNumbers:

Example

int myNumbers[] = {25, 50, 75, 100};

printf("%d", myNumbers[0]);


// Outputs 25

Change an Array Element

To change the value of a specific element, refer to the index number:

Example

myNumbers[0] = 33;

Example

int myNumbers[] = {25, 50, 75, 100};

myNumbers[0] = 33;


printf("%d", myNumbers[0]);


// Now outputs 33 instead of 25

Loop Through an Array

  • You can loop through the array elements with the for loop.
  • The following example outputs all elements in the myNumbers array:

Example

int myNumbers[] = {25, 50, 75, 100};

int i;


for (i = 0; i < 4; i++) {

  printf("%d\n", myNumbers[i]);

}

Set Array Size

Another common way to create arrays, is to specify the size of the array, and add elements later:
Example

// Declare an array of four integers:

int myNumbers[4];


// Add elements

myNumbers[0] = 25;

myNumbers[1] = 50;

myNumbers[2] = 75;

myNumbers[3] = 100;

Using this method, you have to know the size of the array, in order for the program to store enough memory.
You are not able to change the size of the array after creation.

The document C Break/Continue & Arrays | C Programming for Beginners - Class 6 is a part of the Class 6 Course C Programming for Beginners.
All you need of Class 6 at this link: Class 6
10 videos|13 docs|15 tests

Top Courses for Class 6

10 videos|13 docs|15 tests
Download as PDF
Explore Courses for Class 6 exam

Top Courses for Class 6

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

shortcuts and tricks

,

Free

,

Previous Year Questions with Solutions

,

C Break/Continue & Arrays | C Programming for Beginners - Class 6

,

MCQs

,

past year papers

,

practice quizzes

,

Extra Questions

,

Objective type Questions

,

C Break/Continue & Arrays | C Programming for Beginners - Class 6

,

ppt

,

Important questions

,

Summary

,

Viva Questions

,

video lectures

,

pdf

,

study material

,

Exam

,

C Break/Continue & Arrays | C Programming for Beginners - Class 6

,

Semester Notes

,

Sample Paper

;