0% found this document useful (0 votes)
3 views21 pages

C Programming Control Structures

The document provides a comprehensive guide to control structures in C programming, including sequential, conditional, and looping logic. It explains the execution order of statements, branching for decision-making, and iteration for repeating code blocks. Key constructs such as if statements, switch statements, and various loop types (for, while, do-while) are detailed with syntax examples and flowcharts.

Uploaded by

satyamydv0808
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views21 pages

C Programming Control Structures

The document provides a comprehensive guide to control structures in C programming, including sequential, conditional, and looping logic. It explains the execution order of statements, branching for decision-making, and iteration for repeating code blocks. Key constructs such as if statements, switch statements, and various loop types (for, while, do-while) are detailed with syntax examples and flowcharts.

Uploaded by

satyamydv0808
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Control Structures in C

Programming
A Comprehensive Guide to Sequential, Conditional, and Looping

Logic
Introduction to Control Flow

Control structures determine the

execution order of statements in a C "The ability to control the ow of a program is

program. what transforms a simple sequence of

instructions into a powerful and intelligent

application."

↓ Default Execution: By default, C programs execute

sequentially from top to bottom.


Branching: Allows the program to make decisions

and skip certain code blocks.

 Iteration: Enables the repetition of code blocks

until a condition is met.


Categories of Control Structures

  

Sequential Conditional Looping


The default step-by-step Choosing di erent execution Repeating a block of code

execution of statements in the paths based on boolean multiple times until a condition

order they appear. conditions (Selection). is met (Iteration).


Sequential Control Structure

The most basic form of program ow, where

 C Syntax Example
instructions are executed in a linear sequence.

int a = 10;
↓ Linear Flow: Statements are processed from top to
int b = 20;
bottom.
int sum = a + b;
printf("Sum is %d", sum);
 Guaranteed Execution: Every statement in the

block is executed exactly once.

 No Branching: There are no decisions or jumps to

other parts of the code.


Flowchart: Sequential Structure

START

Statement 1
Linear Program Flow

↓ In a sequential structure, the execution path is strictly linear.

Statement 2 There are no branches or loops, meaning every instruction is executed

exactly once, in the speci c order de ned by the programmer.


This is the simplest form of control ow and serves as the foundation for all

Statement 3 C programs.

END
Conditional: The if Statement

DEFINIT IO N EX AM P LE CO DE

The if statement is used to execute a block of code only

when a speci c condition is true. If the condition is false,


if (age >= 18) {
the code block is skipped.
printf("Eligible to vote.\n");
}

C SY NT AX

 Keywords must be in lowercase.


if (condition) {
// code to execute  Condition must be enclosed in parentheses.

}  Braces { } de ne the scope of the block.


Flowchart: if Statement

START

Logic Flow

The program evaluates the condition. If

False
it's true , the code block inside the

Condition? braces executes. If false , the entire

block is skipped.

True

Key Symbol

Execute if-block
The Diamond represents a decision

point where the ow branches based

on a boolean result.

END
Conditional: The if-else Statement

The if-else statement provides a binary choice. It  Practical Example

ensures that one of two blocks of code will always

be executed based on the condition's truth value.

if (num % 2 0) {
printf("Number is Even");
} else {
C SYNT A X
printf("Number is Odd");
}
if (condition) {
block if true
num / 2 is
} else { In this example, if the remainder of zero,

else block
block if false
the rst block runs. Otherwise, the

executes.
}
Flowchart: if-else Statement

START

Binary Branching

Unlike the simple if, the if-else

True False structure ensures that one of two paths


Condition?
is always taken. The program cannot

skip both.

if-block else-block
Execution Logic

If the condition is true , Path A

executes. If false , Path B executes.

Both paths eventually merge back to

the main ow.

END
Conditional: The else if Ladder

The else if ladder is used when you need to check  Grading Example

multiple conditions. It evaluates them one by one

until a true condition is found.

if (marks 90) {
printf("Grade: A");
} else if (marks 80) {
C SYNT A X printf("Grade: B");
} else {
if (cond1) { printf("Grade: F");
block 1 }
} else if (cond2) {
block 2 The program checks marks sequentially. Once a
} else {
condition is met, the corresponding block executes and
default block
the rest of the ladder is skipped.
}
Flowchart: else if Ladder

