chapter 4 october 22, 2013
DESCRIPTION
Chapter 4 October 22, 2013. The If Statement. Programs make decisions If(condition){ Statement(s); } Condition boolean expression Evaluates to either true or false formed using relational operators. The If Statement. OperatorMeaning = = equal to < less than - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/1.jpg)
Chapter 4
October 22, 2013
![Page 2: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/2.jpg)
The If Statement Programs make decisions If(condition){
Statement(s); }Condition
boolean expression Evaluates to either true or false
formed using relational operators
![Page 3: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/3.jpg)
The If StatementOperator Meaning = = equal to< less than <= less than or equal to > greater than >= greater than or equal to != not equal to
![Page 4: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/4.jpg)
Statement single or complex Directions on what to do
Pitfalls do not use = = or ! = with decimals roundoff errors
occur because doubles cannot be exactly represented in binary
The If Statement
![Page 5: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/5.jpg)
Only compare values of the same type
Do not confuse assignment (=) with equal to (==). Will compile but output will vary
Misplaced semicolons if(score ==21);
The If Statement
![Page 6: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/6.jpg)
If-Else StatementContains an else clause
Excuted when if is false If(condition){
statement(s);
}
else{
statement(s);
}
![Page 7: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/7.jpg)
More than one statement enclosed in { } Ex: if(temp > 5){
cout<<“Wear a coat!”<<endl;
cout<<“It is cold outside”<<endl;
}
Compound Statements
Nested if Statements
Controls flow in 3 or more situations if statement within an if statement
![Page 8: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/8.jpg)
If statement within an if statement If(condition){
if(condition){
statement(s);
}
} Ex: if(temp < 5){
if(temp< 2){
cout<<“Wear a coat”<<endl;
}
}
Nested if Statements
![Page 9: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/9.jpg)
Dangling else A logic error associated with
nested if statements reason we use brackets
clarify and group
Nested if Statements
Ex: if(temp < 5)if(temp< 2)
cout<<“Wear a coat”<<endl; else cout<<“It’s hot”<<endl;
![Page 10: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/10.jpg)
Else-if LadderDecides between three or more
actions order of if and else is very importantLast statement is executed iff the
statements before ALL fail. Ex: if(temp < 0)
else if (temp< 2)
else if (temp < 4)
else
![Page 11: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/11.jpg)
if(condition){ statement(s);}
else if(condition){ statement(s);}
else{}
Else-if Ladder
![Page 12: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/12.jpg)
Logical Operators
Used to form Boolean expressions && represents and | | represents orEvaluated based on the following
rules
![Page 13: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/13.jpg)
True/False Operator T/F Expressions
T && T T
T && F F
F && T F
F && F F
Logical Operators
&&- AND
![Page 14: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/14.jpg)
OR –If example If((condition) | | (condition)){
statement(s);
}
If((temp < 10) | | (wind> 20 )){ cout<<“It’s really cold!!”<<endl; }
![Page 15: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/15.jpg)
AND –If example If((condition) && (condition)){
statement(s);
}
If((temp < 10) && (wind> 20 )){ cout<<“It’s too cold to be outside”;
cout<< endl; }
![Page 16: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/16.jpg)
Looping
Control program through iteration Repeat one or more
statements Referred to as looping
![Page 17: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/17.jpg)
Do-whiledo {
statement(s);
}while(condition);Condition
Boolean expression Determines if loop continues
![Page 18: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/18.jpg)
Do-while executed at least once
condition executed after first loop
if condition is true then statements are executed again and condition is reevaluated.
![Page 19: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/19.jpg)
while Statements evaluates before each loop
Can be executed 0 or more times while(condition){
statement(s);
} if the condition is true then the
statements are executed and the condition is reevaluated until condition is false.
![Page 20: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/20.jpg)
Infinite loops
Continue FOREVER Causes
misplaced semicolons missing curly brackets logic errors
![Page 21: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/21.jpg)
Algorithms series of steps that tell how to solve a
problem usually written out first helps with structure of code helps eliminate logic errors
caused by statements that are syntactically correct but produce undesired results
![Page 22: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/22.jpg)
Counting and Summing Counting
counts the number of values entered by the user
really counts number of loops numOfValues = numOfValues + 1;
takes values currently in variable, adds one to it and re-stores that sum in the original variable.
![Page 23: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/23.jpg)
Counting and Summing
Counting counters
variables that count need to be initialized gives it a starting value; usually 0 int i = 0;
![Page 24: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/24.jpg)
Counting and Summing Summing
sums the values entered by a user sumOfValues = sumOfValues +
value; takes value currently stored and adds the new value and re-stores it back in sumOfValues
needs to be initialized usually zero.
![Page 25: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/25.jpg)
Counting and Summing
Sentinel constant value the loop should end on
Easily changed
![Page 26: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/26.jpg)
Increment and Decrement++ after a variable indicates addition of
1 numOfValues++;
adds one and re-stores new value in numOfValues
increment operator Where C++ came from
![Page 27: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/27.jpg)
Increment and Decrement+= adds a value to a sum sumOfValues +=Value;
adds value to sumOfValues stores new value in sumOfValues.
![Page 28: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/28.jpg)
Increment and Decrement -- subtraction of 1 from variable total --;
subtracts 1 from total stores back in total
- = subtracts a value from a total total -=value;
subtracts value from total Stores new value in total
![Page 29: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/29.jpg)
The for Statment executes a loop for a fixed number of times
for(initialization; condition; increment){
statement(s);
} initialization
performed once type counter = startingPoint;
int x = 0;
![Page 30: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/30.jpg)
The for Statement
Condition Boolean Expression Evaluated before each loop
increment performed after each loop Advances the counter
![Page 31: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/31.jpg)
The for Statement
Initialization can be performed outside the loop ex: int x = 0;
for(; condition; increment){
statement(s);
}
![Page 32: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/32.jpg)
#include <iostream> using namespace std;
int main(){
for ( int x = 0; x < 10;x++) { cout<< x <<endl; }
}
![Page 33: Chapter 4 October 22, 2013](https://reader034.vdocuments.site/reader034/viewer/2022051516/56812c8b550346895d913ba6/html5/thumbnails/33.jpg)
The bool Library Boolean variables only hold true or
false library used to implement bool type #include<bool>
using namespace std; bool Tuesday = false;
no quotations marks