Structures/Statements

  • Home
  • Structures/Statements
Shape Image One

Structures/Statements

advanced divider

Control structures/statements:

Control structures control the flow of execution in a program or function. Control structures are used to repeat any block of code, transfer control to a specific block of code, and make a choice by selection. There are three basic control structures in C++

Selection / Decision Making Control Structure

Loops / Iteration Control Structure

Jumps

Selection/decision making control structures/statements:

The selection control structure allows a number of conditions which lead to a selection of one out of several alternatives. There are three types of selection control structure:

 

If selection structure / statement

If-else selection structure / statement

Switch selection structure / statement

If selection statement:

An if statement is a conditional statement that tests a particular condition. Whenever that condition evaluates as true, performs an action, but if it is not true, then the action is skipped.

Syntax:

The general syntax of if statement is:

 

if (condition)

{

Statement (s);

}

Flow Diagram:

If selection statement example:

#include<iostream>

using namespace std;

int main( )

{

 

    int num;

 

    cout << “Enter an integer number: “;

    cin >> num;

 

    if(num > 0)

    {

        cout << “You entered a positive integer: ” << num << “\n”;

    }

 

    return 0;

}

 

Output:

Enter an integer number : 10

You entered a positive integer : 10

Nested If Statement:

An if condition can be written as deeply as needed within the body of another statement.

This is called a nested if statement.

The general syntax of nested if statement is:

Copy code

if (condition 1)

{

    if (condition 2)

    {

        statements;

    }

}

Nested If Statement Example:

Copy code

#include<iostream>

using namespace std;

int main( )

{

    int exp, status;

    cout << “Enter experience: “;

    cin >> exp;

    cout << “\n Enter status: “;

    cin >> status;

    if(exp >= 4)

    {

        if(status >= 2)

        {

            cout << “\n Bonus Given to Employee” << “\n”;

        }

    }

    return 0;

}

Output:

Enter experience: 6

Enter status: 3

Bonus Given to Employee ​

If-else selection statement:

An if-else selection structure performs a certain action when the condition is true and some different action when the condition is false.

Syntax:

The general syntax of if-else statement is:

Copy code

if (condition)

{

    statement(s);

}

else

{

    statement(s);

}

If-else selection statement example:

Copy code

#include<iostream>

using namespace std;

int main()

(Note: The text extraction ended mid-sentence, indicating there may be more text following this that was not captured.) ​​

{

    int number;

    cout << “Enter an integer: “;

    cin >> number;

    if (number >= 0)

    {

        cout << “The number is a positive integer: ” << number << “\n”;

    }

    else

    {

        cout << “The number is a negative integer: ” << number << “\n”;

    }

    cout << “This line is always printed.”;

    return 0;

}

 

Output:

Enter an integer: -5

The number is a negative integer: -5

This line is always printed.

Else-If Selection Statement:

Nested if-else statements test for multiple conditions by placing if-else statements inside if-else statements. When a condition is evaluated as true, the corresponding statements are executed and the rest of the structure is skipped. This structure is also referred to as the if-else-if ladder.

else-if SELECTION STATEMENT EXAMPLE

#include<iostream>

using namespace std;

int main()

{

    int per;

    cout << “\n Enter your percentage: “;

    cin >> per;

    if (per >= 80)

(Note: The text extraction seems to end mid-sentence, suggesting that there may be more text that was not captured.) ​

{

    cout << “Your grade is A+:”;

}

else if (per >= 70)

{

    cout << “Your grade is A:”;

}

else if (per >= 60)

{

    cout << “Your grade is B:”;

}

else if (per >= 50)

{

    cout << “Your grade is C:”;

}

else

{

    cout << “Failed.”;

}

return 0;

}

Output:

Enter your percentage: 70

Your grade is A:

Switch statement:

Switch statement is a control statement that allows to select only one choice among the many given choices. The expression in switch evaluates to return an integer or character value, which is then compared to the values present in different cases. It executes that block of codes which matches the case value. If there is no match, then the default block is executed (if present).

Syntax:

The general form of switch statement is,

 

 

switch(variable)

{

    case constant 1:

    {

(Note: The text extraction seems to end mid-sentence, indicating that there may be more text that was not captured.) ​

statement (s);

break;

}

case constant 2:

{

statement (s);

break;

}

default:

{

statement (s);

break;

}

}

(Note: The extracted text seems to be the latter part of the switch case syntax and does not include the initial ‘switch’ statement or the flow diagram.) ​

Switch statement example:

#include<iostream>

using namespace std;

int main()

{

    char op;

    float num1, num2;

    cout << “Enter an operator (+, -, *, /):”;

    cin >> op;

    cout << “Enter two numbers: ” << “\n”;

    cin >> num1 >> num2;

    switch (op)

    {

        case ‘+’:

            cout << num1 <<” +” << num2 <<” = ” << num1 + num2;

            break;

        case ‘-‘:

            cout << num1 <<” -” << num2 <<” = “<< num1 – num2;

            break;

        case ‘*’:

            cout << num1 <<” *” << num2 <<” = ” << num1 * num2;

            break;

        case ‘/’:

            cout << num1 <<“/” << num2<<” = ” << num1 / num2;

            break;

        default:

            cout << “Error! The operator is not correct”;

    }

    return 0;

}

Output:

Enter an operator: +

Enter two numbers:

10 15

10 + 15 = 25

 

(Note: There may be some OCR errors in the text such as ‘numd’ which should be ‘num1’ and ‘num?’ which should be ‘num2’. These are corrected in the transcribed code above.) ​

Differences between if-else & switch statement:

If-else statement is used to select among two alternatives. The switch statement is used to select among multiple alternatives.

If-else can have values based on constraints. Switch can have values based on user choice.

Float, double, char, int, and other data types can be used in if-else condition. Only int and char data types can be used in switch block.

It is difficult to edit the if-else statement, if the nested if-else statement is used. It is easy to edit switch cases as they are recognized easily.

Quiz

advanced divider