Bo2SS

Bo2SS

3 Program Flow Control Methods

Course Content#

if Statement, switch Statement#

  • (C Language Relational Operators)
    • 0 → false; 1 → true
    • false 👉 0, NULL(null), '\0'
    • <= → =<
    • ! Negation (Relational Operator)
      • Be careful to distinguish: Bitwise NOT operator ~ (Bitwise Operator)
      • ⭐!! Double Negation: Logical Normalization
        • Unify all true values to 1, false values remain 0
  • Sequential Structure
    • Omitted
  • Branch Structure

IF Statement#

  • Image
  • (Expression)

    • Any expression has a logical return value
    • For the assignment expression a = 123;
      • The return value is the value of variable a, which is 123
  • {Code Block;}

    • Understand what a statement is?
      • Single Statement: A statement ending with “;”
        • int a = 1, b = 234; // This is a statement, “,” separates into one expression
      • Empty Statement: Only a “;”, has no effect
      • Compound Statement
        • Uses curly braces
        • That is the code block👆
  • if else Statement

    • Can be replaced by the ternary operator (conditional expression)
      • ...? ...: ...;

SWITCH Statement#

  • Image
  • Variable a must have a clear unique integer value as a mapping!

    • Letters also have such mappings: ASCII code
  • After entering case, subsequent code is executed in order

    • Until encountering break, continue, or the end of the structure~
    • No need to check if the subsequent case is satisfied
  • Default Branch

    • When none of the cases are satisfied, it will enter, equivalent to the else of if else statement

CPU Branch Prediction#

Introduced from LeetCode problem - Palindrome Integer

  • Image

The captain's highly efficient code is as follows:

  • Image
  • The red box implements x<0

    • Used to filter integers less than 0, negative numbers are definitely not palindrome integers
  • This statement appears in the operating system kernel:

  • Image
  • !!(x) Logical normalization, the result can only be 1 or 0

    • Frequent occurrences will inform the CPU of the processing program

⭐CPU Execution Process Analysis

  • C Language Program Execution Process: Compiled to generate executable file → Loaded into memory → CPU executes
  • Cache
    • Level 1 Cache: Fastest speed, but smallest capacity
  • Comparison of CPU Instruction Execution Methods
    • Early Serial (Addressing, Instruction Pre-fetching, Writing Data, Executing, Writing Back to Memory)
    • Today's Parallel
    • The schematic diagram is as follows:
      • Image
      • For 5 instructions

        • Serial method requires 5 * 5 = 25 clock cycles
        • Parallel method only requires 5 + 4 = 9 clock cycles
      • When there are many instructions, efficiency increases nearly 5 times

  • Therefore! CPU prefers sequential structures
    • Dislikes branch structures (should use if else statements sparingly)
    • Branch prediction issues
    • For parallel methods, the CPU does not know what the next step of the branch structure is, which step should be pre-loaded?
      • The CPU actually loads the next step randomly
        • Therefore, if the prediction is wrong, the subsequent work is wasted, and it needs to return to reprocess
  • And __builtin_expect() does exactly that: tells the CPU which branch situation is more likely to occur!!!

Loop Structure#

  • WHILE Statement
    • while () {Code Block;}
      • Check first, then execute
    • do {Code Block;} while (Expression); ← Pay attention to the semicolon!
      • Executes at least once
  • FOR Statement
    • for(Initialization; Loop Condition; Post-execution Operation) {Code Block;}
    • Makes the loop very elegant, consisting of three parts
    • All three parts can be omitted
      • For example: for(; ; );, which is equivalent to while (1);
      • The above is an infinite loop, doing nothing

In-class Exercise#

  • Image
  • Remember to check for redundant condition checks

  • When input is invalid (e.g., "q"), it falls into an infinite loop

    • Solutions and thought processes are detailed below: Thought Point 1
  • Code

  • Image
  • Image

  • Image

    • Familiarize yourself with the characteristics of switch statement endings
    • Code
  • Image
  • Image
  • Code

  • Image

​ For the while method, if the first check succeeds, it is no different from the do...while method

  • Image
  • Code

  • Image

Highlight Notes#

  • ⭐Perform redundant checks on conditional statements (trimming), usually resulting in more concise code
    • For all cases where the expression is checked for 0, you can directly use if (!Expression)
      • For if (Expression) single statement, you can utilize logical AND
        • (Expression) && Single Statement
        • For example, i && printf(" "); to output a space in all loops except the first
        • The single statement cannot be {Code Block;}
    • Replace if...else with the ternary operator
  • Generally, define the loop variable i in the initialization part: int i = 0
    • No need to place it in advance
      • More standardized and concise: define the variable before use, do not place it too far away
    • Scope issues
      • The variable is only used within the loop
  • Is ++i faster than i++?
    • Use ++i whenever possible
      • From the perspective of the function stack: 👇
      • ++i directly pushes the value of i+1 onto the stack
      • Whereas i++, first pushes i onto the stack and then pushes i+1

Code Demonstration#

Branch Structure (6.struct_program.cpp)#

  • Image
  • a - b can replace a == b for equality check

  • if else can be replaced by the ternary operator

  • Image
  • Output 1: false

    • What is the precedence of a++ and &&?
      • ++ has higher precedence than &&
      • Why didn’t the () outside a++ raise the precedence of ++ over &&?
        • It is prioritized
        • The output of false here is unrelated~
          • For the if check, it should first check a, then go to ++
          • The value of the a++ expression does not include the incremented a
  • Output 2: a = 1, b = 0

    • ⭐Logical AND -- Short-circuit Rule
      • If there is a false value in front, it will not proceed further [Smart approach]
  • Image
  • (Continuing from a = 1, b = 0)

  • Output 1: true

  • Output 2: a = 2, b = 0

    • ⭐Logical OR -- Long-circuit Rule
      • If there is a true value in front, it will not proceed further
      • To execute the subsequent expression, all previous expressions must be false
  • Image
  • Output method with space as a separator (no space at the end)

    • Method 1: If it is not the first loop, output a space before
      • First better to obtain: i == 0
      • Can optimize line 43: i && printf(" ");
    • Method 2: If it is not the last loop, output a space after
  • rand()

    • Needs to include <stdlib.h>

    • Outputs a random unsigned number

      • rand() % 100 can randomly output numbers from 0 to 99
    • ⭐Actually, it is fixed randomness

      • Image
      • Set the random seed through srand()

        • srand(time(0))

          • time(0)
            • Get the current time, needs to include <time.h>
            • Accurate to seconds, the number of seconds since 1970.1.1
        • It can be seen that the value is changing, but there is no true randomness in computers

          • Image
  • Optimized Version

    • The above code has three places that can be simplified! Both if statements can be removed

    • Image
    • ⭐~

      1. Bitwise operation and modulus conversion: % 2 is equivalent to & 1
        • % (2 ^ n) is equivalent to & (2 ^ n - 1)
      2. +1 is equivalent to true value 1
      3. Short-circuit rule of logical AND

Loop Structure (7.cpp)#

Check Palindrome Integer (Decimal)

  • Image
  • Remember to make special checks for negative numbers!

  • What if you want to check for palindrome integers in binary?

    • Just change all the circles to 2

      • Computers actually store data in binary at a low level
      • Regardless of the base, palindrome can be checked
    • Palindrome integer check in base

      • Image

Calculate the Number of Digits (Decimal)

  • A subtle comparison between while loop and do...while loop
    • The key is whether the condition holds for the first loop~
      • Image
      • When inputting a non-zero number, digit and digit2 have no difference
    • When inputting the number 0, there is a difference
      • Image
      • Therefore, to check the number of digits when there is a 0
        * Use do...while
        * Or make a special check

Additional Knowledge Points#

  • switch Statement

    • Variables cannot be declared after case
    • Does the default at the end also need a break?
      • It can be optional; adding break is for style consistency
    • Python does not support switch statements
  • In C language, what happens if there is no return statement at the end of the main function?

    • Starting from C99, it has little effect; the standard requires it to be equivalent to automatically adding return 0;
    • Before C99, if control reaches the end of the main function without return, it is undefined behavior.
  • Floating-point equality check

    • Do not use == directly, as it may be inaccurate; use the difference to check if it is less than a certain small value
  • __builtin_expect() has more than 6 siblings

  • Image
  • Bit weight determines the multiples of left and right shifts

    • In decimal, left shift by one bit, *10
    • In binary, left shift by one bit, *2

Thought Points#

  • 💡 (Solution) For 5.if.cpp, i.e., in-class exercise 1. Inputting an invalid value, such as the letter ‘q’, will lead to an infinite loop

    • Is it related to the ASCII code of ‘q’?

      • No relation~
      • Outputting ‘q’ in %d format looks like an address; it is fixed during a single run, which should be a value randomly assigned when initializing n
        • If a valid value is input first and then an invalid value, this value is the previous n value
        • Because no value is actually read to change n
    • Cannot read the value ‘q’

      • The return value of scanf is 0
      • But it does not move to the next input, similar to the infinite loop event of %[^\n] encountered in the first lecture
    • ⭐Of course! You can also use getchar() to swallow this invalid value

      • Is inputting qw invalid?
        • Yes, it needs getchar() to swallow twice
    • Modified Code

      • Image
      • Placing getchar() inside if(!ret) is better, it only swallows characters when reading invalid characters

        • The code above: getcharwill swallow whitespace and invalid characters

        • The code below: will only swallow invalid characters

          • Image
          • To demonstrate that the swallowed characters have no whitespace, only invalid characters, printf function is added
  • Image
  • 💡 Similarly, for 5.if.cpp (with loops, needs manual stopping), when redirecting standard output with >output, there will be issues with output not being written to the redirected file

Ctrl+C: The default interrupt key in Linux; when this key is pressed, the system sends an interrupt signal to the running program and shell.
Ctrl+D: The standard input/output EOF in Linux. When this symbol is encountered in devices using standard input/output, the program considers it has reached the end of the file, thus ending input or output.

  • If there is invalid input leading to a large amount of output, Ctrl+C can also write
    • Question: Is this because the buffer is insufficient, forcing output to the output file in advance?
      • This is the correct understanding
    • Question: So is the output written to the output file just the overflow output, or all output before Ctrl+C?
      • Overflow output
  • Code 5.switch.cpp: Why does inputting letters cause an infinite loop?
    • Same as 5.if.cpp, see Thought Point 1

Tips#

  • OJ Problem Solving Method
    • Write code in vim
    • Use cat to display and copy the code
    • Submit
  • Classic Problem: Given year, month, and day, determine if it is reasonable
    • Is it necessary to use so many if else?
    • You can use space to exchange time: store the number of days for each month in an array
  • Refer to the reference book Chapter 6 Section 6.4

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.