DEV Community

Sherzod5048
Sherzod5048

Posted on

1

C++ da shart operatori(if, else)

If statement Shart operatori shartlar boshqarilishini shartning true yoki false ligiga qarab qaysi tartibda amalga oshirilishini ta'minlaydi.

Masala 1:

Agar a sonini 2 ga bo'lganda qoldiq 0 ga teng bo'lsa, bu son juft, aks xolda toq:

#include <iostream>

using namespace std;

int main(){
  int a;
  cin >> a;

  if(a % 2 == 0){
    cout << "juft"
  }

  else{
    cout << "toq"
  }

  return 0;
}
Enter fullscreen mode Exit fullscreen mode
12
juft
Enter fullscreen mode Exit fullscreen mode

Bu codeda agar if ni ichidagi shart true bo'lsa juft,
aks xolda ya'ni false bo'lsa toq degan xabarni consolega chiqaradi.

Agar tekshirilayotgan shart nisbatan sodda bo'lsa, shart amalini ? : ko'rinishida ishlatishimiz mumkun.
Masala 2:

2 ta son kiritilganda kattasini topuvchi programma tuzilsin:

#include <iostream>

using namespace std;

int main(){
  int a, b, max;
  cin >> a >> b;

  max = (a > b) ? a : b;
  cout << "Katta son : " << max;

  return 0;
Enter fullscreen mode Exit fullscreen mode
12
11

Katta son : 12
Enter fullscreen mode Exit fullscreen mode

Bu code ham huddi if kabi vazifa bajarildi.2 son kiritildi va a katta bo'lsa b dan ? - agar chiqar a ni : - aks xolda a kichkina bo'lsa b dan b ni chiqar deyildi va sonlarning kattasi max nomli uzgaruvchiga yuklandi.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs