C++ Control
Statements
Flowcharts, Syntax & Programs for Each Type
Introduction
• Control statements in C++ define the flow of execution.
• Types:
• - Selection Statements (if, if-else, switch)
• - Iteration Statements (for, while, do-while)
• - Jump Statements (break, continue, goto, return)
Selection: If-Else Statement
• Used for decision-making.
• **Syntax:**
• if (condition) {
• // Code when true
• } else {
• // Code when false
• }
• **Example Program:**
• int num = 10;
• if (num > 0) {
• cout << 'Positive';
• } else {
• cout << 'Negative';
• }
Selection: Switch Statement
• Used for multiple conditions.
• **Syntax:**
• switch (expression) {
• case value1: // Code
• break;
• default: // Code
• }
• **Example Program:**
• int day = 2;
• switch (day) {
• case 1: cout << 'Monday'; break;
• case 2: cout << 'Tuesday'; break;
• default: cout << 'Other Day';
• }
Iteration: For Loop
• Used for repeating a block of code.
• **Syntax:**
• for (init; condition; update) {
• // Code
•}
• **Example Program:**
• for (int i = 0; i < 5; i++) {
• cout << i;
•}
Iteration: While Loop
• Executes while condition is true.
• **Syntax:**
• while (condition) {
• // Code
• }
• **Example Program:**
• int i = 0;
• while (i < 5) {
• cout << i;
• i++;
• }
Iteration: Do-While Loop
• Executes at least once before checking condition.
• **Syntax:**
• do {
• // Code
• } while (condition);
• **Example Program:**
• int i = 0;
• do {
• cout << i;
• i++;
• } while (i < 5);
Jump Statements
• Alter the flow of execution.
• - break: Exits a loop or switch
• - continue: Skips an iteration
• - goto: Jumps to a labeled statement
• - return: Exits a function
• **Example:**
• for (int i = 0; i < 5; i++) {
• if (i == 3) break;
• cout << i;
•}
Conclusion
• C++ Control Statements help manage execution flow.
• - Selection: if-else, switch
• - Iteration: for, while, do-while
• - Jump: break, continue, goto, return
• Mastering these helps in writing efficient code!