Cheatography
https://cheatography.com
Focus on control statements
Basic Structure
#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;
}
|
Selection Statements
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;}
|
Loop Control Statements
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. |
|
|
Input and Output
int num;
cout << "Enter a number: ";
cin >> num;
cout << "You entered: " << num << endl;
|
Repetition Statements (Loop)
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 of Loop
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. |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by lukenelson