0% found this document useful (0 votes)
6 views

Control Structures (Complete)

Control structures dictate the flow of control in programming and are categorized into three types: sequence logic, selection logic, and iteration logic. Selection logic includes decision-making statements like if, if-else, and switch, while iteration logic encompasses loops such as for, while, and do-while. Unconditional statements like break, continue, goto, and return manage execution flow without conditions.

Uploaded by

abdullahirfanhps
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)
6 views

Control Structures (Complete)

Control structures dictate the flow of control in programming and are categorized into three types: sequence logic, selection logic, and iteration logic. Selection logic includes decision-making statements like if, if-else, and switch, while iteration logic encompasses loops such as for, while, and do-while. Unconditional statements like break, continue, goto, and return manage execution flow without conditions.

Uploaded by

abdullahirfanhps
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
You are on page 1/ 11

Control Structures

Control Structures are just a way to specify flow of control in programs. Any algorithm or
program can be clearer and understood if they use self-contained modules called as logic or
control structures. It basically analyzes and chooses in which direction a program flows based on
certain parameters or conditions. There are three basic types of logic, or flow of control, known
as:
1. Sequence logic, or sequential flow
2. Selection logic, or conditional flow
3. Iteration logic, or repetitive flow

Sequential Logic (Sequential Flow)


Sequential logic as the name suggests follows a serial or sequential
flow in which the flow depends on the series of instructions given to
the computer. Unless new instructions are given, the modules are
executed in the obvious sequence. The sequences may be given, by
means of numbered steps explicitly. Also, implicitly follows the order
in which modules are written. Most of the processing, even some
complex problems, will generally follow this elementary flow pattern.

Selection Logic (Conditional Flow)


Selection Logic simply involves a number of conditions or parameters which decides one out of
several written modules. The structures which use these type of logic are known as Conditional
/decision-making Structures.

Iteration Logic (Repetitive Flow)


The Iteration logic employs a loop which involves a repeat statement followed by a module
known as the body of a loop.
Selection/Decision Making Structure
Decision-Making Statements are used to evaluate one or more conditions and make the decision
whether to execute a set of statements or not. These decision-making statements in programming
languages decide the direction of the flow of program execution.

if in C
The if statement is the most simple decision-making statement. It
is used to decide whether a certain statement or block of
statements will be executed or not i.e if a certain condition is true
then a block of statements is executed otherwise not.
Syntax: if (condition)
Statement;

if-else in C
The if statement alone tells us that if a condition is true it will
execute a block of statements and if the condition is false it
won’t. But if we want to do something else when the
condition is false we use C else statement. We can use
the else statement with the if statement to execute a block of
code when the condition is false. The if-else
statement consists of two blocks, one for false expression and
one for true expression.
Syntax: if (condition)
// Executes this block if condition is true
else

// Executes this block if condition is false


Nested if-else in C
A nested if in C is an if statement that is the target of another if statement. Nested if statements mean an if
statement inside another if statement.
Syntax: if (condition1)

// Executes when condition1 is true


if (condition_2)
// statement 1

else
// Statement 2

else

if (condition_3)
// statement 3

else
// Statement 4

if-else-if Ladder in C
The if else if statements are used when the user
has to decide among multiple options. The C if
statements are executed from the top down. As
soon as one of the conditions controlling the if is
true, the statement associated with that if is
executed, and the rest of the C else-if ladder is
bypassed. If none of the conditions is true, then
the final else statement will be executed. if-else-
if ladder is similar to the switch statement.
Syntax:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
switch Statement in C
The switch case statement is an alternative to the if else
if ladder that can be used to execute the conditional
code based on the value of the variable specified in the
switch statement. The switch block consists of cases to
be executed based on the value of the switch variable.
Syntax:
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Example of switch Statement
Difference Between if-else and switch Statement
Iteration Control structure
Iteration statements, commonly known as loops, are statements in programming used to
execute part of code repeatedly based on condition or set of conditions. These constructs are
important for performing repetitive tasks efficiently.
There are mainly three types of iteration statements:
 For Loop
 While Loop
 Do-While Loop

for Loop
for loop in C programming is a repetition control structure that
allows programmers to write a loop that will be executed a
specific number of times. for loop enables programmers to
perform n number of steps together in a single line.
Syntax:
for (initialize expression; test expression; update
expression)
// body of for loop

While Loop
While loop does not depend upon the number of iterations.
In for loop the number of iterations was previously known
to us but in the While loop, the execution is terminated on
the basis of the test condition. If the test condition will
become false then it will break from the while loop else
body will be executed.
Syntax:
initialization_expression;

while (test_expression)

// body of the while loop

update_expression;
do-while Loop
The do-while loop is similar to a while loop but the only
difference lies in the do-while loop test condition which is
tested at the end of the body. In the do-while loop, the
loop body will execute at least once irrespective of the test
condition.
Syntax:
initialization_expression;
do
{
// body of do-while loop
update_expression;

} while (test_expression);

Difference between while & do-while


Difference between for & while
Unconditional
In C, an unconditional statement is a statement that is executed without any condition or
consideration of whether a particular condition is true or false. Unconditional statements are
executed sequentially, one after the other, and they do not depend on any conditional logic.
1. Break Statement: In a loop or switch statement, the break statement is used to exit the
loop or switch block.
Syntax:

2. Continue Statement: In a loop, the continue statement is used to skip the current
iteration and move to the next iteration.
Syntax:
3. Goto: The goto construct causes an unconditional transfer of execution control from the
current location to any other labeled location.

In the above syntax Label is an identifier (meaning a name) and not a number. The same name
used for a label should not be used for declaring an identifier of any other type between the
current label and its goto statement. The compiler identifies this name as a label if it is followed
by a colon (:).
4. Return: Return statement ends the execution of a function and returns the control to the
function from where it was called. The return statement may or may not return a value
depending upon the return type of the function.
Difference between Break and Continue statements:

You might also like