Control Statements in C

Control statements are the heart of any programming language. And as a developer I can say that almost 70-80 percent of the program contains control statements. Hence mastering over control statements means mastering the art of programming. Lets gain control over control statements.

What are Control Statements

Since program are a sequence of statements that are executed linearly one after another. Sometimes we need to change the normal program's flow and here control statements comes in action.
Control statements are statements that are intended to change the normal program's flow.
To understand better lets take an example: Suppose we are travelling from city A to city B. There can be two approaches used to travel from A to B:
  1. First approach(Uses normal program flow) will travel until he finds any obstacle(Road block). In case of any road blocks it terminates the travel program.
  2. The second approach(Uses control statements) will travel until he finds any obstacle(Road block). If he finds any road blocks it checks for an alternate path. If an alternate path is available then selects the alternate path to city B else terminates the travel program.

As you can see that control statements are highly useful in certain circumstances.
There are basically three types of control statements:

Conditional statements:

Conditional statements are also called as


statements. They checks for one or more conditions and if the condition is satisfied i.e true one or more statements are executed. They are most commonly used statements in programming. And if any consequence occurs in programming to run any condition based statement(Statements that requires some condition to be fulfilled prior to their execution) we use conditional statements.
Conditional statements can be of five types:

Repetitive statements:

As the name specifies repetitive statements are used to repeat something. Repeat something but what, repetitive statements are used to repeat one or more statements n times. But whats the need of repetition, the need for repeating statements can be explained via a general and basic example.
Lets take an example of functioning of a processor:
Tasks of a processor
Above diagram illustrates the core functioning of a processor Fetch-Decode-Execute, this core functioning needs to run till we shut down our computer. Hence requires any mechanism to repeat these three core functioning of processor until the computer is awake. And here Repetitive statements comes in action.
There are basically three types of repetitive statements provided in C but languages like C++, Java, C# etc provides an extra iterative statement.

Jump statements:

Jump statements are also one of the important control statements and are sometimes used in combination with repetitive statements. Unlike conditional and repetitive statements jump statements provide an unconditional way to transfer the program's flow from one part to another. There are three types of jump statements:
  • Break
  • Continue
  • Goto

Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment