Control Satements In C

In this article, I am going to explain what is control statements? how many types of control statements are there? let's dive into the topic


Control statements are statements which decide whether other statements should be executed or not.
for example, a condition that is written inside if-statement decides whether statements written inside if block should be executed or not.
Control statement are of three types
1.If statement
2.loops
3.Switch
Generally, control statements are written inside the method.
 So in if block, the condition is written inside the if statement, if the condition is true then if block will be executed or else block will be executed

Above example is a basic if-else block .we will discuss a nested if-else block, if-else if-else block in other section
Similarly, the switch block is used to select a single statement from multiple ones. the programmer gives input in switch statement which case should be executed. see the following example  

As you see in the above example if the user enters a value 1, then your giving value 1 to switch statement( switch(1) ), so the case one will be executed.
 if the user is given value 2 case two will be executed, if the user is given a no which is not there in any of the cases, then default case will be executed.
you may have doubt that why break statement is used in every case? the break keyword is used to terminate the block if this keyword is not used then every case in switch will be executed, as you see in the above example if break keyword is not written inside case one, then automatically all cases will be executed until it finds a break keyword in other cases.
After the execution of the break statement in any case in the above example, it terminates the loops and statements which are written outside the switch block will be executed. now you have clarity about switch control statement.

similarly, the loops are also control statements, we will discuss under loops topic

Post a comment

0 Comments