Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
array, loop, function examples
#include <iostream> // this is a header file that allows for i/o using namespace std; int main() { // this is a constant variable. in other words, // when it is created, it is no longer allowed to // be modified const int SIZE = 10; // this creates an array, which contains the number // of members defined by the SIZE variable // in C++, these types of arrays (called static arrays) // can only be created with a constant size. in other // words, if "const int SIZE = 10;" were "int SIZE = 10;" // you would get a compilation error. there are ways to // create arrays which can be created that way (called // dynamic arrays), but don't worry about them for now int intArray [SIZE]; // right now, intArray contains 10 elements. it is // unitialized. this means that none of its elements // is set to anything in particular. you will not // get any compilation errors if you access one of // these elements, but you will essentially get garbage // if you try to access an element outside of the size // e.g. the 11th element, you will get a compilation error // lets now initialize the elements in the array, first // using a for loop (for sake of clarity, i am putting // each clause on its own line) : for(int i = 0; // this creates an index variable and // initializes it to 0 i < SIZE; // during each iteration of the loop, // i is compared to SIZE. if it is // less than SIZE, the code inside // the loop is executed. if not, // the code is skipped i = i+1) // at each step, i is incremented by // 1. other ways of writing this are: // i += 1, i++, ++i // putting this all in one line looks like: // for(int i = 0; i < SIZE; i = i + 1) { // this code gets executed during each iteration of the loop // this means you are accessing the i-th element in the array // and setting it to i i.e. the 0 element is set to 0, the 8 // element is set to 8. so on, so forth. intArray[i] = i; } }
run
|
edit
|
history
|
help
0
friend function
Wuninitialized
test
ncr
ddd
container store pointer
Kishan_Basic_Geometry
pointconcat
lambda capture
staticfun