0% found this document useful (0 votes)
5 views

Week_3_Control_Structures_Part_1

The document outlines key programming concepts including Boolean expressions, branching mechanisms (if-else, switch), and loops (while, do-while, for). It explains the evaluation of Boolean expressions, operator precedence, and common pitfalls in syntax. Additionally, it provides examples of if-else statements and nested statements to illustrate their usage in programming.
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)
5 views

Week_3_Control_Structures_Part_1

The document outlines key programming concepts including Boolean expressions, branching mechanisms (if-else, switch), and loops (while, do-while, for). It explains the evaluation of Boolean expressions, operator precedence, and common pitfalls in syntax. Additionally, it provides examples of if-else statements and nested statements to illustrate their usage in programming.
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/ 20

Learning Objectives

 Boolean Expressions
 Building, Evaluating & Precedence Rules
 Branching Mechanisms
 if-else
 switch
 Nesting if-else
 Loops
 While, do-while, for
 Nesting loops
Copyright © 2002 Pearson Education, Inc. Slide 3
Boolean Expressions
 Logical Operators
 Logical AND (&&)
 Logical OR (||)
Display 2.1,
page 46

Copyright © 2002 Pearson Education, Inc. Slide 4


Evaluating Boolean Expressions
 Data type bool
 Returns true or false
 true, false are predefined library consts
 Truth tables
 Display 2.2 next slide

Copyright © 2002 Pearson Education, Inc. Slide 5


Evaluating Boolean Expressions
(cont.)
Display 2.2,
page 48

Copyright © 2002 Pearson Education, Inc. Slide 6


Precedence of Operators
Operator
()
Prefix (++ var , -- var)
*, /, %
arithmetic
+, -
<, <=, >, >=
relational
==, !=
&&
||
logical
=
Postfix (var--, var++)

Copyright © 2002 Pearson Education, Inc. Slide 7


Precedence of Operators (cont.)
 F=5.4*2-13.6+18/2;
 Integer is changed into floating point
 Final result is floating point
 Implicit conversion
 If int n=2; evaluate the following: (n+2)*(++n)+5;

Copyright © 2002 Pearson Education, Inc. Slide 8


Precedence Examples
 Arithmetic before logical
 x + 1 > 2 || x + 1 < -3 means:
(x + 1) > 2 || (x + 1) < -3
 Short-circuit evaluation
 (x >= 0) && (y > 1)
 Be careful with increment operators!
 (x > 1) && (y++)
 Integers as boolean values
 All non-zero values  true
 Zero value  false Slide 9
Copyright © 2002 Pearson Education, Inc.
Precedence Examples (cont)
 Determine the value, true or false, of each of the
following Boolean expressions, assuming count = 0
and limit = 10.
 (count == 0) && (limit < 20)
 (limit > 20) || (count < 5)
 !(count == 12)
 ((limit/count) > 7) && (limit < 0)

Copyright © 2002 Pearson Education, Inc. Slide 10


Modulus operator
 % Modulus operator returns remainder
 7 % 5 evaluates to 2

 Attempting to use the modulus operator (%) with


non-integer operands is a compilation error.

Copyright © 2002 Pearson Education, Inc. Slide 11


Branching Mechanisms
 if-else statements
 Choice of two alternate statements based
on condition expression
 Example:
if (hrs > 40)
grossPay = rate*40 + 1.5*rate*(hrs-40);
else
grossPay = rate*hrs;

Copyright © 2002 Pearson Education, Inc. Slide 12


if-else Statement Syntax
 Formal syntax:
if (<boolean_expression>)
<yes_statement>
else
<no_statement>
 Note each alternative is only ONE
statement!
 To have multiple statements execute in
either branch  use compound statement

Copyright © 2002 Pearson Education, Inc. Slide 13


Compound/Block Statement
 Only ‘get’ one statement per branch
 Must use compound statement { }
for multiples
 Also called a ‘block’ stmt
 Each block should have block statement
 Even if just one statement
 Enhances readability

Copyright © 2002 Pearson Education, Inc. Slide 14


Compound Statement in Action
 Note indenting in this example:
if (myScore > yourScore)
{
cout << “I win!\n”;
wager = wager + 100;
}
else
{
cout << “I wish these were golf scores.\n”;
wager = 0;
}
Copyright © 2002 Pearson Education, Inc. Slide 15
Common Pitfalls
 Operator ‘=‘ vs. operator ‘==‘
 One means ‘assignment’ (=)
 One means ‘equality’ (==)
 VERY different in C++!
 Example:
if (x = 12) Note operator used!
Do_Something
else
Do_Something_Else

Copyright © 2002 Pearson Education, Inc. Slide 16


The Optional else
 else clause is optional
 If, in the false branch (else), you want
‘nothing’ to happen, leave it out
 Example:
if (sales >= minimum)
salary = salary + bonus;
cout << “Salary = %” << salary;
 Note: nothing to do for false condition, so
there is no else clause!
 Execution continues with cout statement
Copyright © 2002 Pearson Education, Inc. Slide 17
Nested Statements
 if-else statements contain smaller
statements
 Compound or simple statements (we’ve seen)
 Can also contain any statement at all,
including another if-else stmt!
 Example:
if (speed > 55)
if (speed > 80)
cout << “You’re really speeding!”;
else
cout << “You’re speeding.”;
 Note proper indenting!
Copyright © 2002 Pearson Education, Inc. Slide 18
Multiway if-else
 Not new, just different indenting
 Avoids ‘excessive’ indenting
 Syntax:
Display
page 60

Copyright © 2002 Pearson Education, Inc. Slide 19


Multiway if-else Example
Display page 60

Copyright © 2002 Pearson Education, Inc. Slide 20


If statement Examples
#include <iostream>
using namespace std;
void main()
{
float x;
cout<<"enter a number";
cin>>x;
if(x>0)
cout<<"the number is positive";
else if(x<0)
cout<<"the number is negative";
else
cout<<"the number is zero";

system("pause");
} © 2002 Pearson Education, Inc.
Copyright Slide 21
If statement Examples (cont)
 Write a program that will input one character then
print if its small letter, capital letter or special
character.

Copyright © 2002 Pearson Education, Inc. Slide 22

You might also like