What is Recursion in C++ programming language

Image
 What is Recursion in C++ programming language Today in this blog post we will cover the concept of Recursion in C++ programming language for  beginners. So what is Recursion? And what is the use of Recursion in programming languages? We will cover the answer of these questions shortly. For now let us look at the basic introduction of Recursion. Recursion is a concept in which the function calls itself with in its own body, and breaks the complex problems into smaller and more manageable parts. So the first question was what is recursion? Basically Recursion is a method or technique which is used in programming languages and in this method the function calls itself. It means the function is called with in its own body. We will cover this concept with simple programming example which will make the concept of recursion crystal clear to the audience. The Second question was what is the use of Recursion in programming languages? Recursion simply breaks down complex problems or pro...

How if else-if else ladder statement works in C program.

 How if else-if else ladder statement work in the C program.

In C programming or any other programming language, there is a concept of control statements or conditional statements. In one of our previous blog posts, we shed some light on the if else statement in the C program. In this post, we will cover the if else-if else ladder statement, which is used when multiple conditions need to be addressed. No, we are going to look at the syntax of the if else-if else ladder statement.



Syntax of if else-if else ladder statement

if (condition) 

{

// code to be executed if the condition is true

}

else if (condition)

{

// code to be executed if the condition is true

}

else if (condition

{

// code to be executed if the condition is true

}

else 

{

// code to be executed if none of the conditions is true

}


The if else-if else ladder statement is very similar to the if else statement. The main difference is the addition of an else-if part in it. In the above syntax part, we used two else-if conditions, it is according to our requirement that how many different conditions are needed to be addressed. We can use multiple else-if conditions according to our requirements.

Below is the sample code given for the if else-if else ladder statement. We are going to take a number as input and then we are going to implement the if else-if else statement.



In the above coding segment, we are entering a number, and we are going to check that the entered number is equal to 10. We are using the if condition and going to print the message within the if block that the entered number is equal to 10.



In the above coding segment if the entered number is 20 then we are going to use the else-if condition and run the code block of else-if which prints that the entered number is equal to 20, and the same logic will be used for the remaining else-if conditions and their code blocks. 



And at last, if the entered number is not equal to 10, 20, 50, or 100 then the else part will run and prints that the entered number is not equal to 10, 20, 50, or 100.


The Main difference between if and else-if statement

If we use multiple if conditions in our program, our program will check each and every if condition even if the very first if condition is true still the program will check all the remaining if conditions. Whereas in the else-if condition if the very first else-if condition is true the program will not check the remaining else-if conditions at all.

Congratulations we have successfully implemented the if else-if else ladder statement in our program.

Comments

Popular posts from this blog

What is Recursion in C++ programming language

Call by value and call by reference in C++ programming language

Nested Loops In C++ Programming Language