Friday, April 19, 2024

Conditional structures in c++ language


Conditional structures in c++:

In C++, restrictive proclamations are control structures that let code simply decide. These assertions are otherwise called dynamic explanations. They are of the sort if articulation, if-else , if else-if stepping   stool , switch , and so on. These assertions decide the progression of the program execution.

The progression of a program is overseen involving restrictive proclamations in C++. On the off chance that a condition is valid inside the if statement, the body of the assertion executes. If misleading, else is used to determine a substitute game-plan. You can check a few rules consecutively utilizing the else-if statement and gain C++ without any preparation. For multi-way spreading in view of the consequence of an articulation, utilize the switch proclamation. You can likewise check our C++ Confirmation Course for top to bottom information on C++.

Restrictive Explanations in C++ programming?

In C++, contingent proclamations are control structures that let code decide. These assertions are otherwise called dynamic proclamations. They are of the kind if proclamation, if-else, if else-if stepping stool, switch, and so forth. These assertions decide the progression of the program execution.


Kinds of Restrictive Proclamations in C++

In this article, we'll examine the main sort of contingent assertion for example if...else explanations in C++. We'll examine the switch proclamation in the following instructional exercise switch explanation in C++.

On the off chance that else is the primary sort of control explanation in C++. In this, the tasks are performed by some predefined condition. The assertions inside the body of the on the off chance that block get executed if and provided that the given condition is valid.

We have four variants of if-else statements in C++. They are:

  1. if statement in C++
  2. if-else statement in C++
  3. if else-if ladder in C++
  4. nested if statement in C++

  1. if statement in C++

if statements in C++ are one of the most simple statements for deciding in a program. When you want to print any code only upon the satisfaction of a given condition, go with the simple if statement.

go with the simple if statement.

if statement in C++

Syntax

if(testcondition)
{
 // Statements to execute if
 // condition is true
}

  • In the if statement, the test condition is in ().
  • If the testcondition becomes true, the body of the if block executes else no statement of the if block gets printed.

Example

// C++ program to understand if statement
#include <iostream>
using namespace std;
int main()
{
 int i = 11;
 if (i > 15)
 {
 cout << "11 is greater than 15";
 }
 cout << "I am Not in if";
 return 0;
}

This C++ code in C++ Compiler initializes the integer ito 11 and then checks to see if it is greater than 15 using an ifstatement. It outputs "I am Not in if" to the console because 11 is not greater than 15.

Output

    I am Not in if 
    

    1. if-else statement in C++

    It is an extension of the if statement. Here, there is an if block and an else block. When one wants to print output for both cases - true and false, use the if-else statement.

    if-else statement in C++

    Syntax

    if (testcondition)
    {
     // Executes this block if
     // condition is true
    }
    else
    {
     // Executes this block if
     // condition is false
    }

    Example

    // C++ program to understand the if-else statement
    #include <iostream>
    using namespace std;
    int main()
    {
     int A = 16;
     if (A < 15)
     cout << "A is smaller than 15";
     else
     cout << "A is greater than 15";
     return 0;
    }
    

    This C++ program initializes the integer variable Ato 16 and then checks to see if it is less than 15 using the if-elsestatement. It outputs "A is greater than 15" to the console because it isn't.

    Output

    A is greater than 15
    

    1. nested if statement in C++

    We can include an if-else block in the body of another if-else block. This becomes a nested if-else statement.

    click to study :     nested loop in c++

    click  to study nested loop patterns

    nested if statement in C++

    Syntax

    if(test condition1)
    { //if condition1 becomes true
     if(test condition1.1)
     {
     //code executes if both condition1 and condition 1.1 becomes true
     }
     else
     {
     //code executes if condition1 is true but condition 1.1 is false
     }
    }
    else
    {
     //code executes if condition1 becomes false
    }
    

    Example

    // C++ program to understand nested-if statement
    #include <iostream>
    using namespace std;
    int main() {
     int a = 10;
     int b = 5;
     if (a > b) {
     // Control goes to the nested if
     if (a % 2 == 0) {
     cout << a << " is even";
     }
     else {
     cout << a << " is odd";
     }
     }
     else {
     cout << a << " is not greater than " << b;
     }
     return 0;
    }
    

    • In the above code in C++ Editor, the first if statement checks whether a>b.
    • If a is greater than b, the nested if statement is checked.
    • If the nested if condition is false, the else statement in the nested if block gets executed.
    • If the first if statement evaluates to false, the nested if block gets skipped and the else block executes.

    Output

    10 is even
    

    1. if else-if ladder in C++

    It is an extension of the if-else statement. If we want to check multiple conditions if the first ifcondition becomes false, use the if else-if ladder. If all the if conditions become false the else block gets executed.

    if else if ladder in c++

    Syntax

    if (testcondition1)
     statement;
    else if (testcondition2)
     statement;
    .
    .
    else
     statement;

    Example

    // C++ program to understand if-else-if ladder
    #include <iostream>
    using namespace std;
    int main()
    {
     int A = 20;
     if (A == 10)
     cout << "A is 10";
     else if (A == 15)
     cout << "A is 15";
     else if (A == 20)
     cout << "A is 20";
     else
     cout << "A is not present";
     return 0;
    }

    The if else-if ladder is shown in this section of C++ code. It sequentially compares the value of to several conditions. It outputs "A is 20"as the condition matched and comes out of the ladder.

    Output

    A is 20

    No comments:

    Post a Comment

    last post

    C language Vs C++ language in 2024

    Popular Posts