Rev B Lecture 3

  1. Conditional Statements

A program can operate in three ways

    1. Step through in a sequential manner
    2. Make a choice depending upon condition(s) and branch out to another part of the program.
    3. Repeat in a loop a set of instructions.

What makes a computer powerful is that it can make decisions (item 2) depending upon conditions just executed.  The conditions are true or false utilizing relational and logical operators.

 

Operators

 = = (equal to), ! = (not equal to), < (less than), <=(less than or equal to), > (greater than), > = (greater than or equal to)

Logic

 !(not), &&(and), ||(or)

 

Values use the data type bool which is either true or false or int which is 1 or 0

Truth table for Logic symbols are

!true(1) = false(0)

true(1) && true(1) = true(1)

true(1) && false(0) = false(0)

false(0) && true(1) = false(0)

false(0) && false(0) = false(0)

true(1) || true(1) = true(1)

true(1) || false(0) = true(1)

false(0) || true(1) = true(1)

false(0) || false(0) = false(0)

 

Operator priority

!, +, - (unary operators)

*, /, %

+, -

<, <=, >=, >

= =, !=

&&

||

= (assignment)

 

 

a)     One-way selection

if (condition)

            statement 1;

            statement 2;

b)     Two-way selection

if (condition)

            statement 1;

else

            statement 2;

c)     Compound statement selection

if (condition)

            {

                        statement 1;

                        statement n;

            }

else

            {

                        statement n+1;

                        statement 2n;

            }

 

Important use is when a program attempts to read an input file that does not exist.  We then add the following code to check for this error and terminate the software correctly.

indata.open(“c:\\test.txt”);

if(!indata)

{

cout<< ”Cannot open the input file. ”

            << ”The program terminates.”;<< endl;

return 1;

            }

 

d)     Multiple selection: Nested if

if (condition)

            statement 1;

else if (condition)

            statement 2;

else

            statement 3;

e)     Selection by value

Previous selections were all by logical conditions causing true or false.  The following method uses value as a selection method.

            Switch (expression)

{

case value1:

            statements1;

            break(optional);

case value2:

            statements2;

            break(optional);

            .

            .

            .

case valuen:

            statementsn;

            break(optional);

default:

            statements

}

 

 

Exercises

CHAPTER 4

 

 

1. Give the truth value of the following statements:

 

If a=5, b=1, c=7, d=9

 

a. a > b

b. a == b

c. a + b != c + d

d. b % a == c % d

 

 

If a=6, b=15, c=21

 

a. a+c == b + 12

b. a < b || b < c

c. a > b || b > c

d. a < b || b > c

e. a + 6 * 2 == 15 || !(4 < c + 2)

f . 'a' < 'e'

g. a + 1 < 12 || c <= 0 && b == 15

 

/*Lecture 3 Exercise 1*/

 

#include<iostream>

using namespace std;

 

int main()

{

      int a=5, b=1, c=7, d=9;

      cout<<"a=5, b=1, c=7, d=9"<<endl;

 

      cout<<"(a > b)= "<<(a > b)<<endl;//a

 

      cout<<"(a == b)= "<<(a == b)<<endl;//b

 

      cout<<"(a + b != c + d)= "<<(a + b != c + d)<<endl;//c

 

      cout<<"(b % a == c % d)= "<<(b % a == c % d)<<endl;//d

 

      a=6, b=15, c=21;

      cout<<"a=6, b=15, c=21"<<endl;

     

      cout<<"(a+c == b + 12)= "<<(a + c == b + 12)<<endl;//a

 

      cout<<"(a < b || b < c)= "<<(a < b || b < c)<<endl;//b

 

      cout<<"(a > b || b > c)= "<<(a > b || b > c)<<endl;//c

      cout<<"(a < b || b > c)= "<<(a < b || b > c)<<endl;//d

 

      cout<<"(a + 6 * 2 == 15 || !(4 < c + 2))= "<<(a + 6 * 2 == 15 || !(4 < c + 2))<<endl;//e

 

      cout<<"('a' < 'e')= "<<('a' < 'e')<<endl;//f

 

      cout<<"(a + 1 < 12 || c <= 0)= "<<(a + 1 < 12 || c <= 0)<<endl;//g

 

      return 0;              

}

 

 

