switch statement

shoxjaxon2008

Shoxjaxon2008

Posted on April 4, 2023

switch statement

Salom dasturchi bugun siz bilan switch nima ekanligi va qay holda ishlatiladi . Va if else statement dan nimasi bilan farq qilishi haqida gaplashamiz .

switch - bu if ga o'xshayd . Lekin har doim ham ishlatilmiydi . switch ni o'rniga if ni ham ishlatsa bo'ladi ammo ba'zi bir holatlarda kerak bo'ladi . Shuningdek uni biror son ga tenglayotgan paytimizda ishlatishimiz kerak . Uni yozilish ko'di if dan ancha farq qiladi ammo qulay va oson .
Buni hozir ko'd orqali ham ko'rib tushunib olishingiz mumkin .

#include <iostream>
using namespace std ;

int main2( ) {

int son ;
cin >> son ;

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

  }
# Bu yerda biza kiritayotgan sonimiz 10 va 20 maga tengmi yo'qmi shuni tekshiradi 
  return 0 ;
}
Enter fullscreen mode Exit fullscreen mode

Agar teng bo'lmay qoladigan bo'lsa "none" chiqadi .

Bu dastur huddi parol kirgizishga o'xshaydi .
Tushuntirish
Bu yerda case - bu kalit .
break - bu tugaganini anglatadi ya'ni o'sha bo'limni oxirida kelsa ko'd tugagan bo'ladi .
default - ese if statement dagi else ya'ni aks holda degan ma'noni anglatadi .
Bu qanday ishlatilishini ko'd orqali ko'rib oling .
Birinchi kalit 10 ;
Ikkinchi kalit 20 ;
Switch - uni ichiga if ga o'xshab shart berilmaydi . Faqat variables kiritiladi .

Bu maqolamdan ancha narsa bilib oldingiz degan umiddaman . Keyingi maqolalargacha hayr sog' bo'ling .

💖 💪 🙅 🚩
shoxjaxon2008
Shoxjaxon2008

Posted on April 4, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

switch statement
programming switch statement

April 4, 2023