0% found this document useful (0 votes)
0 views29 pages

C++ Programing Basics - Part 2

This document covers the basics of control flows in C++, including the evaluation of expressions, making decisions with if statements, nested ifs, switch statements, and the conditional operator. It explains the precedence and associativity of arithmetic operators, as well as Boolean expressions and relational operators. The document aims to equip students with the knowledge to effectively use these control flow structures in their programming.
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)
0 views29 pages

C++ Programing Basics - Part 2

This document covers the basics of control flows in C++, including the evaluation of expressions, making decisions with if statements, nested ifs, switch statements, and the conditional operator. It explains the precedence and associativity of arithmetic operators, as well as Boolean expressions and relational operators. The document aims to equip students with the knowledge to effectively use these control flow structures in their programming.
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/ 29

C++ Programing Basics

Part 2
Control Flows
“Would you tell me, please,
which way I ought to go from
here?”

“That depends a good deal


on where you want to get
to,” said the Cat.

LEWIS CARROLL, Alice in Wonderland


Outline
• Further Evaluation of C++ Expressions
• Making Decisions
Lecture Objectives
The student will:
➢ Further evaluate C++ expressions by being able to:
➢ Learn about the precedence and associativity of arithmetic
operations
➢ Use other unary operators
➢ Evaluate Boolean expressions
Lecture Objectives
The student will:
➢Make decisions by being able to:
➢ Use the if and if-else statements
➢ Use nested if statements
➢ Avoid common pitfalls with if statements
➢ Use the switch statement
➢ Use the conditional operator
➢ Use the logical AND and OR operators
Further Evaluation
of C++ Expressions
Precedence and Associativity of Arithmetic
Operators

• When more than one arithmetic operator is included in an


expression, then the concept of MDAS is followed.

• If the arithmetic operators fall under the same precedence


category, then a left to right associativity is followed.

• Some special cases follows a right to left associativity.


Precedence and Associativity Table
Category Operator Associativity
Postfix ( ) [ ] -> . ++ -- Left to Right
Unary + - ! ~ ++ -- (type)* & sizeof Right to Left
Multiplicative * / % Left to Right
Additive + - Left to Right
Shift << >> Left to Right
Relational < <= > >= Left to Right
Equality == != Left to Right
Bitwise AND & Left to Right
Precedence and Associativity Table
Category Operator Associativity
Bitwise XOR ^ Left to Right
Bitwise OR | Left to Right
Logical AND && Left to Right
Logical OR || Left to Right
Conditional ?: Right to Left
= += -= /= %= >>= <<=
Assignment Right to Left
&= ^= |=
Comma , Left to Right
Other Unary Operators
• Positive value operator: +
• Negative value operator: -
• Address operator: &
Evaluating Boolean Expressions
A Boolean expression is one
that is interpreted to be true or Relational Description
false. Operator
== equivalent to
Relational Operators are those > greater than
that evaluate the relationship >= greater than or
between operands. These equal to
operators are used to evaluate
Boolean expressions. < less than
<= less than or equal to
False = 0 ; True = 1 != not equal to
Making Decisions
The Single-Alternative if
This if statement is used if an action takes place only when
the result of the decision is true.

Syntax: Boolean expression


if (Boolean expression)
action if true; Action if true
Type in 22 and press Enter

Type 3 and press Enter


The Single-Alternative if - compounded
When more than one statements are to be executed when
the condition of the if statement is true, then we enclose
them with delimiters or “group” or block them.

driverAge
if (driverAge < 26) < 26?
{
premiumDue += 100
premiumDue += 100;
cout << “Young driver.\n”;
} display Young driver
The Single-Alternative if
If we do not “group” the statements or block them with
curly braces, the C++ compiler will interpret the code
differently
driverAge
< 26?
if (driverAge < 26)
premiumDue += 100; premiumDue += 100
cout << “Young driver.\n”;

display Young driver


The Dual-Alternative if
This is also called the if-else structure, which takes one
action when its Boolean expression is evaluated as true, and
uses an else clause to define the actions to take when the
expression is evaluated as false

Syntax: if (Boolean expression)


action if true;
else
action if false;
genderCode
F

display
display Male
Female

Type in M and press Enter


A little review please …
Consider the following code segment:

if (a > b)
++a;
else
b += 10;
a = 0;
1. If, when this code segment starts, a is 10 and b is 7, then
when it ends, a is 11.
A little review please …
Consider the following code segment:

if (a > b)
++a;
else
b += 10;
a = 0;
2. If, when this code segment starts, a is 2 and b is 4, then
when it ends, b is 14.
A little review please …
Consider the following code segment:

if (a > b)
++a;
else
b += 10;
a = 0;
3. If, when this code segment starts, a is 12 and b is 9, then
when it ends, a is 10.
Using a Nested if
A nested if structure can reside within either or both of the
if and else clauses of its containing if structure.

A nested if is an if statement that is the target of another if


statement. Nested if statements means an if statement
inside another if statement.
Type in g and press Enter
Using the Switch statement
When you want to create different outcomes depending on
specific values, you can use a series of ifs.
Using the Switch statement
However, as an
alternative to the long
string of ifs, you can
use the switch
statement. Although
not as flexible as the if
statement, the switch
statement is useful
when a single variable
must be compared to
multiple values.
Using the Conditional Operator
Another alternative to the if statement involves the
conditional operator (also called the if operator), which is
represented by a question mark (?). The if operator provides
a concise way to express two alternatives.
Consider the statements:
Conditional Operator

In this statement, a is compared to b, and if a is larger, the result is a, and if a is not


larger, the result is b. The comparison and the displayed result are handled in a single,
concise statement. The main advantage of using the conditional operator is that it
returns a value, and therefore it can be built into more complex expressions.

You might also like