DEV Community

Cover image for C++ Switch (Tanlash Operatori).
islomAli99
islomAli99

Posted on

3 1

C++ Switch (Tanlash Operatori).

Assalamu alaykum bugun siz bilan C++ dasturlash tilida tanlash operatori bilan tanishib chiqamiz. Tanlash operatoridan foydalanishimiz uchun switch kalit so'zidan foydalanamiz. Ushbu operatorni qo'llamasdan shart operatori dan foydalansangiz ham bo'ladi, ammo switch ancha ishingiz yengillashadi. Demak kodlarni analiz qilishni boshlaymiz.

Switch tanlash operatorining sintaksis ko'rinishi.

#include <iostream>
using namespace std;

int main()
{


switch(qiymat) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

return 0;
}
Enter fullscreen mode Exit fullscreen mode
  • switch berilgan Ifoda bir marta baholanadi.

  • Ifodaning qiymati har birining qiymatlari bilan taqqoslanadi case - so'zi o'rqali.

  • Agar mos keladigan bo'lsa, tegishli kod bloki bajariladi.

  • break tanlash operatorini ishini yakunlaydi.

  • default kalit so'zlar ixtiyoriy bo'lgan qiymat uchun ishlaydi. yuqoridagi birontasi mos kelmasi demak default ishlaydi.

Pastda misol berilgan 3 ta son bor usha sonni 10, 20, 30 teng bolsa bu son 10, 20, 30 teng degan yozuv chiqadi va shu 3 ta sonnga togri kelmasa keyin default ga kiradi, va none degan yozuv chiqadi.

Misol uchun:

#include <iostream>
using namespace std;

int main()
{

int son;
cin >> son;

switch(son)
{
 case 10:
 cout << "Bu son 10 ga teng";
 break;

 case 20:
 cout << "Bu son 20 ga teng";
 break;

 case 30:
 cout << "Bu son 30 ga teng";
 break;

 default:
  cout << "none";
  break;


return 0;
}
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay