0

for (int i=0; i < n; i++) { float temp; temp = List1.(i) if(temp<0){ //do smth } }

So I want to store every next element(i) of the list in a variable and then check if that element is smaller than 0 (<0).I am getting stuck with what should I write after temp=List1..I am sorry for my beginner question!!!I am using C++.

Matthew Spencer
  • 2,265
  • 1
  • 23
  • 28
Huntix
  • 17
  • 7

1 Answers1

0

Disclaimer : I don't do C++

You should tell us what language you are using, and before asking a question that you yourself point as a newbie qustion you shoud search first, young padawan.

If it's java then check this : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

But in many languages this should do :

if(temp<0){
  //do something
}

For C++ : http://www.cplusplus.com/doc/tutorial/control/

Iteration in C++ : http://en.cppreference.com/w/cpp/language/range-for

or if you want to use the foreach : http://en.cppreference.com/w/cpp/algorithm/for_each

Here is an answer found on SO that should help : https://stackoverflow.com/a/16504109/4088809

looks like what you nee is

for(<type> <name> : <collection>) { ... }

if your list is contains int then

for(int i : vec) {
  if(i<0){
    // do your thing
  }
}
Community
  • 1
  • 1
Tahar Bakir
  • 716
  • 5
  • 14
  • then does my answer suit you or is it something else? – Tahar Bakir Nov 26 '15 at 00:42
  • I know what should I type after IF operator , my questions is to ( List1. .....) What should I use to get the every next element using the ''i'' from the loop. – Huntix Nov 26 '15 at 00:43
  • so your question is about iteration: how to iterate over each element of the list and if it is <0 then you do something with it. right ? – Tahar Bakir Nov 26 '15 at 00:54
  • yep , something like "temp=List1.ref(i)" but I dont know the operator after ''List1'' – Huntix Nov 26 '15 at 00:56