C / C++

Control Statements in C : Effective Way to control the flow of program

Hello Everyone, Hope you are doing great . Here is my twelfth blog for you and fourth blog on C . Today we will discuss how to control flow and statements in c programming. Before that we have covered About C programming , Datatypes in C and operators in C .

Up until now, all programs written in C have had control flow from one instruction to the next. Sequential flow of control refers to this process of control moving from one command to the next.

However, when developing the logic for the majority of C program, the programmer may want to skip some instructions or repeatedly repeat a group of instructions. This type of control flow is referred to as non-sequential. The C statements that enable programmers to make these choices are referred to as decision-making or control statements.

In C, there are two types of decision making statements. One type is used to branch the control into different ways and the other type is used to repeat a set of instructions again and again. The two types of decision making statements are:

  1. Selection statements or Branching statements
  2. Looping statements

Selection Statements or Branching Statements

The C language’s selection statements allow programmers to choose a collection of instructions that will be carried out by the CPU. This decision is predicated on a circumstance. Additionally, C offers a set of unconditional branching statements that move control to different parts of the program. The choice assertions in C are:

  1. if statement
  2. switch statement
  3. Conditional operator statement
  4. goto

if Statement

Programmers can choose a set of instructions to run based on a condition by using the if statement. One set of instructions will be carried out if the condition is true, or an other set of instructions will be carried out if the condition is false. The following is an example of an if statement’s general form:

There are four variations of the if statement. They are:

  1. Simple if or null else
  2. if…else
  3. Nested if
  4. else if Ladder

Simple if or null else

A sequence of instructions can be executed or skipped using the simple if statement depending on the result of a condition. A one-way selection statement is the simple if.

A number of assertions will be carried out if the condition is satisfied. The control moves on to the sentence that follows the if statement if the condition is false. Diagrammatic representation of the simple if is provided below:

Syntax for simple if is as shown below:

if…else Statement

If…else is a selection statement that allows for two possible outcomes. One set of instructions will be carried out if the condition evaluates to true. A different set of instructions will be carried out if the condition evaluates to false. Diagrammatic representation of the if/else expression is provided below:

decision
if… else control

Syntax of if…else statement is as shown below:

Nested if

Cascaded if is another name for nested if. One if statement is layered inside another if statement in a nested if statement. The logical AND operator can be replaced by a nested if statement.

If the first if statement’s condition is determined to be true, the second if statement’s condition will also be determined to be true, and so on. Diagrammatic representation of the nested if statement is provided below:

Syntax of nested if control statement is as shown below:

else if Ladder

An operator for multiple way selection is the else if ladder in C. It enables the programmer to choose one set of instructions from among many different sets. The logical OR operator and the else if ladder are similar.

A set of instructions will be carried out if the first condition is satisfied. The following condition is checked, and so on, if the first one is false. The statements in the default block will be performed if all the circumstances are met. The diagrammatic representation of the else if ladder is as follows:

Syntax of else if statement is as shown below:

Switch Statement

Although C offers a multi-way selection statement similar to otherwise if, the program will get harder to follow as the number of criteria rises. C offers an intuitive multi-way selection statement called a switch statement to address this issue.

When there are more than three possibilities, the switch statement is straightforward to grasp. Diagrammatic representation of the switch statement is shown below:

According on the value of the expression, as shown in the figure above, the switch statement alternates between the blocks. There will be a value assigned to each block.

The switch statement’s expression must always reduce to an integer value. In the switch statement, the expression can therefore be either an integer value, a character constant, or an expression that can be reduced to an integer value. Each block’s label may either be an integer or a character constant.

The switch statement’s syntax is as follows:

As can be seen from the syntax above, each block is identified by the case keyword, which is followed by the block label. The default block and the break statement are both optional in a switch statement.

The statements in the default block are performed if none of the blocks match. Every block has a break statement at the conclusion.

All subsequent blocks are also performed until the next break statement is reached if the break statement is removed from a specific block.

Rules for Switch Control Statement

1) The switch expression must be of an integer or character type.

2) The case value must be an integer or character constant.

3) The case value can be used only inside the switch statement.

4) The break statement in switch case is not must. It is optional. If there is no break statement found in the case, all the cases will be executed present after the matched case. It is known as fall through the state of C switch statement.

Conditional Operator Control Statement

A unique operator termed a conditional operator, denoted by the symbol ( ? : ) is offered by the C programming language. A two-way selection operator is the conditional operator. The following example shows the syntax for a conditional operator statement:

As shown in the above syntax, when the condition evaluates to true, expr1 is executed. Otherwise, if the condition is false, expr2 is executed. The conditional operator statement can be represented diagrammatically as shown below:

Every conditional operator statement can be converted into an if…else statement. But the vice versa is not true.

goto Statement

The goto statement branches without regard to any conditions, in contrast to other selection or branching statements that we have encountered thus far that branch depending on a condition. The goto statement is also known as an unconditional leap statement for this reason.

