0% found this document useful (0 votes)
28 views30 pages

W2 3 Flow of Control

Uploaded by

mohammad3mhamdy
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)
28 views30 pages

W2 3 Flow of Control

Uploaded by

mohammad3mhamdy
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/ 30

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.

Chapter 2
C++ Basics

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Overview

2.1 Variables and Assignments

2.2 Input and Output

2.3 Data Types and Expressions

2.4 Simple Flow of Control

2.5 Program Style

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 3
2.4
Simple Flow of Control

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Simple Flow of Control

◼ Flow of control
◼ The order in which statements are executed

◼ Branching
◼ Lets program choose between two alternatives

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 5
Branch Example: Worker Weekly Pay

◼ To calculate weekly wages there are two choices:


◼ Regular time ( up to 40 hours)

◼ gross_pay = rate * hours;

◼ Overtime ( over 40 hours)


◼ gross_pay = rate * 40 + 1.5 * rate * (hours - 40);

◼ The program must choose which of these


expressions to use

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 6
Implementing the Branch

◼ if-else statement is used in C++ to perform a


branch

if (hours > 40)


gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
else
gross_pay = rate * hours;
Display 2.8
Display 2.9

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 7
Boolean Expressions
◼ Boolean expressions are expressions that are
either True or False
◼ Comparison operators such as '>' (greater than)
are used to compare variables and/or numbers
◼ (hours > 40) Including the parentheses, is the
boolean expression from the wages example
◼ A few of the comparison operators that use two
symbols (No spaces allowed between the symbols!)
◼ >= greater than or equal to
◼ != not equal or inequality
◼ == equal or equivalent
Display 2.10
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Slide 2- 8
if-else Flow Control (1)

◼ if (boolean expression)
true statement
else
false statement
◼ When the boolean expression is true
◼ Only the true statement is executed

◼ When the boolean expression is false


◼ Only the false statement is executed

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 9
if-else Flow Control (2)
◼ if (boolean expression)
{
true statements
}
else
{
false statements
}
◼ When the boolean expression is true
◼ Only the true statements enclosed in { } are executed

◼ When the boolean expression is false


◼ Only the false statements enclosed in { } are executed

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 10
Boolean Operators: AND

◼ Boolean expressions can be combined into more complex


expressions with different operators
◼ && -- The AND operator
◼ True only if both expressions are true
◼ Syntax: (Comparison_1) && (Comparison_2)
◼ Example: if ( (2 < x) && (x < 7) )
◼ True only if x is between 2 and 7

◼ Inside parentheses are optional but enhance meaning

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 11
OR
◼ | | -- The OR operator (no space!)
◼ True if either or both expressions are true

◼ Syntax: (Comparison_1) | | (Comparison_2)

◼ Example: if ( ( x = = 1) | | ( x = = y) )
◼ True if x contains 1

◼ True if x contains the same value as y

◼ True if both comparisons are true

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 12
NOT

◼ ! -- negates any boolean expression


◼ !( x < y)

◼ True if x is NOT less than y

◼ !(x = = y)
◼ True if x is NOT equal to y

◼ ! Operator can make expressions difficult to


understand…use only when appropriate

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 13
Inequalities

◼ Be careful translating inequalities to C++


◼ if x < y < z translates as

if ( ( x < y ) && ( y < z ) )

NOT

if ( x < y < z )

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 14
Pitfall: Using = or ==

◼ ' = ' is the assignment operator


◼ Used to assign values to variables

◼ Example: x = 3;
◼ ’== ' is the equality operator
◼ Used to compare values

◼ Example: if ( x == 3)
◼ The compiler will accept this error:
if (x = 3)
but stores 3 in x instead of comparing x and 3
◼ Since the result is 3 (non-zero), the expression is
always True

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 15
Compound Statements
◼ A compound statement is more than one statement
enclosed in { }
◼ Branches of if-else statements often need to
execute more than one statement
◼ Example: if (boolean expression)
{
true statements
}
else
{
Display 2.11
false statements
}

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 16
Branches Conclusion

◼ Can you
◼ Write an if-else statement that outputs the word
High if the value of the variable score is greater
than 100 and Low if the value of score is at most
100? The variables are of type int.

◼ Write an if-else statement that outputs the word


Warning provided that either the value of the variable
temperature is greater than or equal to 100, OR the
value of the variable pressure is greater than or equal
to 200, or both. Otherwise, the if_else statement
outputs the word OK. The variables are of type int.

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 17
2.5
Program Style

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Program Style

◼ A program written with attention to style


◼ is easier to read

◼ easier to correct

◼ easier to change

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 19
Program Style - Indenting

◼ Items considered a group should look like a group


◼ Skip lines between logical groups of statements

◼ Indent statements within statements

if (x = = 0)
statement;
◼ Braces {} create groups
◼ Indent within braces to make the group clear

◼ Braces placed on separate lines are easier to locate

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 20
Program Style - Comments
◼ // is the symbol for a single line comment
◼ Comments are explanatory notes for the programmer

◼ All text on the line following // is ignored by the


compiler
◼ Example: //calculate regular wages
gross_pay = rate * hours;
◼ /* and */ enclose multiple line comments
◼ Example: /* This is a comment that spans
multiple lines without a
comment symbol on the middle line
*/

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 21
Program Style - Constants

◼ Number constants have no mnemonic value


◼ Number constants used throughout a program
are difficult to find and change when needed
◼ Constants
◼ Allow us to name number constants so they have

meaning
◼ Allow us to change all occurrences simply by

changing the value of the constant

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 22
Constants
◼ const is the keyword to declare a constant
◼ Example:
const int WINDOW_COUNT = 10;
declares a constant named WINDOW_COUNT
◼ Its value cannot be changed by the program
like a variable
◼ It is common to name constants with all
capitals

Display 2.17
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Slide 2- 23
Section 2.5 Conclusion
◼ Can you
◼ Create a named constant of type double?

◼ Determine if a program can modify the value of a


constant?

◼ Describe the benefits of comments?

◼ Explain why indenting is important in a program?

◼ Explain why blank lines are important in a program?

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 24
Chapter 2 -- End

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 25
Display 2.8
Back Next

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 26
Display 2.9 Back Next

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 27
Display 2.10 Back Next

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 28
Display 2.11 Back Next

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 29
Display 2.17
Back Next

Copyright © 2018 Pearson Addison-Wesley. All rights reserved.


Slide 2- 30

You might also like