Professional Documents
Culture Documents
Slide 2
Introduction
Three program structures: Sequence
Statements in the given order
Slide 3
Structured programming
Any program can be written as a sequence of three basic program structures!!! 1. sequences, 2. conditionals, 3. and iterations
Slide 4
Action2
Slide 5
if <it's sunny>{ <go to beach with sun block> } else{ <go to beach with umbrella> }
Slide 6
Slide 7
Slide 8
Slide 9
Slide 10
Slide 11
< >
less than less than or equal to greater than greater than or equal to not equal to
Slide 12
Relational Expressions
Examples:
numberOfStudents < 200 10 > 20 20 * j == 10 + i
Slide 13
Logical AND operator Logical OR operator Logical NOT operator (x>5) && (x<10) (x>10) || (x<5) !(x>5)
&& || !
Examples:
Warning!
Slide 14
A Boolean Type
C++ contains a type (new!!!) named bool which can have one of two values
true false
Example logical expressions bool P = true; bool Q = false; bool R = true; bool S = P && Q; bool T = !Q || R; bool U = !(R && !Q);
Slide 15
Operator Precedence
Which comes first? ( ) / >= != =
Answer:
<
* + <= ==
% >
Slide 16
arithmetic relational
logical
Parentheses Unary operators Multiplicative operators Additive operators Relational ordering Relational equality Logical and Logical or Assignment
Slide 17
Example:
Slide 18
Boolean expressions
Arithmetic expression: use arithmetic operators +,,*,/, to produce a number as the final result Boolean expression: use relational operators <,>, ==, and boolean operators AND (&&), OR (||), NOT (!) to produce one of the two values true (1) and false (0) as the final result
New type: bool, true, false Old versions of C++, simulated boolean type by int with 0/1 Example: bool cond; cond = true; cond = (x>y);
If (BOOLEAN EXPRESSION)
Slide 19
Slide 20
The if Statement
Syntax
if(Expression) Action Expression
If the Expression is true then execute Action Action is either a single statement or a group of statements within braces Example: absolute value
if(value < 0) value = -value;
true
false
Action
Slide 21
Slide 22
int value1; int value2; int temp; cout << "Enter two integers: "; cin >> value1 >> value2; if(value1 > value2){ temp = value1; value1 = value2; value2 = temp; } cout << "The input in sorted order: " << value1 << " " << value2 << endl;
Slide 23
Nested if Statements
Slide 24
Nested if Statements
Example:
if <it's Monday>{ if <it's 9:00 am>{ if <it's raining>{ <bring umbrella> } <go to COMP 104> } <call your friends> }
Slide 25
if-else-if Statements
if <condition 1 exists>{ <do Q> } else if <condition 2 exists>{ <do R> } else if <condition 3 exists>{ <do S> } else{ <do T> }
Q R S T
Slide 26
if <Mon, Wed AM>{ <goto ICSM121> } else if <Tues, Thurs AM>{ <goto MTHS001> } else if <1PM or 7PM>{ <eat> } else{ <sleep> }
Slide 27
Selection
Often we want to perform a particular action depending on the value of an expression Two ways to do this
if-else-if statement
if-else
switch statement
An
advanced construct
Slide 28
switch Statement
An alternative way of writing nested if-else-if statements for multiple choice.
switch (Expression) { case constant_1: action1; break; case constant_2: action2; break; case constant_n: actionN; break; default: actionDefault } if Exp==constant_1 { action1; } else if Exp==constant_2 { action2; } else if { } else{ actionDefault }
Slide 29
Slide 30
Another example:
int left; int right; char oper; cout << "Enter simple expression: "; cin >> left >> oper >> right; cout << left << " " << oper << " " << right << " = "; switch (oper) { case '+' : cout << left + right << endl; break; case '-' : cout << left - right << endl; break; case '*' : cout << left * right << endl; break; case '/' : cout << left / right << endl; break; default: cout << "Illegal operation" << endl; }
Slide 32
Slide 33
C++ groups a dangling else with the most recent if. The following indentation shows how C++ would group this example (answer: c=1).
int a=-1, b=1, c=1; if(a>0) if(b>0) c = 2; else // dangling else grouped to nearest if c = 3;
Slide 34
Use extra brackets { } to clarify the intended meaning, even if not necessary.
int a=-1, b=1, c=1; if(a>0){ if(b>0) c = 2; else // parenthesis avoid dangling else c = 3; }
Slide 35
The fundamental If-else statement if, nested if, if-else-if, switch Bool type and boolean expression for a test true/false relational operators (==, <, ) logical operators (&&, ||, !) Full table of operator precedence (c++ technicality)