Us C++ 2021 02
Us C++ 2021 02
- Arithmetic operators
Exercise
Write a program that determines the values of
expressions a, b given x,y,z
How to sold a problem in
computer science?
Level 4
Non-convex Optimization
Level 3
Convex Optimization
MCMs & PDEs
Level 2
Graphs
Level 1
“Exhausted check”
method & “Look-up
Table” method
Agenda
Rules for forming structured
programs
Rules for Forming Structured Programs
- Begin with the "simplest activity diagram“.
- Any action state can be replaced by two action states in
sequence _ Stacking rule .
- Any action state can be replaced by any control statement
(sequence, if, if...else, switch, while, do...while or for) _
Nesting rule.
if ( grade >= 60 )
cout << "Passed";
if...else Double-Selection
Statement
The if single-selection statement performs an indicated action only
when the condition is TRUE; otherwise the action is skipped.
if ( condition )
action if true
else
action if false
Nested if...else Statements
Nested if...else statements test for multiple cases by
placing if...else selection statements inside other if...else
selection statements.
while (condition)
do something;
1 2 3 4 5 6 7 8 9 10
for Repetition Statement
The for repetition statement is often used for loops that involve
counting.
You can write any for loop as a while (and any while as a
for).
- the update statement is executed each time the body of the loop
has been executed (and before the condition is checked)
Counter-controlled repetition with the for
statement.
1 // Fig. 5.2: fig05_02.cpp
2 // Counter-controlled repetition with the for statement.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 int main()
8{
9 // for statement header includes initialization,
10 // loop-continuation condition and increment.
11 for ( int counter = 1; counter <= 10; counter++ )
12 cout << counter << " ";
13
14 cout << endl; // output a newline
15 return 0; // indicate successful termination
16 } // end main
1 2 3 4 5 6 7 8 9 10
do...while Repetition Statement
The do...while repetition statement is similar to the while statement.
In the while statement, the loop-continuation condition test occurs
at the beginning of the loop before the body of the loop executes.
The do...while statement tests the loop-continuation condition after
the loop body executes; therefore, the loop body always executes
at least once.
When a do...while terminates, execution continues with the
statement after the while clause. Note that it is not necessary to
use braces in the do...while statement if there is only one
statement in the body; however, most programmers include the
braces to avoid confusion between the while and do...while
statements
do
somestuff;
while ( condition );
switch Multiple-Selection
Statement
switch multiple-selection statement to perform many different
actions based on the possible values of a variable or expression.
Each action is associated with the value of a constant integral
expression
break Statement
The break statement, when executed in a while, for, do...while or
switch statement, causes immediate exit from that statement.
Program execution continues with the next statement.
Common uses of the break statement are to escape early from a
loop or to skip the remainder of a switch statement
1 // Fig. 5.13: fig05_13.cpp
2 // break statement exiting a for statement.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 int main()
8{
9 int count; // control variable also used after loop terminates
10
11 for ( count = 1; count <= 10; count++ ) // loop 10 times
12 {
13 if ( count == 5 )
14 break; // break loop only if x is 5
15
16 cout << count << " ";
17 } // end for
18
19 cout << "\nBroke out of loop at count = " << count << endl;
20 return 0; // indicate successful termination
21 } // end main
continue Statement
The continue statement, when executed in a while, for or do...while
statement, skips the remaining statements in the body of that statement and
proceeds with the next iteration of the loop.
In while and do...while statements, the loop-continuation test evaluates
immediately after the continue statement executes.
In the for statement, the increment expression executes, then the loop-
continuation test evaluates.
1 // Fig. 5.14: fig05_14.cpp
2 // continue statement terminating an iteration
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 int main()
8 {
9 for ( int count = 1; count <= 10; count++ )
10 {
11 if ( count == 5 ) // if count is 5,
12 continue; // skip remaining code
13
14 cout << count << " ";
15 } // end for
16
17 cout<<"\nUsed continue to skip printing 5"<<endl;
18 return 0;
19 } // end main
Exercise 1
Write C++ statements to accomplish each of the following tasks.
Set variable x to 1.
Add variable x to variable sum and assign the result to variable sum.
Print "The sum is: " followed by the value of variable sum.
Calculate and print the sum of the integers from 1 to 10. Use the while
statement to loop through the calculation and increment statements.
The loop should terminate when the value of x becomes 11.
Exercise 2
Write single C++ statements that do the following:
Assume the integer variables sum and count have been declared.
Calculate the value of 2.5 raised to the power 3 using function pow.
What prints?
Print the integers from 1 to 20 using a while loop and the counter
variable x. Assume that the variable x has been declared, but not
initialized. Print only 5 integers per line. [Hint: Use the calculation x %
5. When the value of this is 0, print a newline character; otherwise,
print a tab character.]
Calculate
Exercise 5
Write a C++ program to accomplish the following:
[email protected]
Quiz #2
1. Calculate the values of the following expressions
given a natural number n
n
k!
1 1 1
k =1
+ + .... +
2 3 k
a = b =1
1 1
1 1
a = b k −1
+ a k −1
k
2 2
= 2 a k −1 + bk −1
2
b k