You are on page 1of 23

Todays Agenda

 Need/Purpose of Selection
 Selection: if and if...else
 One-Way Selection
 Compound (Block of) Statements
 Comparing if...else Statements with a Series of if Statements; Short-Circuit Evaluation
 Solution/Algorithm development (Pseudo-Code and Flow-Chart)
 Comparing Floating-Point Numbers for Equality
 Associativity of Relational Operators
 Input Failure and the if Statement
 Confusion between the Equality Operator (==) and the Assignment Operator (=)
• switch Structures (Pseudo-Code and Flow-Chart)
Selection and Repetition

•Selection: used for decisions, branching -- choosing between 2 or more alternative paths. In C++, these are the types of selection statements:
•if
•if/else
•switch
•Repetition: used for looping, i.e. repeating a piece of code multiple times in a row. In C++, there are three types of loops:
•while
•do/while
•for
Some useful tools for building programs or
program segments
• pseudocode - helps "think" out a problem or algorithm before trying to
code it
• flowcharting - graphical way to formulate an algorithm or a program's
flow
• stepwise refinement (top-down design) of algorithms
True and False

•Selection and repetition statements typically involve decision steps. These steps rely on conditions that are evaluated as true or false
•C++ has a boolean data type (called bool) that has values true and false. Improves readability.
•Most functions that answer a yes/no question (or a true/false situation) will return a boolean answer (or in the case of user-defined functions, they should be c
•Important: ANY C++ expression that evaluates to a value (i.e. any R-value) can be interpreted as a true/false condition. The rule is:
•If an expression evaluates to 0, its truth value is false
•If an expression evaluates to non-zero, its truth value is true
Logical Operators

The arithmetic comparison operators in C++ work much like the symbols we use in mathematics. Each of these operators returns a true or
a false. x == y // x is equal to y x != y // x is not equal to y x < y // x is less than y x <= y // x is less than or equal to y x > y // x is greater than y
x >= y // x is greater than or equal to y
We also have Boolean operators for combining expressions. Again, these operators return true or false
x && y // the AND operator -- true if both x and y are true x || y // the OR operator -- true if either x or y (or both) are true !x // the NOT operator
(negation) -- true if x is false
These operators will be commonly used as test expressions in selection statements or repetition statements (loops). commonly be used as test
expressions in selection
Examples of expressions

(x > 0 && y > 0 && z > 0) // all three of (x, y, z) are positive
(x < 0 || y < 0 || z < 0) // at least one of the three variables is negative
( numStudents >= 20 && !(classAvg < 70))
// there are at least 20 students and the class average is at least 70
( numStudents >= 20 && classAvg >= 70)
// means the same thing as the previous expression
The if/else Selection Statement

•The most common selection statement is the if/else statement. Basic syntax: if (expression) statement else statement
•The else clause is optional, so this format is also legal: if (expression) statement
Example

if (grade >= 68) cout << "Passing";


// Notice that there is no else clause. If the grade is below 68, we move on.
Some common errors

What's wrong with these if-statements? Which ones are syntax errors and which ones are logic errors?
•if (x == 1 || 2 || 3) cout << "x is a number in the range 1-3";
•if (x > 5) && (y < 10) cout << "Yahoo!";
•if (response != 'Y' || response != 'N') cout << "You must type Y or N (for yes or no)";
Increment decrement operators
• The increment ( ++ ) and decrement ( — ) operators in C++
are unary operators for incrementing and decrementing the
numeric values by 1 respectively. The incrementation and
decrementation are one of the most frequently used operations
in programming for looping, array traversal, pointer arithmetic,
and many more.
Increment Operator in C++
• The increment operator ( ++ ) is used to increment the value
of a variable in an expression by 1. It can be used on variables of
the numeric type such as integer, float, character, pointers, etc.
• Syntax
• // as prefix
• ++m
1+m

• //as postfix
• m++ m+1

• Where m is the variable


Pre-Increment
• In pre-increment, the increment operator is used as the prefix.
Also known as prefix increment, the value is incremented first
according to the precedence and then the less priority
operations are done.
• Result=++var1;
• Can be expanded as
• Var=var+1;
• Result=var;
Post-Increment
• In post-increment, the increment operator is used as the suffix
of the operand. The increment operation is performed after all
the other operations are done. It is also known as postfix
increment.
• Result=var1++;
• Can be expanded as
• Result=var;
• Var=var+1;
Example of Increment Operator
// C Program to illustrate the increment of both type
#include <stdio.h>

void increment()
{
int a = 5;
int b = 5;

// PREFIX a= 1 + a
int prefix = ++a;
printf("Prefix Increment: %d\n", prefix);

// POSTFIX a = a+1
int postfix = b++;
printf("Postfix Increment: %d", postfix);
}

// Driver code
int main()
{
increment();

return 0;
Output

Prefix Increment: 6
Postfix Increment: 5 first then 6
Decrement Operator in C
• The decrement operator is used to decrement the value of a variable
in an expression. In the Pre-Decrement, the value is first decremented
and then used inside the expression. Whereas in the Post-Decrement,
the value is first used inside the expression and then decremented.
• Syntax
• //as prefix
m = 1 -m
• --m
• As postix
• m—
• Where m is avariable
Pre-Decrement Operator
• The pre-decrement operator decreases the value of the variable
immediately when encountered. It is also known as prefix
decrement as the decrement operator is used as the prefix of
the operand.
• Example
m = m-1
• Result= --m;
• Can be expanded as
• m=m-1;
• Result=m;
Post-Decrement Operator
• The post-decrement happens when the decrement operator is
used as the suffix of the variable. In this case, the decrement
operation is performed after all the other operators are
evaluated.
• Example
• Result=m--;
• Can be expanded as
• Result=m;
• m=m-1;
Example of decrement operator
// C program to illustrate the decrement operator of both
// types
#include <stdio.h>

void decrement()
{
int a = 5;
int b = 5;

// PREFIX
int prefix = --a;
printf("Prefix = %d\n", prefix);

// POSTFIX
int postfix = b--;
printf("Postfix = %d", postfix);
}

// Driver code
int main()
{
decrement();

return 0;
}
Output
• Prefix = 4
• Postfix= 5 then 4
Order of precedence
• Already discussed in previous lecture
ASCII Value of a Character in C++
• American Standard Code for Information Interchange (ASCII) is
a character encoding standard that assigns a unique numerical
value to all characters including special symbols. In C
programming, the ASCII value of the character is stored instead
of the character itself. For example, the ASCII value of ‘A’ is 65.
• Each character or special character is represented by some ASCII
code.
• Each ASCII code occupies 7 bits in memory.
• Each character variable is assigned an ASCII value ranging from
0 to 127.

You might also like