Wednesday, September 28, 2022
HomejQuery5+ Examples to Study C++ If, else, else if statements

5+ Examples to Study C++ If, else, else if statements


Resolution making assertion in C++

  • To execute a press release or block of statements if a sure situation(s) is true, it’s possible you’ll use the C++ if assertion.
  • As a situation is true within the if assertion, C++ will execute the given assertion(s).
  • In an effort to execute a number of statements if the situation is fake, it’s possible you’ll use the else assertion.
  • When you’ve got a couple of situations to test, in case the primary situation is fake, it’s possible you’ll use the else if statements.
  • Within the following sections, we are going to present you examples of utilizing if, if with else, and if..else if..else statements.

For newbies to get this:

It’s possible you’ll evaluate decision-making statements (if..else and change..case) in C++ or different programming languages identical to the decision-making in actual life. So, if it’s important to select which shade shirt to purchase, which place to journey this 12 months and so on. you’ve got choices to select from.

In an effort to decide, you undergo sure checks/situations e.g. Do I like this shade? Sure/No. Do I’ve already this shade shirt? Sure/No and so forth. Primarily based on these standards, you decide.

Syntax of utilizing the if assertion in C++

The overall approach of writing the C++ if assertion is:

An instance of utilizing a easy if assertion

On this instance, we solely used the C++ if assertion. So, the assertion contained in the if will execute provided that the situation is true. In any other case, nothing will show. We are going to test the worth of the variable x in opposition to y as follows:



Output:

CPP-if-statement

What if the situation is fake? Utilizing C++ else assertion

As talked about within the introductory part, the assertion contained in the if assertion executes if the situation is true. In an effort to execute a number of statements if the situation is fake, we use C++ else assertion.

Normal approach of utilizing the else assertion in C++:

The instance of the else assertion

Within the following instance, we assigned a price to the variable x. Then we are going to test if the variable worth is larger than or equal to five.

If the worth is larger or equal to five then the assertion contained in the if assertion will execute. In any other case, else assertion will execute:



CPP-if-else

C++ if..else if..else assertion

The else if assertion is used when you’ve got a number of situations to test – if the primary situation is fake. The syntax of utilizing the else if assertion is as follows:

A easy instance of utilizing if..else if..else statements

On this instance, a variable marks is asserted and assigned a price. Then we used if..else if..else statements. See the code and output:



Output:

CPP-if-else-if-else

The instance of else if with string variable

Within the following instance, we are going to test the colour worth saved within the variable utilizing if..else if, and else statements. So, the primary situation checks if the colour is crimson, it can show its message. The second assertion (else if) test if the colour is inexperienced. Third test if the colour is black.

Lastly used the else assertion that may execute if all situations are false. Take a look:



Output:

CPP-if-else-if-strings

An instance of utilizing if assertion with for loop

The instance under reveals utilizing an if assertion with a C++ for loop. We are going to test the worth of a variable used within the for a loop. If the worth of variable x is 5 – that we are going to test within the if assertion (inside for loop) then for loop will terminate through the use of the break assertion.



Output:

Extra complicated instance with person enter and a number of situations

On this instance, we are going to use the logical operator && for implementing a number of situations within the if assertion.



As you run this program (copy/paste the code in your editor),  it can ask you to enter a quantity. If the quantity is between 0 to 100, then if situation will consider as true.

There we used a logical operator && to test the minimal and most numbers. If the entered quantity is between 0 to 100 (inclusive) then if situation will likely be true and it’ll show a message.

In any other case, else assertion will execute:

Output as I entered 75:

CPP-if-else-multiple

Output as I entered quantity 105

CPP-if-else-multiple-2

Easy methods to use nested if assertion in C++

C++ means that you can use an if assertion inside one other assertion. That is referred to as a nested if assertion.

That is how we are able to use it:



So, the place we write the statements to execute inside curly braces, we provoke one other if assertion. The primary assertion is named outer if whereas the second that we used inside, is named inside if assertion.

An instance of nested if assertion

Allow us to see the nested if assertion within the motion under:



Output:

CPP-nested-if

 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments