03 Expressions Statements Control Flow
03 Expressions Statements Control Flow
What is a statement?
controls the sequence of execution, evaluates an expression, or does nothing (the null statement) Basically all lines of code
Any place you can put a single statement, you can put a compound statement, also called a block. Non-strictly speaking, we can say that a block of code acts as one statement.
What is an expression?
Operators
a symbol that causes the compiler to take an action Operators act on operands. All operands are expressions.
Assignment operator
=
x = a + b;
lvalue term used for an operand that can be on the left side of an assignment operator rvalue ? Example:
x = 35; 35 = x;
Mathematical Operators
subtraction of unsigned integers that lead to negative values can produce unpredictable results beware of division!
you can put the same variable on the same side of the assignment operator
myVal = myVal + 2;
you can also use a shorthand notation (called the self-assigned addition operator):
myVal += 2; myVal /= 2;
((n+2) * (++n)) + 5;
postfix: x++; prefix: ++x; When value is not assigned to another variable no effect.
int x = 6, y = 6; int a = ++x; // a = x = x+1 int b = y++; // b = y; y = y + 1; The value of x and y is of course 7. a = 7, b = 6
Precedence
* 24)*(NumSupervisors + NumEngrs)); Better: ActualDays = NumDays NumWeekends; ActualHours = ActualDays * 24; NumPeople = NumSupervisors + NumEngrs; EffortSpent = ActualHours * NumPeople;
Truth
In C++, zero is considered false, all other values are considered true. Note: there is a data type bool whose value can only be TRUE or FALSE. Program flow is controlled by whether a certain condition is TRUE or FALSE.
Relational operators
used to compare whether two numbers are equal, or if one is greater or less than the other.
equals ==
not equals
greater than greater than or equals less than less than or equals
!=
> >= < <=
Examples
int x = 1, y = 2, z = 2, a = 0; 100 < x; // FALSE x==z; // FALSE y<=z; // TRUE a=z; // TRUE???
More precedence
Logical operators
when you want to ask more than one relational question at a time: is it true that x > y and y>z?
AND OR
NOT
&& ||
!
(x > y) && (y > z) x>y and y>z (x > y) || (y > z) (x > y) or (y > z) !(x > y) is it NOT true that x > y?
More precedence
Machine Problem #2
Deadline: June 28, 2010
IF
if (expression) statement;
Indentation styles
A if (expression) { statements } B if (expression) { statements } C if (expression) { statements }
ELSE
if (expression){
statement;
}
else { }
statement;
Nested IFs
if (expression1) { if (expression2) statement1; else { if (expression3) statement2; else statement3; } } else statement4;
if (x > 10) if (x > 100) cout << "More than 100" << endl; else cout << "Less than 10" << endl;
ELSE IF
if (expression1)
Conditional Operator
this reads: if expression1 is true, return the value of expression2; else, return the value of expression3
absValue = a>b ? a-b : b-a;
Practice exercises
Write a program that outputs High if the value of the variable is greater than 10, and Low if otherwise. The variable is of type int and is inputted by the user. Write a program that obtains input from the user a high_value, a low_value and a value from the user. Your program should inform the user if the value is out of range!; otherwise, output thank you!
Enter high value: 10 Enter low value: 3 Enter your choice: 2 value out of range!
Write a program similar to number 1 except that it will inform the user of the 3 following possibilities: a) number <10, b) 10<number<100, c) number>100
Exercises
Write a program that computes the absolute value of the difference of two user inputted integers.
switch
similar to the IF statement. allows multi-way branches. it is less flexible than IF-ELSE because switch statements use constants.
switch
switch (controlling_expression) {
case constant1: statement_sequence_1; break; case constant2: statement_sequence_2; break; default: default_statement_sequence;
if you forget a break statement, your code will continue until a break statement from another case statement is reached or until the end of the switch statement
case A: case a: cout << Excellent <<You need not take the finals; break;
Looping
while
while (boolean_expression) { }
statement_1; statement_2;
while
example:
int numOfGreetings; cin >> numOfGreetings; while(numOfGreetings > 0) { cout << Hello ; numOfGreetings--; } cout << endl;
do - while
do {
do - while
example:
int numOfGreetings; cin >> numOfGreetings; do { cout << Hello ; numOfGreetings--; } while(numOfGreetings > 0) ; cout << endl;
for
for (initialization; boolean_expr; update) }
for
example:
int numOfGreetings; cin >> numOfGreetings; for (; numOfGreetings > 0; numOfGreetings-){ cout << Hello ; } cout << endl;
for
int numOfGreetings; cin >> numOfGreetings; for (int i=0; i<numOfGreetings; i++){ } cout << endl;
Infinite loops
when exit conditions are not satisfied, the program continues to loop forever your program will hang
used to alter the flows of while, do-while and for statements break
continue
Practice exercises
Write a program that finds and prints all of the prime numbers between 3 and 100. Write a program which takes a single integer from the user and displays a "pyramid" of this height (between 1-30) made up of of "*" characters on the screen.
Exercises
The LeVan Car Rental Company charges 30PhP/km if the total mileage is less than 100km. If the total mileage is over 100, the company charges 30PhP/km for the first 100 km, then it charges 20PhP for any additional km over 100. The company gives a 500PhP discount for total mileage more than 500km. Write a program so that the clerk has to input the number of kilometers and the program will output the total price
READING ASSIGNMENT
Functions