DEV Community

Sherzod5048
Sherzod5048

Posted on

C++ da operatorlar

Assalomu alaykum!
Operatorlar - mavzusida davom etamiz.

  • Increment/Decrement operatorlar

"++" va "--" operatorlari o'zgaruvchidagi qiymatni bittaga oshiradi yoki kamaytitadi:

#include <iostream>
#include <iomanip>

using namespace std;
int main() {
  int n = 5;
  ++n;

  cout << n;
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Bu yerda n oz'garuvchisidagi qiymat bittaga oshirildi.

#include <iostream>
#include <iomanip>

using namespace std;
int main() {
  int n = 5;
  --n;

  cout << n;
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Bu yerda esa n oz'garuvchisidagi qiymat bittaga kamaytirildi.

Agar kodda o'zgaruvchini oldidan increment/decrement oldin kelsa birinchi bo'lib incremant/decrement hisoblanib keyin boshqa buyruqlar bajariladi:

#include <iostream>
#include <iomanip>

using namespace std;
int main() {
  int n = 5;

  cout << ++n << endl;
  cout << n;

  return 0;
}
Enter fullscreen mode Exit fullscreen mode
6
6
Enter fullscreen mode Exit fullscreen mode

Lekin increment/decrement o'zgarucvhidan keyin kelsa avval boshqa buyruqlar bajarilib keyin increment/decrement amallari bajariladi:

#include <iostream>
#include <iomanip>

using namespace std;
int main() {
  int n = 5;

  cout << n++ << endl;
  cout << n;

  return 0;
}
Enter fullscreen mode Exit fullscreen mode
5
6
Enter fullscreen mode Exit fullscreen mode

Top comments (0)