Show Menu
Cheatography

Module 2-Control Statements Cheat Sheet by

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
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 variable'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

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

CONTIN­UE-­NESTED

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          C++ Pointers cookbook Cheat Sheet
          Modern C++ 17 Standard Library Features Cheat Sheet
          C# CheatSheet Cheat Sheet

          More Cheat Sheets by lukenelson

          Module 1-Logic Formulation Cheat Sheet