Control Structures (Complete)
Control Structures (Complete)
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
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
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)
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);
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: