Decision Making(Condition Statement) And Branching-Learnt Hub

Decision Making Statements:- 
You can decide yourself which statements you want to execute and which statements you want to skip in your program. This process is called decision making.
Most decision making is done on the basis of a condition. When a particular condition occurs, you can execute the statements you want. For this, you use some built in statements. Because these statements work with conditions, they are also called conditional statements. And these statements control execution in the program, so they are also called control statements.
Read Also:
  1. Basic Of C Programming Language
  2. Data Types
  3. Variable And Constant
Before getting to know about these statements, let us try it with an example.
Suppose you want to print the marks of any two students whose marks is more. 
See the program given below:.

In the above example, the marks of 2 students is stored in 2 variables. But you only have to print the marks of the student whose marks is the highest. In this situation you need to take a decision.
You can do this through any decision making statement (If, If - else, Switch).
Type Of Decision making Statements:- 
There are various types of decision making statements, which written below:

1. If Statement:- 

If statement is a control statement that test a particular condition. In these types of statements, if evaluated condition comes out to be true, then statement-block will be exicuted. Otherwise, the given set of action are ignored. The general syntex of a simple if statement is:
The 'statement-block' may be a single statement or a group of statement. If the test expression is true, the statement-block will be executed; otherwise the statement-block will be skipped and the execution will jump to the statement-x. When the condition is true both the statement-block and the statement- x are executed in sequence.

 2. If-else Statement:- 

The if-else statement is an extension of the simple if statement. In these type of statements, when condition is true a group of statements are executed. If the condition become false, then else part will be executed.
The general Syntex of if-else statement is:

If the test expression is true, then the true-block statement are executed, otherwise, the false-block statement are executed. In either case, either true-block or false-block will be executed, not both. After axecution of any statement (true or false) the control is transferred to the statement-x.

3. Nested if-else Statement:- 

This is extension of if else statement. When a series of decisions are involved. we may have to use more than one if-else statement in nested form. This is called nested if else statement.
The general syntex of Nested if-else statement is:

If the condition-1 is false, the statement-3 will be executed, otherwise it test the condition-2. If the condition-2 is true, the statement-1 will be executed, otherwise the statement-2 will be executed and then the control is transferred to the statement-x.

The Else-if Ladder:- 

When we have a problem that only one of the many conditions is correct, then we use this control statement.  In this, several if conditions are given in sequence with else and program control checks all these conditions in order and wherever the if condition becomes true, the program control executes the statement block of that if condition.
The general syntex of  Else-if Ladder is:
In this type of statement, first test condition-1, if condition-1 is true then statement-1 will be executed, and if condition-1 is false then it test condition-2, if condition-2 is true then statement-2 will be executed otherwise, it reached to condition-3, if condition true then statement-3 executed, otherwise it is reached to next statement, unless true condition found.

Post a comment