DEV Community

Sherzod5048
Sherzod5048

Posted on

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.

Oldest comments (0)