3 Structured+programming+25 10 2023
3 Structured+programming+25 10 2023
Presented by:
1
Agenda
2
C++ Math
Other Math Functions
A list of other popular Math functions (from the <cmath> library) can be found in the table below:
Function Describtion
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x
asin(x) Returns the arcsine of x
atan(x) Returns the arctangent of x
cbrt(x) Returns the cube root of x
ceil(x) Returns the value of x rounded up to its nearest integer
cos(x) Returns the cosine of x
cosh(x) Returns the hyperbolic cosine of x
x
exp(x) Returns the value of E
C++ Math
Other Math Functions
A list of other popular Math functions (from the <cmath> library) can be found in the table below:
Function Describtion
x
expm1(x) Returns e -1
fabs(x) Returns the absolute value of a floating x
fdim(x, y) Returns the positive difference between x and y
floor(x) Returns the value of x rounded down to its nearest integer
2 2
hypot(x, y) Returns sqrt(x +y ) without intermediate overflow or
underflow
fma(x, y, z) Returns x*y+z without losing precision
fmax(x, y) Returns the highest value of a floating x and y
fmin(x, y) Returns the lowest value of a floating x and y
fmod(x, y) Returns the floating point remainder of x/y
C++ Math
Other Math Functions
A list of other popular Math functions (from the <cmath> library) can be found in the table below:
Function Describtion
pow(x, y) Returns the value of x to the power of y
sin(x) Returns the sine of x (x is in radians)
sinh(x) Returns the hyperbolic sine of a double value
tan(x) Returns the tangent of an angle
tanh(x) Returns the hyperbolic tangent of a double value
C++ Switch
C++ Switch Statements
Use the switch statement to select one of many code blocks to be executed.
C++ Switch
This is how it works:
• The value of the expression is compared with the values of each case
• The break and default keywords are optional, and will be described later in this chapter
C++ Switch
The example below uses the weekday number to calculate the weekday name:
C++ Switch
The break Keyword
When C++ reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing.
A break can save a lot of execution time because it "ignores" the execution of all the rest of the code in
the switch block.
C++ Switch
The default Keyword
The default keyword specifies some code to run if there is no case match:
C++ While Loop
C++ Loops
Loops are handy because they save time, reduce errors, and they make code more readable.
The while loop loops through a block of code as long as a specified condition is true:
C++ While Loop
In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less
than 5:
C++ Do/While Loop
The Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before
checking if the condition is true, then it will repeat the loop as long as the condition is true.
C++ Do/While Loop
The Do/While Loop
The example below uses a do/while loop. The loop will always be executed at least once, even if the
condition is false, because the code block is executed before the condition is tested:
C++ For Loop
C++ For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop
instead of a while loop:
C++ For Loop
C++ For Loop
• Statement 1 is executed (one time) before the execution of the code block.
• Statement 3 is executed (every time) after the code block has been executed.
C++ For Loop
C++ For Loop
Example explained
• Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true,
the loop will start over again, if it is false, the loop will end.
• Statement 3 increases a value (i++) each time the code block in the loop has been executed.
C++ For Loop
Another Example
This example will only print even values between 0 and 10:
C++ For Loop
Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
C++ For Loop
The foreach Loop
There is also a "for-each loop" (introduced in C++ version 11 (2011), which is used exclusively to loop
through elements in an array (or other data sets):
C++ For Loop
The foreach Loop
The following example outputs all elements in an array, using a "for-each loop":
C++ Break and Continue
C++ Break
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump
out" of a switch statement.
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues
with the next iteration in the loop.