DEV Community

Abror
Abror

Posted on

C++ intro for starter 8

Assalomu alaykum!

Bugungi darsda biz o'tgan mavzuda if&else mavzusining davomi nested if ya'ni ichma-ich if va switch&case haqida gaplashamiz.

Bu qanaqa bo'ladi? Demak boshladik,

#include <iostream>
using namespace std;

int main()
{
 int s;
  cin >> s;

  if (s > 0)
     {
    if (s % 2 == 0)
      cout << "Musbat juft son";
    else
      cout << "Musbat toq son";
      } 
 else if (s < 0)                                                                         
    {
    if (s % 2 == 0)
      cout << "Manfiy juft son";
    else
      cout << "Manfiy toq son";
     } 
  else
    cout << "Son nolga teng ";

  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Ko'rib turganingizdek bizda ichma-ich if&elsehosil bo'lmoqda va siz buni yuqorida kod orqali osongina tushunishingiz mumkin va siz uchun bu juda oson bo'ladi.

Endigi navbatda switch&case haqida gaplashamiz.

#include <iostream>
using namespace std;
int main(){
witch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
return 0;
}
Enter fullscreen mode Exit fullscreen mode

Siz yuqoridagi switch&casening qisqa qilib aytganda formulasi deya atashimiz mumkin.

Undan tashqari quyidagi kodda ham aniqroq tushunishingiz mumkin.

#include <iostream>
using namespace std;

int main(){
int day = 4;
switch (day) {
  case 1:
    cout << "Monday";
    break;
  case 2:
    cout << "Tuesday";
    break;
  case 3:
    cout << "Wednesday";
    break;
  case 4:
    cout << "Thursday";
    break;
  case 5:
    cout << "Friday";
    break;
  case 6:
    cout << "Saturday";
    break;
  case 7:
    cout << "Sunday";
    break;}
return 0;
}

Enter fullscreen mode Exit fullscreen mode

Bugun shu mavzular haqida gaplashdik.@dawroun

E'tiboringiz uchun rahmat!

Top comments (0)