Cis162ab c
Download
1 / 39

CIS162AB - C++ - PowerPoint PPT Presentation


  • 70 Views
  • Uploaded on

CIS162AB - C++. Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt). Overview of Topics. Pseudocode Control Structures Flowcharts Single and Compound Boolean Expressions Single and Compound Statements If, if-else, nested ifs While, do-while, nested loops. Pseudocode.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CIS162AB - C++' - serafina-mauro


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Cis162ab c

CIS162AB - C++

Flow Control if, while, do-while

Juan Marquez(03_flow_control.ppt)


Overview of topics
Overview of Topics

  • Pseudocode

  • Control Structures

  • Flowcharts

  • Single and Compound Boolean Expressions

  • Single and Compound Statements

  • If, if-else, nested ifs

  • While, do-while, nested loops


Pseudocode
Pseudocode

  • Pseudocode is a mixture of C++ code and English like statements.

  • Used when designing algorithms.

  • When designing, we don’t necessarily want to be concerned about where semi-colons go. We want to concentrate on the design.

  • I’ll use pseudocode throughout the course, so don’t feel compelled to correct my syntax.

  • However, if at anytime you are not sure about a command, please be sure to ask for clarification. 


Flow control

The order in which statements are executed.

There are four control structures.

Sequence Control Structure

Selection Control Structure

Also referred to as branching (if and if-else)

Repetition Control Structure (loops)

Case Control Structure (switch)

Flow Control


Flowcharting
Flowcharting

  • A flowchartis a pictorial representation of an algorithm or logical steps.

  • Each step is represented by a symbol and the arrows indicate the flow and order of the steps.

  • The shape of the symbol indicates the type of operation that is to occur.

  • Flowcharts may help the move visual students learn and understand logic.


Flowchart symbols
Flowchart Symbols

Input or Output

Begin or End

Decision

Processing

Branch or Direction of Flow


1 sequence control structure
1. Sequence Control Structure

  • The order statements are placed (sequenced)

    cin >> price >> qty;

    subtotal = price * qty;

    cout << subtotal;

  • The only way to display the subtotal, statements must be in this order.


Flowchart sequence control
Flowchart – Sequence Control

Begin

Input price, qty

subtotal = price * qty;

Output subtotal

End


2 selection control if
2. Selection Control ( if )

Simple if with single statement.

discountRate = 1.00;

if (qty > 25)

discountRate = .98;

subtotal = qty * (price * discountRate);


Flowchart if statement
Flowchart – if statement

discountRate = 1.00;

If qty > 25

True

discountRate = .98;

False

Subtotal = qty * (price * discountRate);


Selection control if else
Selection Control (if-else)

  • Simple if-else with single statements.

    DISCOUNT_RATE = .98;

    if (qty > 25)

    subtotal = qty * (price * DISCOUNT_RATE);

    else

    subtotal = qty * price;

    cout << subtotal;


Flowchart if else statement
Flowchart – if-else statement

DISCOUNT_RATE = .98;

If qty > 25

Subtotal = qty * (price * DISCOUNT_RATE);

True

False

Subtotal = qty * price;

Output subtotal


Boolean expressions
Boolean Expressions

  • Boolean expressions evaluate to true or false.

  • Must be enclosed in parenthesis

    if (Boolean expression)

    true statements (Yes)

    else

    false statements (No)


Compound statements
Compound Statements

  • Use braces to create a block of statements.

    if (hours > 40)

    {

    regularPay = 40 * payRate;

    overtimePay = (hours – 40) * (payRate * OVERTIME_RATE);

    }

    else

    {

    regularPay = hours * payRate;

    overtimePay = 0;

    }

    grossPay = regularPay + overtimePay;


Incorrect if else
Incorrect if-else

if (hours > 40)

{

regularPay = 40 * payRate;

overtimePay = (hours – 40) * (payRate * OVERTIME_RATE);

}

else

regularPay = hours * payRate;

overtimePay = 0;

grossPay = regularPay + overtimePay;

  • overtimePay would always be set to zero.


Boolean expressions1
Boolean Expressions

  • Simple Expressions

    • Have one comparison

    • (hours > 40)

  • Compound Expressions

    • Have more than one comparison

    • Created by using And and Or operators

    • And - (qty > 0 && qty < 51)

    • Or - (qty < 0 || qty > 51)


And operator
And Operator - &&

  • Both conditions must be true.

    if (qty > 0 && qty < 51)

    {

    subtotal = qty * price;

    }

    else

    cout << “Quantity must be 1 – 50.”;

  • What happens if qty = 0, qty = 25, or qty = 60?

  • A block with a single statement is valid.


Or operator
Or Operator - | |

  • Either condition must be true.

    if (qty < 1 || qty > 50)

    {

    cout << “Quantity must be 1 -50.”;

    }

    else

    subtotal = qty * price;

  • What happens if qty = 0, qty = 25, or qty = 60?


Pipe character
Pipe Character - |

  • Where is the pipe character on the keyboard?

  • On most keyboards

    • It is right above the Enter key

    • Shares the key with the back slash - \

    • Must hold the shift key go get it

    • Instead of a solid line, it is shown as a broken line

  • For the Or operator, 2 pipe characters must be entered - | |.

  • For the And operator, 2 ampersands characters must be entered - &&.


Notes on boolean expressions
Notes on Boolean Expressions

  • Short-circuit evaluation – if the evaluation of the entire expression can be determined by the result of the first expression, the second expression is not evaluated.int y =1, x =0;if (y > 0 | | x > 0) // T or F

  • Not operator (!):( y != 0) //This statement is OK, but the not( !(y= =0)) //outside the parentheses can be confusing.


Booleans only true or false
Booleans Only True or False

  • C++ does the math to get to a True or False.

  • true = 1, false = 0

  • Non-zeros converted to true, which is a one.

      if ( (5 && 7) + (!6) )

    T && T + (!T)

    T + F

    1 + 0 = 1 = T


Nested if statements indented
Nested if Statements - Indented

if (qty > 0)

if (qty < 51)

subtotal = qty * price;

else

cout << “Quantity must be < 51”;

else

cout << “Quantity must be > 0”;


Matching else to if
Matching else to if

How are else statements matched with an if?

Compiler works it’s way back. When an else is encounter, it looks back to find an if that has not been matched to an else.

Why do we indent each level?

We indent to make programs easier to read. Indenting has no effect on how compiler matches an else to an if.


Multiway branching indenting not always practical
Multiway BranchingIndenting not always practical

if (qty < 26)

discount = 0

else

if (qty < 51)

discount = .01

else

if (qty < 76)

discount = .02

else

if (qty < 101)

discount = .03 … more


Multiway branching
Multiway Branching

if (qty < 26)

discount = 0

else if (qty < 51)

discount = .01

else if (qty < 76)

discount = .02

else if (qty < 101)

discount = .03

else

discount = .04;


Conditional operator expression
Conditional Operator Expression

if (qty > 25)

discountRate = 0.98;

else

discountRate = 1.00;

discountRate = (qty > 25) ? .98 : 1.00;


Assignment vs comparison
Assignment (=) vs Comparison (==)

  • if (x = 12) // always true

  • if (x == 12) // comparison

  • To prevent accidental assignment, state constant first.

  • if (12 = x)

  • Compiler will report this as a syntax error.


3 repetition control loops
3. Repetition Control (loops)

  • loop – a group of statements that are repeated until a certain conditions occurs to stop it.

  • while loop

  • do-while loop

  • for-loop (covered later)


While loop example
While Loop Example

count = 3; //initialize controlling variable

while (count > 0)

{

cout << count;

count--;

}

Output:

3 2 1 


While loop
While Loop

  • Controlling Boolean expression evaluated before executing loop body.

  • Controlling variable must be initialized.

  • Boolean expression must be true to enter loop body.

  • No semicolon after Boolean expression.

  • It is possible that body is not executed at all.

  • Boolean expression must be false to get out of loop.

  • Controlling variable must be modified inside loop.

  • Execution continues with next statement after loop.


Flowchart while loop
Flowchart – While Loop

Initialization important for While Loop

count = 3

while count > 0

False

True

Output count

Represents Loop

Skip or Exit Loop

count--

Next statement


Do while loop example
Do-while Loop Example

count = 3;

do

{

cout << count;

count--;

}while (count > 0);

Output:

3 2 1


Do while loop
Do-while Loop

  • Controlling Boolean expression evaluated after executing body.

  • So, body is always executed at least one time.

  • Initialization of controlling variable not necessarily required if it will be initialized inside the loop.

  • Boolean expression must be false to get out of loop.

  • Controlling variable must be modified inside loop.

  • Semicolon required after Boolean expression.

  • Execution continues with next statement after loop.


Flowchart do while loop
Flowchart – Do-while Loop

count = 3

Output count

Loop will be executed at least one time, because the condition is at the bottom.

count--

Represents Loop

while count > 0

True

False - Exit Loop

Next statement


Loop summary
Loop Summary

  • Incrementor: count++

  • Decrementor: count--

  • Infinite loop – expression that always evaluates to true

    • Avoid using = = as the operator.

    • Controlling variable must be altered within the loop.

    • Use Control-C to terminate an infinite loop.

  • When to use a while or do-while will become evident as we continue to use and learn each loop.


Nested loops
Nested Loops

count = 3;

while (count > 0)

{

cout << endl << count;

count--;

count2 = 1;

do

{

cout << count2;

count2++;

}while (count2 < 4);

}

3

1 2 3

2

1 2 3

1

1 2 3


While loop error
While Loop Error

  • Semi-colon after expression creates an infinite loop with a single statement that does nothing.

  • DO NOT DO THIS.

    while (count > 0);

    {

    cout << count;

    count--;

    }


4 case control structure
4. Case Control Structure

  • Switch statement covered later.


Summary
Summary

  • Control Structures

  • Flowcharts

  • Single and Compound Boolean Expressions

  • Single and Compound Statements

  • If, if-else, and nested ifs

  • While, do-while, and nested loops