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

In C++ Learning Objectives Explain Each: -Use conditional statements to control

ID: 3730889 • Letter: I

Question

In C++ Learning Objectives Explain Each:

-Use conditional statements to control program flow.

-Examine relational and logical operators and describe how to use them

-Formulate and evaluate logical (Boolean) expressions

-Explain the difference between the switch and nested if statement.

Instructions:

Read chapter 4 in your text book or google the Web; if you use the Web, then you must include the link(s) you used for help you formulate your response.

Respond to item 1 above using one descriptive paragraph; be succinct and to the point.

Respond to item 2 above using one descriptive paragraph, be succinct and to the point.

Respond to item 3 above using two examples of logical (Boolean) expressions.

Respond to item 4 above using one descriptive paragraph, be succinct and to the point.

Explanation / Answer

a)

Conditional Statement are the statements which helps in decision making where when the statement is true a set of statements gets executed or other set gets.

We have many conditional statement:

               Syntax:

                    if(expression)

                                  {

                                    //statements to be executed

                                  }

              Syntax :

                  if(expression)

                   {

                   //set of statements

                   }

               else

                 {

                  //set of statements

                }

                   Syntax:

                      if(expression)

                     {

                  //set of statements

                   }

               else if(expression)

              {

              //set of statements

              }

               ..

               ..

               ..

              else

             {

             //set of statements

             }

               Syntax:

                switch(a)

                 {

case 1:

                                //set of statements

                              break;

case 2:

                              //set of statements

                                break;

..

..                            

..

..

default:

//set of statements

break;

}

Syntax:

bigger = a>b?a:b

Here if a is greater than b then it will pick a and assign it to bigger else it will b to bigger (it picks value written before and after: based on expression).

b)

Relational Operators: these are binary operators which are used to identify relation between two values. But the comparison can happen only in similar data types and they always return either 1 or 0.

Some of the operators are

== equal to: to check whether two values are equal or not.

> greater than: to check one value is greater than other.

< less than: to check one value is less than other.

<= less than or equal: to check if value is less than or equal to another.

!= not equal: to check if the value is not equal to another.

>= greater than or equal: to check if one value is greater than or equal to another.

Let see each one of them for two values

10 and 20

a) 10 == 20: 10 and 20 are not equal hence this will return 0

b) 10 > 20: 10 is not greater than 20 hence it will return 0

c) 10 < 20: 10 is less than 20 hence it will return 1

d) 10 < = 20: 10 is less than 20 hence it will return 1

e) 10 != 20: 10 and 20 are not equal hence it will return 1

f) 10>=20: 10 is neither greater nor equal to 20 hence it will return 0.

Logical Operator: used to check between conditions

AND (&&): returns true if both the expressions are true

OR(||): returns true if both or one of them is true

NOT(!): returns true if expression is false else true (i.e. it negates the result of expression).

c)

Boolean expression is an expression which either results in true or false and it contains relation and/or logical operators.

Say we need to check whether a person is a Man and single then we have two variables

isMan and isSingle

and we make the boolean expression isMan && isSingle and store the result in variable check

check = (isMan && isSingle)

Another example let's say we need to check whether student scored more than 60 marks then we have 60 and studentScore (non zero numbers are considered true)

boolean expression here becomes if(studentScore>60)

d) The difference between switch and nested if is that nested if is type strict condition check whereas switch is jump value catching. In nested if we can have expression to be evaluated and in switch variable expressions are not allowed. switch is faster than nested if as it just has to check which case to be executed rather than checking which case if satisfied. switch is more readable.