For loops are trivial, right? Well, I wondered about some things, probably because I'm a physicist and I don't have proper programming. Let's use an example of having a string of words, and we want to print seperatelly characters, and then in reverse order.
Q1) Should we declare a variable for size, assign it and use it, or call the size () function of the string, inside the for loop
string s = "asdf";
int size = s.size();
for (int i = 0; i<size; i++){
cout<<s[i]<<endl;
}
//or
for (int i = 0; i<s.size(); i++){
cout<<s[i]<<endl;
}
// Q2) If we want to print backward characters, what is more correct? Also, am I using the new variable size again?
//AND)
for (int i = size-1; i>= 0; i--){
cout<<s[i]<<endl;
}
//AT)
for (int i = size; i>= 0; i--){
cout<<s[i - 1]<<endl;
}
//WITH)
for (int i = size - 1; i>= 0; i--){
cout<<s[i]<<endl;
}
// D) // I used this in case I had to use I for another function, where it should be increased. (ok sure a.size = s.size)
for (int i = 0; i<size; i++){
cout<<s[size - i - 1]<<endl;
}
Q3) Which resource is most convenient?
Q4 ). ( ).
Q5) ?
.