Chapter 4.1 - Control Structure (1)

Chapter 4: Control Structures (Part 1)

Page 1
  • Chapter Introduction: Overview of control structures in programming.
Page 2: Learning Outcome
  • Understanding of Control Structures.
  • Usage and Types of Operators in C.
  • Structure and implementation of select statements: if and switch statements.
  • Structure and implementation of null statements.
Page 3: Control Structures
  • Defined as the flow of execution in a program.
  • Statements in a program typically execute sequentially, one after the other.
  • Control transfer allows specification of the next statement to execute.
Page 4: Types of Control Structures
  • Research by Böhm and Jacopini indicated three primary control structures:
    • Sequence Structure: Executes statements in a specific order.
    • Selection Structure: Makes decisions based on conditions.
    • Iteration Structure: Executes statements repeatedly based on a condition.
Page 5: Sequence Structure
  • Execution is straightforward; each statement runs in the order written.
  • Flowcharts are vital to visualize and develop algorithms.
  • Visual representation aids in understanding how control structures function.
Page 6: Flowcharts Basics
  • The rectangle symbol represents an action (calculation, input, output).
  • Flowlines indicate the order of actions.
  • When creating flowcharts, start with a "Begin" symbol and end with an "End" symbol.
  • For partial flowcharts, connector symbols (small circles) may be used instead of rounded rectangles.
Page 7: Selection Statements in C
  • Single-selection statement (if): Executes actions when a condition is true.
  • Double-selection statement (if-else): Executes different actions based on the truth value of a condition.
  • Multiple-selection statement (switch): Executes one of many actions based on an expression's value.
Page 8: Iteration Structures
  • Control structures that perform tasks repeatedly include:
    • while
    • do…while
    • for
Page 9: Summary of Control Statements
  • Programs combine various control statements appropriately.
  • Single-entry/single-exit helps in building clear programs.
  • Control statements can be linked by connecting their exit to another's entry (stacking) or through nesting statements.
Page 10: Operators Overview
  • Types of operators in C:
    • Assignment Operator
    • Increment and Decrement Operator
    • Relational Operator
    • Logical Operator
Page 11: Assignment Operator
  • C offers assignment operators for concise expressions.
  • For example, c = c + 3; can be abbreviated as c += 3; which stores the result in variable c.
Page 12: Assignment Operator Examples
  • Sample assignment expressions:
    • c += 7; makes c equal to 10 (if c was initially 3).
    • d -= 4; makes d equal to 1 (if d was 5).
    • e *= 5; makes e equal to 20 (if e was 4).
    • f /= 3; alters f to 2 (if f was 6).
    • g %= 9; changes g to 3 (if g was 12).
Page 13: Increment and Decrement Operators
  • Unary increment (++) and decrement (--) operators:
    • ++a increases a by 1 and uses the new value.
    • a++ uses the current value and then increases it by 1.
    • Similarly for decrement operations (e.g., --b).
Page 14: Pre and Post Increment/Decrement
  • Preincrement: ++var is used before the variable.
  • Postincrement: var++ is used after the variable.
  • Operators should be adjacent to their operands without spaces for clarity.
Page 15: Pre/Post Incrementing Example
  • Example demonstrating preincrement and postincrement:
#include <stdio.h>
int main(void) {
    int c = 5;
    printf("%d\n", c);
    printf("%d\n", c++);
    printf("%d\n\n", c);
    c = 5;
    printf("%d\n", c);
    printf("%d\n", ++c);
    printf("%d\n", c);
}
  • Shows the difference in behavior between pre and post incremental operators.
Page 16: Operator Precedence
  • Operators and their grouping types:
    • Postfix: ++ (postfix) (right to left)
    • Prefix: ++ (prefix) (right to left)
    • Other operators include additive, relational, and equality operators.
Page 17: Relational Operators
  • Used to compare values; return true (1) or false (0).
  • Common relational operators include:
    • ==: Equal to
    • !=: Not equal to
    • <: Less than
    • >: Greater than
    • <=: Less than or equal to
    • >=: Greater than or equal to
