Category: CPP

Recursion in C++ with example

Recursion in C++ with example The process in which a function calls itself is known as recursion and the corresponding function is called the recursive function. The popular example to understand the recursion is factorial function. Factorial function: f(n) = n*f(n-1), base condition: if n<=1 then f(n) = 1. Don’t worry we wil discuss what is base […]

Functions in C++ with example

Functions in C++ with example A Functions in C++ is block of code which is used to perform a particular task, for example let’s say you are writing a large C++ program and in that program you want to do a particular task several number of times, like displaying value from 1 to 10, in […]

goto statement in C++ with example

goto statement in C++ with example The goto statement in C++ is used for transferring the control of a program to a given label. The syntax of goto statement looks like this: goto label_name; Program structure: label1: … … goto label2; … .. label2: … In a program we have any number of goto and […]

Break statement in C++ with example

break statement in C++ with example The break statement in C++ is used in following two scenarios: a) Use break statement to come out of the loop instantly. Whenever a break statement is encountered inside a loop, the control directly comes out of loop terminating it. It is used along with if statement, whenever used inside […]

Continue Statement in C++ with example

Continue Statement in C++ with example Continue Statement in C++ is used inside loops. Whenever a continue statement is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop’s body for the current iteration. Syntax of continue statement continue; Example: continue statement […]

do-while loop in C++ with example

do-while loop in C++ with example As discussed in the last tutorial about while loop, a loop is used for repeating a block of statements until the given loop condition returns false. In this tutorial we will see do-while loop in C++. do-while loop is similar to while loop, however there is a difference between them: […]

While loop in C++ with example

While loop in C++ with example In the last tutorial we discussed for loop. In this tutorial we will discuss while loop in C++. As discussed earlier, loops are used for executing a block of program statements repeatedly until the given loop condition returns false. Syntax of while loop while(condition) { statement(s); } How while Loop […]

For loop in C++ with example

For loop in C++ with example A loop is used for executing a block of statements repeatedly until a particular condition is satisfied. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 […]

Switch Case statement in C++ with example

Switch Case statement in C++ with example Switch Case statement in C++ is used when we have multiple conditions and we need to perform different action based on the condition. When we have multiple conditions and we need to execute a block of statements when a particular condition is satisfied. In such case either we […]

If else Statement in C++

If else Statement in C++ Sometimes we need to execute a block of statements only when a particular condition is met or not met. This is called decision making, as we are executing a certain code after making a decision in the program logic. For decision making in C++, we have four types of control statements […]