You are on page 1of 5

bool four;

four = (2 < =3)

to be used to make true or false staments in c++ program.

A==B

In bool means that is 3==3 will be true. True or false is numbers not match. With 3!=3 the statement
will be false.

Ch. 4 control structures.

Control structures

Computers proceed in sequence selectively and repetitively

And only some parts are executed in parts if conditions are met

Ex. Prompt the uset to enter a number .

Get the number.

If the numbers is positive or negative to send a message to the user if only positive send only this
message or if its negative only post negative message and skip all others.

There are selection statements in c++, there is selection and repetition.

Relational Operators

A condition is represented by a logical expression that can be true or false

Relation operatros are ==,

8>15 is ture

6!=6 is false

2.5>5.8 is false and so forth

Comparing characters

Expression with relational operators depends on the machines collating sequence

Like ‘r’>’t’t is false

True has a value of 1 and false has a value of 0

Strings can also be compared


String str1 = “hello”;

String str2 =”hi”;

It compares letter by letter the closer it is to the beginning the less value it as thus a is the smallest value
in the alphabet and z is the highest.

Capital letters also have effects on the values

Like “hello” cannot equal “Hello”

“hello” > “Hello” would be true. Captial letters have less value than lower case

! not

&& and

|| is or.

Example

!(‘A’>’B’) would be true. It is negation. In reality it would be false because b is bigger than a. but with
the negation change it to true.

Example 2.

(14>=5) && (‘A’<’B’) has a value of true

(24>=35) && (‘A’<’B’) has a value of false because both have to be true. It won’t test the second part.
Complier wont care to read the second message.

(14>=5) | | (‘A’>’B’) would have a value of true. Only one has to be true in the | | the complier would
read it all unlike and operators.

Order of precedence

Relational and logical operatros are evaluated from left to right like reading

Parenthesis can override precedence.

Example how to display 0<= X <=10

In c++

0<= x && x <= 10

In order of precedence negation is first in precedence


< are after addition , the n == operations

Then & operation

The or operations

End is equal.

Note Wednesday meet in asb in rm 2.146


B= a*3 < 5 -4%2

In this equation, multiply first, then do modular, to do subtraction, to execute when done.

Like if a=1 ,

B=3a<5 and if its true, b is given a value of 1

5<=3 && !3 || 3+4*2

!3=0 because its false so do that first, the multiplication the addition, then to the less than equal to, false
is always 0, so false and false in the &&, 0 is false so it is given a false value

So now

0 || 11

A false with a true, || is or so it has a true value, so the value is 1.

EXAMPLE 1

Bool found = true;

Int age = 20;

Double hours

!found is false. Becase found is true, negation gives it to be opposite

!age, a number greater than 0 so it has true, negation makes it false, 0

Ex.

If (score >=60)

Grade = ‘P’

Compound statement is a statement is a single statement


If (score>=60)

{grade =”pass”

Cout << “ … }

Two way selections make different statements for different values

If (expression)
Statement 1
Else
Statement 2

If (hours >40.0)
Wages =40*rate +
1.5*rate*(hours -40);

Else
Wages= hours*rate;

Nesting More if statements within if or else statements


For example
If (balance > 50000.00)
Interest rate = 0.07;
Else
If (balance >= 25000)
Interest rate = 0.05;
Else
If( balance >=1000.00)
Interest rate = 0.03;
Else
Interest rate = 0.00

Short circuit evaluation

Short c e: evaluation of a logical expression stops as soon as the value of the expression is know

(age >=21) || (x ==5) this statement is true. If this is true then they whole things will go to if.if it is all
false then it will go to the else statement.

(grade==’A’) && (x>=7)

Be careful with order of precedence operations

Such as write 0<=x<=10 as 0<=x && x<=10


Or it can have false or incorrect results!!!!!

Conditional operator (?:) takes three arguments

Syntax for using conditional operator: it can take the place of If else statements.

Expression 1? Expression 2?: Expression 3?

Switch structures:alternate to if elese

Switch(integral) expression is evaluated first, value of the expression determines which corresponding
action is taken, expression is sometimes called a selector

Switch ( ) can have any letter value or true values as numbers instead of Boolean statements unlike
the true false statements from the if else statements
{case value 1: __
__
__
Case value 2 ____
___
Default:
Statements

If there is not break in the case values it will keep going when it reaches a break statement is received to
get out of the switch.

Example 1
Switch (grade)
{
Case ‘a’:
Cout << “ the grade point is 4.0.”;
Break;
Case ‘b’:
Cout << “the grade point is 3.0.”;
Default:
Cout << “the grade Is invalid.”;

You might also like