Module 2-Control Statements Cheat Sheet

A Programming › C cheat sheet in English by lukenelson

Focus on control statements

2,677 views 387 downloads 2 Mar 2025 History

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
Descri­ption
Syntax
If Statement
Executes a statement if a condition is true
if (condi­tion) {state­men­t(s);}
If-else Statement
Executes one block if the condition is true, another if false
if (condi­tion) {statement (s);} else{s­tat­ement (s);}
Ternary Operator
A shorthand for if-else
variable = (condi­tion) ? value if true : value if false;
Switch Statement
Selects a case to execute based on a variab­le's value
switch­(ex­pre­ssion) {case value: statement; break; default: statem­ent;}

Loop Control Statements

Statement
Descri­ption
break
Exits a loop or switch statement.
continue
Skips the rest of the loop iteration and continues to the next.
exit()
Terminates the program immedi­ately.

IF_ELS­E-S­WITCH

IF_ELSE-SWITCH

WHILE LOOP

WHILE LOOP

Input and Output

int num;
cout << "­Enter a number: ";
cin >> num;
cout << "You entered: " << num << endl;

Repetition Statements (Loop)

Loop Type
Descri­ption
Syntax
while loop
Repeats a block of code while a condition is true.
while (condi­tion) {state­men­t(s);}
do-while loop
Executes at least once, then repeats while a condition is true.
do {state­men­t(s);} while (condi­tion);
for Loop
A compact loop with initia­liz­ation, condition, and increment.
for (int; condition; update) { statem­ent(s); }

Types of Loop

Types
Descri­ption
Counte­r-C­ont­rolled Loop
Executes a set number of times.
Sentin­el-­Con­trolled Loop
Runs until a special value is entered.
Flag-C­ont­rolled Loop
Uses a bool flag to control execution.
Nested Loops
A loop inside another loop.

FOR-BREAK

FOR-BREAK

CONTIN­UE-­NESTED

CONTINUE-NESTED

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.