Fundamentals 17 min read

Master C Operators and Control Flow: A Complete Guide

This article explains C language operators—including arithmetic, assignment, increment/decrement, comparison, logical, bitwise, comma, and ternary operators—covers operator precedence and associativity, and provides detailed examples of conditional and loop statements such as if/else, switch, for, while, and do/while constructs.

AI Cyberspace
AI Cyberspace
AI Cyberspace
Master C Operators and Control Flow: A Complete Guide

Operators

Operators are symbols that tell the compiler to perform specific arithmetic or logical operations.

Operand : the smallest data unit participating in evaluation, such as a constant, variable, or function.

Expression : a combination of operands and operators that can be evaluated.

Precedence : operators with higher precedence are evaluated first.

Associativity : determines the direction of evaluation; left‑associative evaluates from left to right, right‑associative from right to left.

Parentheses () can change the evaluation order in an expression; when operators share the same precedence, associativity decides the order.

Arithmetic Operators

NOTE: The operand of the remainder operator must be an integer.

Assignment Operators

Assignment expression: connects a variable and an expression using an assignment operator.

Multiple assignment: V1 = V2 = ... = Vn = expression assigns from right to left.

Assignment operators are divided into:

Basic assignment: = assigns a value to a variable.

Compound assignment: combines a basic assignment with arithmetic, bitwise, etc.

NOTE:

The left side of an assignment must be a variable; the right side must be an expression.

The value of an assignment expression is the value stored in the left‑hand variable.

The data type of the assignment expression is the type of the left‑hand variable.

If the right‑hand expression’s type differs, it is implicitly converted before assignment.

Increment and Decrement Operators

i++

: value is used first, then i = i + 1. ++i: i = i + 1 is performed first, then the value is used. i--: value is used first, then i = i - 1. --i: i = i - 1 is performed first, then the value is used.

NOTE: Increment/decrement operators can only be applied to variables, not constants or expressions.

Comparison Operators

Assuming A = 10 and B = 20:

NOTE: Comparison results are 0 (false) or 1 (true). C uses 0 for false and non‑zero for true; C++ introduces a dedicated bool type.

Logical Operators

A logical expression combines operands with logical operators.

Assuming A = 1 and B = 0:

When multiple operators appear, evaluation order is arithmetic → relational → logical.

Bitwise Operators

Bitwise operators work on individual bits. Truth tables for &, |, ^, and ~ are shown below:

Example: A = 60 (00111100),

B = 13 (00001101)
A & B = 00001100
A | B = 00111101
A ^ B = 00110001
~A = 11000011

Comma Operator

The comma operator evaluates a sequence of expressions from left to right; the value of the whole expression is the value of the last expression.

NOTE: The comma operator has the lowest precedence and is used to simplify code.

// C/C++ evaluates expressions 1, 2, ..., n in order
// The value of the whole comma expression is the value of expression n
expression1, expression2, expression3, ..., expressionn

Operator Precedence

Operators with higher precedence appear higher in the table and are evaluated first.

Structured Programming

Structured programming breaks a complex problem into controllable, understandable phases, making code easier to write, read, modify, and maintain.

In 1966, Boehm and Jacopini identified three basic structures: sequence, selection, and iteration.

Sequence : a series of statements executed in order.

Selection : chooses a branch based on a condition (e.g., if/else, switch).

Iteration : repeats a block of code (loops).

Conditional Statements

if/else

if statement

#include <stdio.h>

int main()
{
    /* local variable */
    int a = 10;
    /* check condition */
    if (a < 20)
    {
        /* true branch */
        printf("a is less than 20
");
    }
    printf("a = %d
", a);
    return 0;
}

if/else statement

#include <stdio.h>

int main()
{
    int a = 100;
    if (a < 20)
    {
        printf("a is less than 20
");
    }
    else
    {
        printf("a is greater than 20
");
    }
    printf("a = %d
", a);
    return 0;
}

if/else‑if/else statement

#include <stdio.h>

int main()
{
    int a = 100;
    if (a == 10)
    {
        printf("a is 10
");
    }
    else if (a == 20)
    {
        printf("a is 20
");
    }
    else if (a == 30)
    {
        printf("a is 30
");
    }
    else
    {
        printf("no match
");
    }
    printf("a = %d
", a);
    return 0;
}

Nested if

#include <stdio.h>

int main()
{
    int a = 100;
    int b = 200;
    if (a == 100)
    {
        if (b == 200)
        {
            printf("a is 100 and b is 200
");
        }
    }
    printf("a = %d
", a);
    printf("b = %d
", b);
    return 0;
}

switch

switch(expression 1)
{
    case constant1: statements1; break;
    case constant2: statements2; break;
    ...
    case constantN: statementsN; break;
    default: statementsDefault;
}

Execution steps:

Evaluate the switch expression.

Compare its value with each case constant in order.

When a match is found, execution starts at that case and proceeds sequentially.

If no case matches, the default block (if present) is executed.

NOTE : default is optional; without it, no action occurs when there is no match. Use break to exit the switch after a case.

#include <stdio.h>

int main()
{
    char grade = 'B';
    switch(grade)
    {
        case 'A':
            printf("Excellent!
");
            break;
        case 'B':
        case 'C':
            printf("Good job
");
            break;
        case 'D':
            printf("Passed
");
            break;
        case 'F':
            printf("Try again
");
            break;
        default:
            printf("Invalid grade
");
    }
    printf("Your grade is %c
", grade);
    return 0;
}

Nested switch

switch(ch1)
{
    case 'A':
        printf("Outer A
");
        switch(ch2)
        {
            case 'A':
                printf("Inner A
");
                break;
            case 'B':
                // inner B code
                break;
        }
        break;
    case 'B':
        // outer B code
        break;
}

Conditional (Ternary) Operator

Exp1 ? Exp2 : Exp3;
#include <stdio.h>

int main()
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    (num % 2 == 0) ? printf("Even") : printf("Odd");
    return 0;
}

Loop Statements

Loops repeatedly execute a block of code until a condition becomes false.

for loop

for (init; condition; increment)
{
    statements;
}

Execute init once.

Evaluate condition; if true, run the loop body.

After the body, execute increment and repeat step 2.

#include <stdio.h>

int main()
{
    for (int a = 10; a < 20; a = a + 1)
    {
        printf("a = %d
", a);
    }
    return 0;
}

while loop

#include <stdio.h>

int main()
{
    int a = 10;
    while (a < 20)
    {
        printf("a = %d
", a);
        a++;
    }
    return 0;
}

do/while loop

#include <stdio.h>

int main()
{
    int a = 10;
    do
    {
        printf("a = %d
", a);
        a = a + 1;
    } while (a < 20);
    return 0;
}

Loop Control Statements

break : exits the nearest enclosing loop or switch.

continue : skips the remaining statements in the current iteration and proceeds to the next iteration.

goto : jumps to a labeled statement, often used to exit multiple nested loops.

Loop Control Style Tips

Even if a default case is not needed, it should be kept for safety.

Use goto to jump out of deeply nested loops when appropriate, reducing repetitive break statements.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CfundamentalsoperatorsControl Flow
AI Cyberspace
Written by

AI Cyberspace

AI, big data, cloud computing, and networking.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.