2.  /*Lecture 3 exercise 2

        Trace the following segment of code.  Input is  40 80 56 34*/

 

  /*Lecture 3 exercise 2*/      

 

#include<iostream>

using namespace std;

 

int main()

{

  double  total_sales = 0;

  double item1,item2,item3,item4;

  double discount= 0;

 

  cout<<"Trace the following segment of code.  Input is  40 80 56 34"<<endl;

 

  cin>>item1>>item2>>item3>>item4;

  total_sales = item1 + item2 + item3 + item4;

  if (total_sales > 100)   discount = .10;

  cout<<"total sales= "<<total_sales<<endl;

  cout << "Total amount owed: "<<total_sales - total_sales*discount<<endl;

 

      return 0;              

}

3.   Change this program so it makes sense

   if (age < 1)

       cout<<"Infant";

   else if (age < 13)

       cout<<"Child";

   else if (age < 20)

       cout<< "Teen";

   else if (age == 65)

       cout<< "you are eligible for retirement";

 

 

/*Lecture 3 exercise 3*/      

 

#include<iostream>

using namespace std;

 

int main()

{

      double age;

      cout<<"enter the age = ";

      cin>>age;

      cout<<endl;

      if (age < 1)

       cout<<"Infant"<<endl;

   else if (age < 13)

       cout<<"Child"<<endl;

   else if (age < 20)

       cout<< "Teen"<<endl;

   else if (age == 65)

       cout<< "you are eligible for retirement"<<endl;

   else if (age == 110)

       cout<< "you are finally qualified to teach csc126"<<endl;

 

      return 0;              

}

 

 

4.  if (num_year == 5 && month==12)

     {

             salary = salary + salary*.10

             cout << "You will receive a raise";

      }

      else

             cout <<"Not eligible for a raise";

 

5.  if (num_year == 5)

         if (month == 12)

         {

             salary = salary + salary*.10

             cout << "You will receive a raise";

          }

          else

 cout << "You will receive a raise in "

      <<12-month<<"months";

     else

           cout<< "Not eligible for a raise";

 

.6.      What is the output of the following program?

 

#include<iostream>

using namespace std;

 

const char FIRST = 'A';

const char LAST = 'Z';

 

int main()

 

{

 

      char letter;

      int number=45;

     

      letter = 'V';

 

      if (letter >= FIRST && letter <= LAST)

            cout<<"the letter is capitalized";

      else

            cout<< "not a capital letter";

 

      if (letter == 'V' && number == 46 || number == number)

            cout<<"\ntrue!";

      else

            cout<<"\nfalse!";

 

      if (6 + 7 < number && letter > 'M')

            cout << "\ntrue!";

      cout<< "\nfalse!";

      return 0;

}

 

 

7. For alpha = any value from 0 to 7 what is the final output of alpha?

 

 

cin>> alpha;

switch(alpha)

{

case 1:

case 2: alpha = alpha + 2;

             break;

case 4: alpha++;

case 5: alpha = 2 * alpha;

case 6: alpha = alpha + 5;

            break;

default: alpha--;

}

cout<<”the final value of alpha = “ << alpha;

 

/*Lecture 3 exercise 7 Switch*/

#include <iostream>

 

using namespace std;

 

int main()

{

      int alpha;

      cout<<"Please enter a number from 0 to 7 ";

      cin>> alpha;

      cout<<endl;

switch(alpha)

{

case 1:

case 2: alpha = alpha + 2;

             break;

case 4: alpha++;

case 5: alpha = 2 * alpha;

case 6: alpha = alpha + 5;

            break;

default: alpha--;

}

cout<<"the final value of alpha = " << alpha<<endl;

 

      return(0);

}