C++ Conditionals and Loops
Control flow with if statements and loops
If-Else Statements
if (condition) { # check condition
// code if true
} else if (condition2) { # additional check
// code if condition2 true
} else { # default case
// code if all false
}
Switch Statement
switch (expression) { # evaluate expression
case 1: # if expression is 1
// code
break; # exit switch
case 2: # if expression is 2
// code
break;
default: # if no match
// code
}
For Loop
for (int i = 0; i < 10; i++) { # loop 10 times
cout << i; # print index
}
for (int x : array) { # range-based for
cout << x; # print each element
}
While and Do-While
while (condition) { # repeat while true
// code
}
do { # execute at least once
// code
} while (condition); # check after execution
Loop Control
break; # exit loop immediately
continue; # skip to next iteration