KEMBAR78
2 Structured+Programming+18 10 2023 | PDF
0% found this document useful (0 votes)
31 views31 pages

2 Structured+Programming+18 10 2023

This document discusses structured programming in C++. It covers conditions using if/else statements, boolean expressions that return true or false, and several mathematical functions in C++ like max, min, square root, and others from the cmath header file. Practical examples are provided like checking if a user is old enough to vote and evaluating boolean expressions.

Uploaded by

abdoalsenaweabdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views31 pages

2 Structured+Programming+18 10 2023

This document discusses structured programming in C++. It covers conditions using if/else statements, boolean expressions that return true or false, and several mathematical functions in C++ like max, min, square root, and others from the cmath header file. Practical examples are provided like checking if a user is old enough to vote and evaluating boolean expressions.

Uploaded by

abdoalsenaweabdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Structured Programming

Presented by:

Dr . Mona Hussein Alnaggar


2023-2024
1st term
Lecture 3

1
Agenda

• C++ Conditions If ... Else

2
Practical question 1

• Write a simple code to Add Two Numbers.

3
Practical question 1

• Write a simple code to Add Two Numbers.

4
Practical question 2:

• Write a simple code to add two


numbers.
• The user must input two numbers. Then
we print the sum by calculating
(adding) the two numbers:

5
Practical question 2:

• Write a simple code to add two


numbers.
• The user must input two numbers. Then
we print the sum by calculating
(adding) the two numbers:

6
7
C++ Booleans
C++ Booleans

Very often, in programming, you will need a data type that can only have one of two values, like:

• YES / NO

• ON / OFF

• TRUE / FALSE

For this, C++ has a boo1 data type, which can take the values true (1) or false (0).
C++ Booleans
A boolean variable is declared with the bool
keyword and can only take the values true or false:

From the example above, you can read that a true


value returns 1, and false returns 0.
C++ Boolean Expressions
Boolean Expression

A Boolean expression returns a boolean value that is either 1 (true) or 0 (false).

This is useful to build logic, and find answers.

You can use a comparison operator, such as the greater than (>) operator, to find out if an expression (or
variable) is true or false:
C++ Boolean Expressions
Boolean Expression

Or even easier:

In the examples below, we use the equal to (==) operator to evaluate an expression:
C++ Boolean Expressions
Real Life Example

Let's think of a "real life example" where we need to find out if a person is old enough to vote.

In the example below, we use the >= comparison operator to find out if the age (25) is greater than OR
equal to the voting age limit, which is set to 18:
C++ Boolean Expressions
Real Life Example

the code above in an if...else statement, so we can perform different actions depending on the result:
C++ Conditions If ... Else
C++ Conditions and If Statements:

You already know that C++ supports the usual logical conditions from mathematics:

• Less than: a < b

• Less than or equal to: a <= b

• Greater than: a > b

• Greater than or equal to: a >= b

• Equal to a == b

• Not Equal to: a != b


C++ Conditions If ... Else
C++ has the following conditional statements:

• Use if to specify a block of code to be executed, if a specified condition is true

• Use else to specify a block of code to be executed, if the same condition is false

• Use else if to specify a new condition to test, if the first condition is false

• Use switch to specify many alternative blocks of code to be executed


C++ Conditions If ... Else
The if Statement

Use the if statement to specify a block of C++ code to be executed if a condition is true.

In the example below, we test two values to find out if 20 is greater than 18. If the condition is true, print
some text:
C++ Conditions If ... Else
The if Statement

We can also test variables:

Example explained

In the example above we use two variables, x and y, to test whether x is greater than y (using the >
operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we print to the screen that "x is
greater than y".
C++ Else
The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.
C++ Else
The else Statement

Example explained

In the example above, time (20) is greater than 18, so the condition is false. Because of this, we move on
to the else condition and print to the screen "Good evening". If the time was less than 18, the program
would print "Good day".
C++ Else If
The else if Statement

Use the else if statement to specify a new condition if the first condition is false.
C++ Else If
The else if Statement

Example explained

In the example above, time (22) is greater than 10, so the first condition is false. The next condition, in
the else if statement, is also false, so we move on to the else condition since condition1 and condition2 is
both false - and print to the screen "Good evening".
C++ Short Hand If Else
Short Hand If...Else (Ternary Operator)

There is also a short-hand if else, which is known as the ternary operator because it consists of three
operands. It can be used to replace multiple lines of code with a single line. It is often used to replace
simple if else statements:
C++ Short Hand If Else
Short Hand If...Else (Ternary Operator)

Instead of writing:

You can simply write:


C++ Math
C++ has many functions that allows you to perform mathematical tasks on numbers.

Max and min

The max(x,y) function can be used to find the highest value of x and y:

And the min(x,y) function can be used to find the lowest value of x and y:
C++ Math
C++ <cmath> Header

Other functions, such as sqrt (square root), round (rounds a number) and log (natural logarithm), can be
found in the <cmath> header file:
C++ Math
Other Math Functions

A list of other popular Math functions (from the <cmath> library) can be found in the table below:
Function Description
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 Description
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 Description
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

You might also like