CS208 C++ ProgrammingBoolean ExpressionsAlgebraic vs C++ Equality & Relational OperatorsDecisionsThe if StatementOne Alternative if StatementOne Alternative if Control StructureOne Alternative if ExamplesOne Alternative if Program DescriptionAge Program DesignAge Program AlgorithmAge Program CodeAge Program Explanation (1/3)Age Program Explanation (2/3)Age Program Explanation (3/3)Two Alternative if StatementTwo Alternative if-else Control StructureTwo alternative if ExamplesTwo Alternative Program Example DescriptionGas Program DesignGas Program AlgorithmGas Program Example CodeGas Program Explanation (1/3)Gas Program Explanation (2/3)Gas Program Explanation (3/3)Gas Program Modificationif ExerciseIf Exercise AnswerCompound StatementsLoop StatementsThe conditional while LoopThe while Statementwhile control structurewhile Loop ExampleCount by Twos Explanation (1/9)Count by Twos Explanation (2/9)Count by Twos Explanation (3/9)Count by Twos Explanation (4/9)Count by Twos Explanation (5/9)Count by Twos Explanation (6/9)Count by Twos Explanation (7/9)Count by Twos Explanation (8/9)Count by Twos Explanation (9/9)Loop Example #2Example #2 Outputwhile loop conditionsExample #2 Output RevisitedInfinite Loopswhile Loop Exercisewhile Loop Exercise SolutionDebuggingSlide 52Slide 53Slide 54Slide 55Slide 56Slide 57Programming01/14/19 1CS208 C++ Programming Part 22Boolean Expressions A boolean expression is a condition in which a relational operator tests the relationship between two expressions and returns a boolean (TRUE or FALSE) result. Syntax for condition: (<expr> <relop> <expr>)3Algebraic vs C++Equality & Relational Operators Algebraic operator C++ operator Example of C++ condition Meaning of C++ condition Equality operators = == x == y x is equal to y != x != y x is not equal to y Relational operators > > x > y x is greater than y < < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y NOTE that “=“ is for assignment and “==“ is for equality comparison4DecisionsBoolean expressions are used to make decisionsThe expression is evaluated to TRUE or FALSEExample:Given the boolean expression:( Num < 5 )When Num = 1, the expression evaluates to TRUEWhen Num = 25,the expression evaluates to FALSE5The if Statement Syntax: One Alternativeif (condition)statementT;//do the statement if condition is TRUETwo Alternativesif (condition)statementT ; // do only this if TRUEelse statementF ; // do only this if FALSE6One Alternative if Statementif ( condition ) statement;if is a C++reserved wordThe condition must be a boolean expression.It must be enclosed in parentheses.It must evaluate to either true or false.The statement is indented.If the condition is TRUE, the statement is executed.If it is FALSE, the statement is skipped.7One Alternative if Control StructureEnglish: if student’s grade is greater than 60, print "Passed"Grade over 60?print "Passed"truefalseCode: if (grade > 60)cout << "Passed";8One Alternative if Examplesif (Age < 18) cout << "Minor";if (Divisor != 0)Answer = Num / Divisor; if (Num < 10)Num = Num + 1;9One Alternative if Program DescriptionModify previous Age program (from part 1) to take into account the current MONTH, as well as the current YEAR.Read in the user’s birth MONTH and birth YEARCalculate and display the user’s age10Age Program DesignWhat are the program inputs?Year and Month of Birth needs variablesWhat are the program outputs?Age needs a variableAre there any values the programmer will set?Current year and month needs constants11Age Program AlgorithmPseudocode:Prompt for and read in user’s year of birthPrompt for and read in user’s month of birthCompute user’s ageIf birth month has not yet passed,Subtract one from user’s ageDisplay user’s age12Age Program Code#include <iostream.h>void main() { const int CurYr = 2005; const int CurMon = 9; int BirthYr, BirthMon, Age; cout << "Enter 4-digit year of birth: "; cin >> BirthYr; cout << "Enter numeric month of birth: "; cin >> BirthMon; Age = CurYr - BirthYr; if (BirthMon > CurMon)Age = Age – 1; cout << "Your age is " << Age;}13Age Program Explanation (1/3) int BirthYr, BirthMon, Age;/* declare 3 integer variables, BirthYr, BirthMon and Age */ Memory2005CurYr: CurMon: 10BirthYr: Age: BirthMon: const int CurYr = 2005; const int CurMon = 9;/* declare two constants, CurYr and CurMon */14Age Program Explanation (2/3)cout << "Enter 4-digit year of birth: ";cin >> BirthYr;/* display prompt, and read value entered into BirthYr */ 1962 10 cout << "Enter numeric month of birth: ";cin >> BirthMon; /* display prompt, and read value entered into BirthMon */Screen:Enter 4-digit year of birth: 1962Enter numeric month of birth: 10Memory2005CurYr: CurMon: 9 BirthYr: Age: BirthMon:15Age Program Explanation (3/3)Age = CurYr - BirthYr;/* subtract value in BirthYr from value in CurYr; store result into variable Age */Enter numeric month of birth: Screen:Enter 4-digit year of birth: 196210 1962 10 Memory2005CurYr: CurMon: 9 BirthYr: Age: BirthMon: 43 42 if (BirthMon > CurMon)Age = Age – 1;/* test to see if BirthMon is greater than CurMon if so, subtract 1 from value in variable Age */cout << "Your age is " << Age;// display message and calculated AgeYour age is 4216Two Alternative if Statementif ( condition )statementT;elsestatementF;The condition must be a boolean expression.It must be enclosed in parentheses.It must evaluate to either true or false.If the condition is TRUE, only statementT is executed.If it is FALSE, only statementF is executed.17Two Alternative if-else Control StructureGrade over 60?truefalseprint “Failed”print “Passed”; English: if students' grade is greater than 60, print "Passed“, otherwise print “Failed”Code: if (grade > 60) cout << "Passed";elsecout << "Failed";18Two alternative if Examplesif (Num >= 0)cout << "Positive";else cout << "Negative"; if (Temp < 50)Heater = 1;else Heater = 0;if (MorD == 'M')cout << "Hi Mom!"; elsecout << "Hi Dad!";19Two AlternativeProgram Example DescriptionAt a gas station, there are two kinds of gas: unleaded and premium. Unleaded is $1.89 per gallon, and premium is $1.98 per gallon. Design a program to read the type of gas and number of gallons from the user. The program should display the
View Full Document