Have you ever thought of controlling the execution of the program…..!!!.Those which control the flow of the program are called as control Structure of a program.These control structures are common for all programming languages irrespective of their designing.
Control Structures are broadly divided into two types:
1.Decision Making statement or Conditional Branching.
2.Looping or Iterative statements.
In this post we shall learn about both of these.
Decision Making Statements
These decision making statements are divided into four types.
1.simple if
2.if…else
3.nested if else
4.else if ladder.
1.simple if:If we need to check a single condition and then execute the program depending on the result of the condition then we use a simple if statement.The syntax of the simple if statement is:
if(condition) { statements to execute }</b>
eg:
if(a>b) { printf("A value is greater than B); }
2.if…else:In the above case if the condition fails we don’t have other option to execute.Hence we use the if…else statement to display an appropriate message when the condition fails.The syntax of the if…else statement is
if(condition) { statements to execute } else { statements to execute }
for eg:
if(a>b) { printf("A value is greater than B); } else { printf("B value is greater than A); }
3.Nested if else:After an initial condition if we need to check another condition under it then we use Nested if else.That is if statement is placed in another if statement.The syntax of nested if else is;
if(condition 1) { if(condition 2) { statements to execute } else { statements to execute } } else { statements to execute }
For eg:While comparing three numbers
if(a>b) { if(a>c) { printf("A is greatest of all the three"); } else { printf("B is greatest of all the three"); } } else { if(b>c) { printf("B is greatest of all the three"); } else { printf("C is greatest of all the three"); } }
4.else…if:If we need to check for multiple conditions then we use else..if statement.The syntax of the else…if statement is:
if(condition 1) { statements to execute; } else if(condition 2) { statement to execute; } else if(condition 3) { statements to execute; } else { statements to execute; }
With this we complete the explanation of decision making statements.The next post will be about Iterative statements…..
Pingback: Control Structures in C…!!!(Iteration or Looping) | letusprogram...!!!
Pingback: Switch case in C | letusprogram...!!!