DEV Community

Munisa Kuranbekova
Munisa Kuranbekova

Posted on

Switch statement haqida

Assalomu aleykum! Bugungi postimiz switch statement haqida bo'ladi.
Switch - ifga o'xshash bo'lgan gramatika bo'lib , ifdan soddaroq tuzilgan. Ularning asosiy farqlaridan biri shuki , if orqali hamma datatiplarni ishlatish mumkin bo'lsa, switch statementda faqat int va chardn foydalanish mumkin.
Switch expression
{
case Constant 1:
statement 1
break;
case Constant 2:
statement 2
break;
case Constant 3:
statement 3
break;
}

Yuqorida yozilgan strukturamiz switchning syntaxi hisoblanadi.Kodga o'tadigan bo'lsak:

#include <iostream>

using namespace std;

int main()
{  

    int son;
    cin>> son;

switch(son)
{
case 1:
cout << " One " << endl;

case 2:
cout << " Two " << endl;

case 3:
cout << " Three " << endl;

case 4:
cout << " Four " << endl;

 default:
cout << "none" << endl;   

    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Ushbu kodni run qiladigan bo'lsak consolega kodimizga to'g'ri kelgan sonlarni chop etadi. To'g'ri javobning bir o'zi kerak bo'lsa , har bitta cout komandasining tagidan break; deb yozib chiqishimiz zarur.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

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

Okay