Page 18: Logical Operators
  • Perform logical operations on boolean values.
  • Common logical operators:
    • &&: Logical AND
    • ||: Logical OR
    • !: Logical NOT
Page 19: Selection: IF Statement
  • Selection statements decide between alternative actions.
  • Example: Check if a student's grade meets a threshold to print an outcome.
Page 20: Example of IF Statement
  • If structure in C language:
if (grade >= 60) {
    printf("Passed");
}
  • Condition must be defined before applying the if statement.
Page 21: Indentation in IF Statements
  • Indentation improves readability and structure.
  • Compiler ignores white-space characters, but they are critical for developers.
Page 22: IF Statement Flowchart
  • Flowchart shows decision points with diamond symbols for conditions.
  • Flowlines indicate pathways based on true or false outcomes.
Page 23: Important Flowchart Symbols
  • Decision symbol (diamond) indicates a condition to evaluate.
  • Two paths (true/false) emerge from the diamond for respective outcomes.
Page 24: IF…ELSE Statement
  • Introduces alternate actions for true and false conditions.
  • Example: Print different messages based on grade boundaries.
Page 25: C Language Implementation of IF…ELSE
  • Example structure in C:
if (grade >= 60) {
    printf("Passed");
} else {
    printf("Failed");
}
Page 26: IF-ELSE Statement Flowchart
  • Demonstrates decision flow between true and false outcomes in a clear manner.
Page 27: Conditional Expressions
  • C's conditional operator (ternary) format:
printf((grade >= 60) ? "Passed" : "Failed");
  • Ensure similar operand types to prevent errors in expressions.
Page 28: Nested IF…ELSE Statements
  • Allows checking multiple conditions to categorize values.
  • Example structure for grading system:
if (grade >= 90) {
    printf("A");
} else if (grade >= 80) {
    printf("B");
} ... 
Page 29: C Implementation of Nested IF…ELSE
  • Complete nested structure in C showing multiple grade checks.
Page 30: C Code of Nested IF…ELSE Statements
  • Detailed nested if/else conditions outlined in C programming.
Page 31: IF-ELSE IF Statements
  • Simplified structure for multiple conditions:
if (grade >= 90) {
    printf("A");
} else if (grade >= 80) {
    printf("B");
} ... 
Page 32: Blocks and Compound Statements
  • Use braces {} to include multiple statements in if statements for clearer structures.
  • Essential to avoid mistakes with indentation and nested control flows.
Page 33: Importance of Braces in Statements
  • Using braces is crucial in controlling the flow of execution. Improperly closed blocks may lead to execution outside intended scope.
Page 34: Selection: SWITCH Statement
  • Used for multiple selections based on different values.
  • Switch statement facilitates better organization than nested if-else.
Page 35: Structure of Switch Statement
  • Example of a switch statement in C:
switch (choice) {
    case 1: ... 
    case 2: ... 
    default: ... 
}
Page 36: Example of Switch Statement
  • Complete usable example demonstrating choices and actions depending on user input.
Page 37: Handling Grades with SWITCH Statement
  • Illustrates usage through user input and responses for grades.
Page 38: Default Case in SWITCH
  • Enforces fallback for unmatched cases, emphasizing importance of default clauses for robust coding.
Page 39: SWITCH Statement Flowchart
  • Visualizes control flow with multiple case actions.
Page 40: Ignoring Whitespace Characters
  • Method to handle newline, tab, and space characters in input processing to ensure clean input handling.
Page 41: Integral Expressions in SWITCH
  • Constraints around using switch with only constant integer expressions. Importance of single vs double quotes in C programming.
Page 42: Null Statements
  • A null statement represented as ;, serves as a placeholder in programming logic without functional execution.
  • Often used where a statement is syntactically needed but no operation is required.
Page 43: Conclusions
  • Decision-making capability in programming is facilitated by control structures, influencing program flow.
  • The switch statement simplifies handling multiple selection conditions, enhancing readability.
  • Understanding operators essential for formulating effective control statements in C.
  • Code clarity and maintenance enhanced through structured control statements.
  • Efficient execution is achieved by filtering relevant code execution through control statements.
Page 44: Thanks
  • Gratitude expression for engagement with the material.