## CIS22A - Notes for Week 4, 10/14-10/17

### Terminology

#### rand() function and mod practice

Example - write code to simulate rolling two dice.

How to seed the random number generator

### Chapter 4

#### if statements

if ( condition )  statement;

if ( condition )
statement;

if ( condition )  {
block of statements
}

if ( condition )
{
block of statements
}

#### relational operators

> < == >= <= !=

bool type

The boolalpha manipulator

#### logical operators

&&
and is an alias for &&
||
or is an alias for ||
!
not is an alias for !

#### Common if statement problems

if ( x > y );
cout << " x > y ";

if (x = y)
cout << " x = y ";

if (x <= y)
cout << " x is less ";
cout << " than y ";

#### if ... else

Program 4-8 - Page 168
Program 4-9 - Page 169

#### Nested if

Program 4-10 - Page 172
Program 4-11 - Page 173

The dangling else

if (condition)  ...
if (condition)
...
else
...

#### sequence of if statements

if ( condition )
statement or block
if ( condition )
statement or block
if ( condition )
statement or block
if ( condition )
statement or block
...

#### sequence of if ... else statments

if ( condition )
statement or block
else if ( condition )
statement or block
else if ( condition )
statement or block
else if ( condition )
statement or block
else
statement or block

#### if ... else Review Problems

 Assign a letter grade for      90 - 100% = A        80 - 89% = B        70 - 79% = C        60 - 69% = D  less than 60% = F Generate a random int, x,  between 1 and 10.  The int is big if it is greater than 6, otherwise it is small. Write if and if ... else statements for the following: if x is big, print "big" if x is big, print "big", otherwise print "small" identify x as big even or small even identify x as big even, small even, big odd, or small odd identify x as big, small even or small odd identify x big or small, even or odd, and a multiple of 3 or not.  How may combinations are there?  Should the if ... else's be separate or nested?

Program 4-20 - Page 197-198

#### The conditional operator

aka ternary operator

condition expression1 ? expression2 : expression3

If the condition express1 is true, the operator returns expression2 otherwise it returns expression3.

#### The switch statement

Syntax:

switch (integral expression)
{
case (integral value1):
statement(s)
case (integral value2):
statement(s)
case (integral value3):
statement(s)
default:
}

block and scope

Example:

90 - 100% = A
80 - 89% = B
70 - 79% = C
60 - 69% = D
less than 60% = F

Example:

Write a program to simulate a random playing card and printing the value of a random card, such as
queen of clubs

Videos
if statement
condition
block
scope

expression

logical value
type bool
true
false

relational operator
>
<
==
>=
<= !=
logical operator
&&
||
!
alias
and
or
not

fall through

else
nested if
three way condition

dangling else