Two additional unconditional branch statements are available in C. Break and continue are the two. The switch statement’s break statement has previously been viewed. But loops frequently employ both break and continue.

Therefore, these two unconditional branch statements will be discussed later. We can either skip some instructions and advance the program forward by using the goto branch statement, or we can jump back and repeat a series of instructions.

So there are two ways in which we can use the goto statement. They are:

  1. Forward jump
  2. Backward jump.

Syntax of forward jump and backward jump is as shown below:

goto-syntax

As shown in the above syntax, if the label is after the goto statement, then it is known as forward jump and if the label is before the goto statement, it is known as backward jump.

Decision Making Looping Statements

While writing C programs, the programmer might want a set of instructions to be repeated again and again until some condition is satisfied. For this purpose, C provides decision making looping statements. The looping statements provided by C are:

  1. while
  2. do…while
  3. for

All the looping statements in C essentially consist of two parts namely: control statement and body of the loop. The control statement decides when the loop will be stopped and the body of the loop contains the instructions that are to be repeated.

Based on where the control statement is placed in the loop, the looping statements are categorized to two categories. They are:

  1. Entry controlled loops
  2. Exit controlled loops

Why use loops in C language?

The looping turns difficult problems into simple ones. It permits us to change the program’s flow so that we can repeat the same code a set amount of times rather of writing it again. For instance, we can print inside a loop that iterates up to 10 times if we need to print the first 10 natural integers rather than using the printf statement 10 times.

Advantage of loops in C

1) It provides code reusability.

2) Using loops, we do not need to write the same code again and again.

3) Using loops, we can traverse over the elements of data structures (array or linked lists).

Entry controlled loops

The looping statements in which the control statement is placed before the body of the loop are known as entry controlled loops. Ex: while and for loops.

In entry controlled loops, the condition is checked first and if the value is true, then the body of the loop is executed. Otherwise the body is never executed. An entry controlled loop can be represented diagrammatically as shown below:

entry-controlled-loop

Exit controlled loops

The looping statements in which the control statement is placed after the body of the loop are known as exit controlled loops. Ex: do…while loop.

In exit controlled loops, the body of the loop is executed once and then the condition is checked. If the value is true, the body of the loop is executed again. Otherwise, the execution of the loop stops. An exit controlled loop is represented diagrammatically as shown below:

exit-controlled-loop

Based on the nature of the loops, the loops can be categorized into two types namely:

  1. Definite loops (Ex: for)
  2. Indefinite loops (Ex: while and do…while)

Definite loops: If the programmer exactly knows how many times he/she is going to repeat the set of instructions (loop), such loops are known as definite loops.

Indefinite loops: If the programmer does not know exactly how many times he/she is going to repeat the set of instructions (loop), such loops are known as indefinite loops.

The variable used in the condition inside of a definite loop is known as a counter and such loops are also known as counter controlled loops. The variable used in the condition inside of a indefinite loop is known as sentinel and such loops are also known as sentinel loops.

while Loop

The while loop is an entry-controlled loop in the C language. Only when the condition is true is the body of the while loops executed. The body of the loop is not run if the condition evaluates to false.

When a set of instructions needs to be repeated an infinite number of times, while loops are typically utilized. The following is an example of a while loop’s syntax:

do…while Loop

Do…while loops are exit-controlled loops in C. Prior to evaluating the condition, the do…while loop’s body is run. If the answer is true, the body of the loop is run once more; if the answer is false, the body of the loop is stopped.

While the do…while loop’s body is guaranteed to be executed at least once (>=1), the while loop’s body is not guaranteed to be executed at all. The do…while loop’s syntax is as follows:

for Loop

In C, the for loop is an entry controlled loop. The for is generally used while implementing definite loops in C programs. The for loop’s syntax is a little bit different from the other loops.

In the syntax of the for loop, first the counter is initialized, and then the condition is evaluated. If the value of the condition is true, the body of the for loop is executed. Otherwise, the body of the loop is not executed. After the execution of the for loop’s body, the counter is either incremented or decremented. Then the condition is evaluated again and so on.

The syntax of the for loop is as shown below:

The flowchart of the for loop is as shown below:

for-loop-cf

break and continue

C provides two unconditional branching statements which are extensively used inside the looping statements. They are:

  1. break
  2. continue

break Statement

To stop the loop from running, use the break statement inside of looping statements. The body of the loop’s execution stops when the break statement is encountered, and control is passed to the next instruction after the body of the loop.

The syntax of the break statement is as shown below:

continue Statement

The continue statement is used inside the looping statements to skip the execution of a set of instructions and return the control back to the loop.

When a continue statement is encountered within the body of the loop, the statements after the continue statement are skipped and the control is passed back to the loop.

The syntax of the continue statement is as shown below:

Conclusions

Hope you find it interesting and gain useful knowledge from it. We have covered all decision and control statements today. For more blogs like this visit our site : Theax Blogs .

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button