Codectionary / Developer documentation / C++

C++ Conditions & Loops

C++ uses if, else if, and else for decisions. Range-based for loops iterate directly over the elements of a container.

Syntax

if (score >= 50) { /* pass */ }
for (const auto& item : items) { /* use item */ }

Examples

Classifying scores

A small, runnable example of this syntax.

#include <iostream>
#include <vector>

int main() {
  std::vector<int> scores{72, 49, 90};
  for (int score : scores) {
    std::cout << (score >= 50 ? "pass" : "retry") << "\n";
  }
}

Best practices

  • Use braces even for one-line branches and loops; they prevent errors when the body changes later.
  • Keep examples small while learning, then combine the idea with a real project.

At a glance

Purpose
Systems and general-purpose software
File extension
.cpp ยท .hpp
Runs in
Compiled native applications
Usually used with
Compiler and standard library

Specifications & further reading

Related C++ documentation