Home » C++ programs » C++ Most popular & searched programs

How to skip some of the array elements in C++?

Learn: How to skip some of the array elements using C++ program? Here, using an example - we going to understand the concept of skipping some of the elements in C++.
Submitted by Abhishek Pathak, on October 02, 2017

Arrays are used to store continuous homogenous data. You'll find arrays in almost all of the intermediate-advanced level programs. Therefore you should know how to manipulate the array. Here is a tutorial on how you can skip on some elements of an array?

Suppose we want to skip every third element. We need to store this data in an array. C++ has one keyword known as continue which skips the current iteration and continues, whenever it sees the keyword continue. So, we will be suing this guy in our program.

Consider the program: In this program each third element of the array is skipping.

#include <iostream>
using namespace std;

int main()
	int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	int i;
	for(int i=0; i<10; i++)
	  if((i+1)%3 == 0)  //If index is every third element
		continue;  //Continue
	  cout<<arr[i]<<" ";  //Print array element
	return 0;


1 2 4 5 7 8 10 

We are using % (modulus operator) to check every 3rd iteration. If it is, the continue will skip everything down in loop's scope and continue executing the next iteration. For all other elements it will print them.

Small trick, but might come handy in future. If you find this amazing, leave comments below.



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.