Focus on control statements
#include <iostream> //Header file for input-output operations
using namespace std; //Allow using standard namespace
int main()
{
cout << "Hello,World!" << endl; //Prints output to console
return 0;
}
|
Statement
|
Description
|
Syntax
|
|
If Statement
|
Executes a statement if a condition is true
|
if (condition) {statement(s);} |
|
If-else Statement
|
Executes one block if the condition is true, another if false
|
if (condition) {statement (s);} else{statement (s);} |
|
Ternary Operator
|
A shorthand for if-else
|
variable = (condition) ? value if true : value if false; |
|
Switch Statement
|
Selects a case to execute based on a variable's value
|
switch(expression) {case value: statement; break; default: statement;} |
|
Statement
|
Description
|
|
break
|
Exits a loop or switch statement.
|
|
continue
|
Skips the rest of the loop iteration and continues to the next.
|
|
exit()
|
Terminates the program immediately.
|
int num;cout << "Enter a number: ";cin >> num;cout << "You entered: " << num << endl;|
Loop Type
|
Description
|
Syntax
|
|
while loop
|
Repeats a block of code while a condition is true.
|
while (condition) {statement(s);} |
|
do-while loop
|
Executes at least once, then repeats while a condition is true.
|
do {statement(s);} while (condition); |
|
for Loop
|
A compact loop with initialization, condition, and increment.
|
for (int; condition; update) { statement(s); } |
|
Types
|
Description
|
|
Counter-Controlled Loop
|
Executes a set number of times.
|
|
Sentinel-Controlled Loop
|
Runs until a special value is entered.
|
|
Flag-Controlled Loop
|
Uses a bool flag to control execution.
|
|
Nested Loops
|
A loop inside another loop.
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.