START

Sequential Testing
Cond 1? Block 1

Conditions are tested one by one.


False
As soon as a true condition is
found, its block executes and the
Cond 2? Block 2
rest are skipped.

Else Block

Default Path

The else block acts as a catch-all


that executes only if all preceding

conditions evaluate to false.

END
Conditional: Nested if Statements

A nested if is an if statement that is placed inside  Hierarchical Example

another if or else block. It allows for complex,

multi-layered decision making.


DEPENDENT LOGIC

if (has_id 1) {
C SYNT A X if (age 18) {
printf("Entry allowed");
} else {
if (outer_condition) {
printf("Too young");
if (inner_condition) {
}
executes if both are true } else {
} printf("No ID found");
} }

The inner age check only occurs if the outer ID check is

successful. This creates a logical hierarchy.


Flowchart: Nested if Statement

START

Hierarchical Logic
False
Outer Cond? The Inner Condition is only evaluated

if the Outer Condition is true. This

creates a dependency between


True
decisions.

False
Inner Cond?

Execution Path
True
To reach the inner code block, both

conditions must evaluate to true. If


Execute Inner Code

either fails, the ow skips to the end.

END
Conditional: The switch Statement

DEFINIT IO N EX AM P LE CO DE

The switch statement allows a variable to be tested for


equality against a list of values. Each value is called a
switch (day) {
case 1
case. printf("Monday");
break;
C SY NT AX case 2
printf("Tuesday");
break;
switch (expression) { default:
case constant1
printf("Invalid day");
code
}
break;
default:
default code
} ⚠ break is vital to prevent "fall-through" to next cases.
 default executes if no case matches the expression.
 Expression must result in an integer or character.
Flowchart: switch Statement

START

Multi-Way Branch

The switch expression is


Case 1 Default

Expression evaluated once and compared

against each case value. It's


more e cient than multiple if-
Case 2

else checks.

Block 1 Block 2 Default Block

The Break Rule

The break statement is crucial.


Without it, execution "falls

through" to the next case

regardless of the value.

END
Looping: The for Loop

The for loop is a count-controlled loop used when ↻ Iteration Example

the number of iterations is known beforehand. It

bundles initialization, condition, and update into

for (int i = 0; i < 5; i ) {


one line.
printf("%d ", i);
}

C SYNT A X
Iteration i Value Condition (i < 5)

for (init; condition; update) { 1st 0 True

code to repeat
} 2nd 1 True

... ... ...

6th 5 False (Exit)


Flowchart: for Loop

Initialization

The Loop Cycle

False The for loop follows a strict sequence:


Condition?
Init → Condition → Body → Update →
Condition.

True

Entry Control
Execute Body

The condition is checked before the

body executes. If the condition is false

at the start, the loop body never runs.

Update

END
Looping: The while Loop

The while loop is an entry-controlled loop. It is  Counter Example

used when the number of iterations is not known

in advance and depends on a condition.


ENTRY-CONTROLLED

int count = 0;
C SYNT A X while (count < 10) {
printf("%d ", count);
count ;
while (condition) {
}
code to repeat
update condition
} The condition count < 10 is checked before each execution.
If it becomes false, the loop terminates immediately.
Flowchart: while Loop

START

Entry-Controlled

The condition is evaluated before the


False
Condition? loop body. If the condition is false

initially, the body is never executed.

True

Inde nite Iteration

Commonly used when the number of


Execute Body

& Update Condition iterations is not known beforehand and

depends on dynamic data or user

input.

END
Looping: The do-while Loop

The do-while loop is an exit-controlled loop. It  Execution Example

guarantees that the loop body will execute at least

once before the condition is tested.


EXIT-CONTROLLED

int i = 10;
C SYNT A X do {
printf("Value: %d\n", i);
i ;
do {
} while (i < 5);
code to repeat
} while (condition);
Even though 10 < 5 is false, the program will print "Value:
10" once because the check happens at the end.

 Note: Don't forget the semicolon (;) after the while condition!
Flowchart: do-while Loop

START

Exit-Controlled

Execute Body The condition is checked after the


body executes. This guarantees that

the loop runs at least once.

True Post-Test Logic


Condition?
If the condition is true, the ow returns
to the start of the body. If false , the

loop terminates.
False

END

You might also like