DEV Community

Mahamadjon000
Mahamadjon000

Posted on

1

Switch

switch - используется когда нужно проверить переменную.

switch- он сразу заходит и находит ответ.

Мы можем использовать только int и char!.

Дробные числа нельзя!.

Пример кода:

#include <iostream>

using namespace std;

int main()
{

int day = 3;

switch (day) 
{
    case 1:
        cout << "Понедельник" << endl;
        break;
    case 2:
        cout << "Вторник" << endl;
        break;
    case 3:
        cout << "Среда" << endl;
        break;
    case 4:
        cout << "Четверг" << endl;
        break;
    case 5:
        cout << "Пятница" << endl;
        break;
    case 6:
        cout << "Суббота" << endl;
        break;
    case 7:
        cout << "Воскресенье" << endl;
        break;
    default:
        cout << "none" << endl;
}

return 0;
}
Enter fullscreen mode Exit fullscreen mode

Обьяснение кода:

Переменная day проверяется на совпадение с каждым значением case.
Если значения совпадает то код выполняется. break - используется чтобы прервать выполнение и выйти из switch. Иначе программа будет продолжаться. Если не одно не подошло то выполняется default.

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)

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

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay