DEV Community

Cover image for C++ Switch (Tanlash Operatori).
Islamali Akhmadjanov
Islamali Akhmadjanov

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

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

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

Okay