2.10 Control Statements

     Programming in C    


Control Statements in C – A Complete Guide

Introduction

In the C programming language, program statements are normally executed sequentially, meaning one after another from the beginning of the main() function until its end. This natural order of execution is known as sequential execution.

However, real-world problems rarely follow a straight-line logic. Programs often need to make decisions, repeat tasks, or jump to different sections of code. This is where control statements play a vital role.

Control statements determine the flow of execution in a C program. They allow programmers to alter the normal sequence of execution based on conditions, repetitions, or explicit control transfers.


Statement Blocks and Flow of Control

A statement block (also called a compound statement) is a group of statements enclosed within curly braces { }. Syntactically, it is treated as a single statement. Compound statements were designed to make control structures easier to implement.

In C89, all local variables must be declared at the beginning of a block. In C99, variables can be declared anywhere inside the block, provided they are declared before use.

Variables declared inside a block have block scope, meaning they are visible only within that block. Accessing them outside the block results in a compilation error.

The flow of control refers to the order in which program statements are executed. Controlling this flow is one of the most important aspects of programming.



Types of Control Statements in C

Control statements in C are broadly classified into three categories:

  1. Selection (Branching) Statements

  2. Iteration (Looping) Statements

  3. Jump Statements

Together, these constructs direct how a program executes.


Test Conditions in C

Selection and iteration statements rely on test expressions.

  • If a test expression evaluates to true, it returns 1

  • If it evaluates to false, it returns 0

  • Any non-zero value is treated as true

  • Zero is treated as false

Test expressions are formed using:

  • Relational operators

  • Logical operators

  • Or a combination of both

Relational Operators

<, >, <=, >=

Equality and Logical Operators

==, !=, &&, ||, !

Among these, ! (logical NOT) is a unary operator; all others are binary operators.


True and False in C

C does not have built-in Boolean values (until C99 introduced _Bool).

  • 0 → false

  • Any non-zero value → true

Examples of expressions evaluating to true:

  • (a)

  • (-a)

  • (a = 3)

Examples evaluating to false:

  • (a - 1)

  • !(a)

  • (0 * c)

⚠ Using assignment (=) instead of equality (==) is a common logical error.


Short-Circuit Evaluation

C uses short-circuit evaluation:

  • In &&, if the first operand is false, the second is not evaluated

  • In ||, if the first operand is true, the second is not evaluated

This improves efficiency but may cause unexpected behavior if the second operand has side effects.



1. Selection Statements

Selection statements allow a program to choose between alternative execution paths. This process is also known as branching.

Types of Selection

  • One-way selection

  • Two-way selection

  • Nested selection

  • Multi-way selection

1.1 if Statement (One-Way Selection)

The if statement executes a block of code only if a condition is true.

if (TestExpr) stmtT;

If the condition is false, control moves to the next statement.

1.2 if-else Statement (Two-Way Selection)

The if-else statement executes one block if the condition is true and another if it is false.

if (TestExpr) stmtT; else stmtF;

Common Pitfall

if(a = b) // WRONG

This assigns b to a.
✔ Correct form:

if(a == b)

✔ Safer practice:

if(3 == x)

1.3 Multi-Way Selection

When multiple conditions must be tested, C provides:

  • if-else-if ladder

  • switch statement

if-else-if Ladder

Conditions are evaluated top-down, and the first true condition executes.

1.4 Nested if and Dangling Else

An if inside another if is called a nested if.
An else always associates with the nearest unmatched if, leading to the dangling else problem.

✔ Best solution: Use braces { } clearly.


1.5 Conditional Operator ( ?: )

The conditional operator is a compact alternative to if-else.

expr1 ? expr2 : expr3;

Example:

c = a > b ? a : b;

✔ Shorter code
✔ Useful inside expressions
✔ Often produces efficient object code


1.6 switch Statement

The switch statement performs multi-way selection based on equality testing.

switch(expr) { case constant1: statements; break; default: statements; }

Key Points

  • Expression must be of integral type

  • break prevents fall-through

  • default is optional but recommended




2. Iteration (Looping) Statements

Loops execute a block of statements repeatedly as long as a condition remains true.

C provides three loop constructs:

  • for

  • while

  • do-while

2.1 for Loop

A counter-controlled loop, used when the number of iterations is known.

for(init; TestExpr; update) { stmT; }

✔ All expressions are optional
for(;;) creates an infinite loop
✔ Loop variable can be declared inside the loop (C99)

2.2 while Loop

A pre-test loop where the condition is checked before execution.

✔ May execute zero times
✔ Used when iterations are not known in advance

2.3 do-while Loop

A post-test loop where the body executes at least once.

✔ Condition checked after execution
✔ Useful when at least one execution is mandatory




3. Jump Statements

Jump statements transfer control unconditionally.

C supports:

  • goto

  • break

  • continue

  • return

3.1 go-to

Transfers control to a labeled statement.

⚠ Should be avoided due to poor readability
✔ Occasionally useful to exit deeply nested loops

3.2 break and continue

  • break exits the loop or switch completely

  • continue skips the current iteration

3.3 Nested Loops

A loop inside another loop is called a nested loop.

✔ Inner loop runs fully for each outer loop iteration
✔ Commonly used for:

  • Pattern printing

  • Matrix operations

  • Tables

  • Prime number generation



Common Programming Errors

  • Using = instead of ==

  • Writing a < b < c

  • Forgetting braces

  • Dangling else

  • Semicolon after loop headers

  • Comparing floating-point values using ==

✔ Correct floating-point comparison:

if(fabs(a - b) < 0.000001)

Conclusion

Control statements are the backbone of logical programming in C. They enable decision-making, repetition, and structured execution flow. Mastery of selection, iteration, and jump statements allows programmers to write efficient, readable, and reliable programs.





📖 Reference

The content for this subject is prepared by referring to the standard textbook
“Computer Fundamentals and Programming in C” by Pradip Dey and Manas Ghosh,
Second Edition, Oxford University Press (2018).
The explanations are exam-oriented and strictly aligned with the concepts presented in the reference book.



Aivette-coi is created with the intention of helping college students learn smartly, revise quickly, and approach exams with confidence.

With love and care,
by Aivette 💙


Comments

Popular posts from this blog

22IMC7Z2 - CONSTITUTION OF INDIA

1.1 Historical Background of the Indian Constitution: The Company Rule & The Crown Rule

1.3 Preamble and Salient Features of the Indian Constitution