If imbricat

Un if poate conține alt if în interior — se numesc instrucțiuni imbricate (nested).

nested_if.cpp
int varsta, nota;
cin >> varsta >> nota;

if (varsta >= 10) {
  if (nota >= 5) {
    cout << "Elev promovat" << endl;
  } else {
    cout << "Elev corigent" << endl;
  }
} else {
  cout << "Prea mic pentru scoala" << endl;
}

Problemă: triunghi

int a, b, c;
cin >> a >> b >> c;

if (a + b > c && a + c > b && b + c > a) {
  if (a == b && b == c) {
    cout << "Echilateral" << endl;
  } else if (a == b || b == c || a == c) {
    cout << "Isoscel" << endl;
  } else {
    cout << "Scalene" << endl;
  }
} else {
  cout << "Nu formeaza triunghi" << endl;
}
Prea mult nesting face codul greu de citit. Dacă ai mai mult de 3 nivele, consideră refactorizarea folosind funcții sau &&.