ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Общий форум

i++ and ++i
Послано Neznakomec 27 июн 2013 02:58
Sorry, can anyone explain difference
between using i++ or ++i
in loop
for(int i=1;i<=n;++i)
or
for(int i=1;i<=n;i++)

i tried print "i" in loop, all the same
Re: i++ and ++i
Послано Erop [USU] 28 июн 2013 00:04
int i = 5;
int a = i++;
int b = i;
i = 5;
int c = ++i;
int d = i;
cout << a << " " << b << endl;
cout << c << " " << d << endl;
Re: i++ and ++i
Послано Bogatyr 28 июн 2013 03:52
The difference comes in when you use it like this:

while (  i++ < n )
vs.
while ( ++i < n )

also:
 value = array[ i++ ]
vs.
value = array[ ++i ].

There is a difference in the for loop version though, more subtle, when you're using iterators/objects instead of plain ints, in short, i++ is more efficient.   Except for this efficiency corner case, when "++i" or "i++" is used all by itself, you're correct, there's no apparent difference.

Edited by author 28.06.2013 03:54