Professional Documents
Culture Documents
Welcome to
CS 211: Introduction to Computing
Weeks (4): Solving simple constraint satisfaction problems – Assignment and
Operators in C++ - Control Statements
Week#4 Lecture Outline
• Problem-Solving Principle:
• Problem decomposition
• Solving simple constraint satisfaction problems
15 km/hour
5 km/hour
Problem # 1: Average Speed (Discussion)
To break it down and simplify it:
1. First we need to know how to calculate the average speed, what is
the formula?
• Speed=distance / time
2. Calculate the total distance for the whole trip, what is it?
3. Calculate the total time for the whole trip, what is it?
4. Examine this for certain value.
5. Can you have a general formula or calculation for such problem?
6. Desk-check the resulting formula with different values.
Problem # 1: Average Speed (Discussion)
How to apply it?
Problem-Solving Technique e.g. of Technique Application
An experienced problem-solver will simply make up a If you don’t know the distance between the child’s
value for the missing parameter to move the problem home and the store assume your program shall work
forwards. for any distance.
Further, the experienced problem-solver will select a In this case, the distance of 15 km from the home to
value that is very convenient. the store is a good choice. Why 15 is convenient ?
Ensure that all measurement units used in the For example, all distances are measure using km , all
calculations are the same. speed km/h , time is measured using hours and so on.
• Mr White: G
• Mr Green: B
Programming and Application
Characters
• Characters are enclosed in single quotes
Slide 2- 25
Display 2.5
Mini Quiz
1. Which of the following is a character literal?
a. ‘56’
b. ‘%’
c. “a”
d. both a and b
2. Which of the following is a string literal?
a. “$”
b. “Good Morning!”
c. “ ”
d. all of the above
3. If you assign the number 10.25 to an int variable, what will the computer
store in the variable?
Commonly Used Escape Sequences
Write the C++ statement to print the text (He said: “The \ is called a backslash”)
The endl Manipulator
• You do NOT put quotation marks around endl
• Branch
• Let the program choose between two alternatives.
• A selection structure is needed when a decision must be made
(based on some condition) before selecting an instruction to execute
• A selection structure’s condition must be phrased as a Boolean
expression (evaluates to true or false)
Assignment 1
Display 2.8
One-Way Selection
parenthesis { }
Slide 2- 38
Comparison Operators and Numeric Data Types
• You can use the comparison operators with all numeric data types:
• 8 < 15 evaluates to true
• 6 != 6 evaluates to false
Slide 2- 39
Comparison Operators and Char Data Types
• You can use the comparison operators with char data type (using the
ASCII collating sequence):
Slide 2- 40
Logical Operators
• Used to evaluate more than one expression at the same time.
• Logical operators such as:
• AND
• OR
• NOT
Slide 2- 41
Logical Operators (AND)
• The AND operator: &&
• True if both expressions are true.
• Syntax: (Expression 1) && (Expression 2)
• Example:
Slide 2- 42
Logical Operators (OR)
• The OR operator: ||
• True if either or both expressions are true.
• Syntax: (Expression 1) || (Expression 2)
• Example:
Slide 2- 43
Logical Operators (NOT)
• The NOT operator: !
• Negates any Boolean expression.
• ! Operator can make expressions difficult to understand. Use only when appropriate
• Example:
Slide 2- 44
Common errors to avoid in if condition
• Confusion Between == and =
• ' = ' is the assignment operator, used to assign values to variables:
o Example: x = 3;
• ’ = = ' is the equality operator, used to compare values:
o Example: if ( x == 3)
• The compiler will accept this error: if (x = 3)
but stores 3 in x instead of comparing x and 3
o Since the result is 3 (non-zero), the expression is always true!
Slide 2- 45
Common errors to avoid in if condition
• Be careful translating inequalities to C++
• if x < y < z translates in C++ as: if ( ( x < y ) && ( y < z ) )
NOT as: if ( x < y < z )
Assignment 2
• Write an if-else statement that outputs the word High if the value of
the variable score is greater than 80 and Low if the value of score is at
most 80? The variables are of type int.