decision making statemant

 What is Decision Making Statement?

In the C programming language, the program execution flow is line by line from top to bottom. That means the c program is executed line by line from the main method. But this type of execution flow may not be suitable for all the program solutions. Sometimes, we make some decisions or we may skip the execution of one or more lines of code. Consider a situation, where we write a program to check whether a student has passed or failed in a particular subject. Here, we need to check whether the marks are greater than the pass marks or not. If marks are greater, then we decide that the student has passed otherwise failed. To solve such kind of problems in c we use the statements called decision making statements.

Decision-making statements are the statements that are used to verify a given condition and decide whether a block of statements gets executed or not based on the condition result.

n the c programming language, there are two decision-making statements they are as follows.


(i). if statement

(ii). switch statement

 πŸ‘‰πŸ‘‰if statement in c

In c, if statement is used to make decisions based on a condition. The if statement verifies the given condition and decides whether a block of statements are executed or not based on the condition result. In c, if statement is classified into four types as follows...


Simple if statement

if-else statement

Nested if statement

if-else-if statement (if-else ladder)

πŸ‘‰ Simple if statement

Simple if statement is used to verify the given condition and executes the block of statements based on the condition result. The simple if statement evaluates specified condition. If it is TRUE, it executes the next statement or block of statements. If the condition is FALSE, it skips the execution of the next statement or block of statements. 

πŸ‘‰.  if-else statement

The if-else statement is used to verify the given condition and executes only one out of the two blocks of statements based on the condition result. The if-else statement evaluates the specified condition. If it is TRUE, it executes a block of statements (True block). If the condition is FALSE, it executes another block of statements (False block). The general syntax and execution flow of the if-else statement is as follows.

πŸ‘‰. Nested if statement

Writing a if statement inside another if statement is called nested if statement. The general syntax of the nested if statement is as follows..

Syntex:-

If Condition (1)

If Condition (2)

{

True block of statement 1

}

}

Else

False block of condition 2 ;

}

}

πŸ‘‰. if-else-if statement (if-else ladder)

Writing a if statement inside else of an if statement is called if-else-if statement. The general syntax of the if-else-if statement is as follows.. 

Syntex:-

If (Condition 1)

{

True block of statement 1;

}

Else if (Condition 2)

False block of condition 1;

 &

True block of condition 2;

}


πŸ‘‰πŸ‘‰   switch' statement in C

Consider a situation in which we have many options out of which we need to select only one option that is to be executed. Such kind of problems can be solved using nested if statement. But as the number of options increases, the complexity of the program also gets increased. This type of problem can be solved very easily using a switch statement. Using the switch statement, one can select only one option from more number of options very easily. In the switch statement, we provide a value that is to be compared with a value associated with each option. Whenever the given value matches the value associated with an option, the execution starts from that option. In the switch statement, every option is defined as a case.

Comments

Popular posts from this blog

Computer science modem

operating system