Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab04_4.cpp is provided. The program demonstrates the if statement. All the acti

ID: 3730825 • Letter: L

Question

Lab04_4.cpp is provided. The program demonstrates the if statement. All the action statements execute because all the Boolean expressions evaluate to true. Edit the program to reverse the sense of the Boolean expressions so none of the action statements execute.

// Lab04_4.cpp

// Exercise the if statement

// YOUR NAME:

// CCCC CSC120 Fall 2009

// DATE:

#include <iostream>

using namespace std;

int main()

{

// expression evaluates to true, this is the trivial case

if ( true )

{

cout << "action of first if statement executed" << endl;

}

// expression using logical not operator (a unary operator)

if ( ! false )

{

cout << "action of second if statement executed" << endl;

}

// expression using relational operator

if ( 4 < 5 )

{

cout << "action of third if statement executed" << endl;

}

// expression using relational operator

if ( 5 > 4 )

{

cout << "action of fourth if statement executed" << endl;

}

// expression using equality operator

if ( 4 == 4 )

{

cout << "action of fifth if statement executed" << endl;

}

// expression using equality operator

if ( 4 <= 5 )

{

cout << "action of sixth if statement executed" << endl;

}

// expression using relational operator

if ( 5 >= 5 )

{

cout << "action of seventh if statement executed" << endl;

}

// expression using relational operator

if ( 4 != 5 )

{

cout << "action of eighth if statement executed" << endl;

}

// Numeric expression will also work.

// We usually think of one as true, and zero as false.

if ( 1 )

{

cout << "action of ninth if statement executed" << endl;

}

// Actually, any non-zero value evaluates to boolean true.

if ( 4 )

{

cout << "action of tenth if statement executed" << endl;

}

return 0;

} // end of function main

Explanation / Answer

// Lab04_4.cpp

// Exercise the if statement

// YOUR NAME:

// CCCC CSC120 Fall 2009

// DATE:

#include <iostream>

using namespace std;

int main()

{

    // expression evaluates to true, this is the trivial case

    if (false)

    {

        cout << "action of first if statement executed" << endl;
    }

    // expression using logical not operator (a unary operator)

    if (!true)

    {

        cout << "action of second if statement executed" << endl;
    }

    // expression using relational operator

    if (4 >= 5)

    {

        cout << "action of third if statement executed" << endl;
    }

    // expression using relational operator

    if (5 <= 4)

    {

        cout << "action of fourth if statement executed" << endl;
    }

    // expression using equality operator

    if (4 != 4)

    {

        cout << "action of fifth if statement executed" << endl;
    }

    // expression using equality operator

    if (4 > 5)

    {

        cout << "action of sixth if statement executed" << endl;
    }

    // expression using relational operator

    if (5 < 5)

    {

        cout << "action of seventh if statement executed" << endl;
    }

    // expression using relational operator

    if (4 == 5)

    {

        cout << "action of eighth if statement executed" << endl;
    }

    // Numeric expression will also work.

    // We usually think of one as true, and zero as false.

    if (0)

    {

        cout << "action of ninth if statement executed" << endl;
    }

    // Actually, any non-zero value evaluates to boolean true.

    if (0)

    {

        cout << "action of tenth if statement executed" << endl;
    }

    return 0;

} // end of function main