If/Else and While Loops - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

If/Else and While Loops

Description:

Amazon.com Program. 8/19/09. Ethan Cerami, NYU. 3. If Statement ... Amazon.com Series ... Amazon.com, Version 2.0. Feature Set (everything from Version 1.0 plus) ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 29
Provided by: Cer8
Learn more at: https://cs.nyu.edu
Category:
Tags: loops

less

Transcript and Presenter's Notes

Title: If/Else and While Loops


1
If/Else andWhile Loops
  • Ethan Cerami
  • New York University

2
This Week
  • Tuesday
  • Basics of If/Else Statement
  • Relational Operators
  • Increment/Decrement Operators
  • Thursday
  • While Loops
  • Amazon.com Program

3
If Statement
  • Control Structure a structure that controls the
    flow of a program.
  • If Statement
  • If some condition is true, do this.
  • Otherwise, do something else.
  • Every programming language has some form of an if
    statement.

4
Basic If Syntax
  • Basic If Example
  • if (grade gt 60)
  • printf (You passed!)
  • Basic If/Else Example
  • if (grade gt 60)
  • printf (You passed!)
  • else
  • printf (You failed!)

5
Understanding Flow Charts
  • Flow Chart a visual tool that helps you
    understand the flow of your program.
  • You can think of flow charts as pipes of water
  • You pour water in the top.
  • Water pours through the pipes and is pulled
    downward

6
Flow Chart Basics
Diamonds Represents conditions
Rectangles represent statements of work. For
example printf()
7
If/Else Flow Chart
                               
 
True
False
grade gt60
Print You passed
Print You failed
 
8
Understanding Blocks of Code
  • If you want to run several lines of code
    together, you must include them within a block of
    code.
  • Blocks of code are denoted with
  • For example
  • if (grade gt 60)
  • printf ("You passed!!!")
  • printf ("\nCongratulations!")

9
Understanding Indentation
  • Note that everything within the block of code
    should be indented. This helps you see the block
    at a quick glance.
  • Avoid writing code like this
  • if (grade gt 60)
  • printf ("You passed!!!")
  • printf ("\nCongratulations!")
  • This is valid C code, but it is not easy to view
    the block. Hence, it is considered bad style.

10
Example
  • / Introduction to If/Else Statements /
  • include ltstdio.hgt
  • include ltconio.hgt
  • main ()
  • int grade
  • printf ("Enter your course grade ")
  • scanf ("d", grade)
  • if (grade gt 60)
  • printf ("You passed!!!")
  • printf ("\nCongratulations!")
  • else
  • printf ("You failed!")
  • getch()

Note the indentation
11
Relational Operators
Operator Meaning
gt Greater than
lt Less than
gt Greater than or equal to
lt Less than or equal to
Equal to
! Not Equal to
12
Testing for Equality
  • To test for equality, use the operator.
  • For example
  • if (grade 100)
  • printf (Perfect Score!)
  • To test for inequality, use the ! operator.
  • For example
  • if (grade ! 100)
  • printf (Not perfect!)

13
Equality v. Assignment
  • A very common mistake
  • if (grade 100)
  • printf (Perfect Score!)
  • In this case, we are using a single character.
    (We really want to use )
  • Nonetheless, this is perfectly valid code. But
    what will it actually print?

14
Understanding Truth
  • In C (unlike life) truth is very simple.
  • 0 is False
  • Anything else is True
  • For example
  • -1 is true
  • 299 is true
  • O is false

15
For Example
  • / Understanding Truth /
  • include ltstdio.hgt
  • include ltconio.hgt
  • main ()
  • int x -100
  • int y 299
  • int z 0
  • if (x)
  • printf ("x d\n", x)
  • if (y)
  • printf ("y d\n", y)
  • if (z)
  • printf ("z d\n", z)
  • getch()

This is a completely valid program. Output x
-100 y 299 Note z is not printed because the
if condition fails.
16
Equality v. Assignment
  • Given
  • if (grade 100)
  • printf (Perfect Score!)
  • This statement does the following
  • Grade is assigned the value 100.
  • Because 100 is true, the condition is always
    true.
  • No matter what the student grade, it always says
    Perfect Score!

17
Amazon.com Series
  • Over the next couple of weeks, we will take a
    single program and make it more and more
    complicated.
  • Each class we will add some new feature to the
    program.
  • Examples are the best way to learn how to
    program.
  • Make sure to study these programs!

18
Amazon.com Series
  • The Amazon.com Series will simulate the
    purchasing of books.
  • For example
  • Prompt you for all the books you want to buy.
  • Calculate the total sales.
  • Print out a receipt.

19
Amazon.com Version 1.0
  • Feature Set
  • Prompt the user for 3 digit order
  • Prompt the user for cover price
  • Prompt the user for the of books
  • Calculate the total sales
  • If sales gt 100.00 send via Federal Express
  • else send via U.S. Mail

20
Program Snapshot
  • / E-Commerce Program for Amazon.com, Version
    1.0 /
  • / Created by Ethan Cerami, 1998 /
  • include ltstdio.hgt
  • include ltconio.hgt
  • main ()
  • int order_number, number_books
  • float price, sales
  • / Print Introduction /
  • printf ("Welcome to Amazon.com!\n")
  • printf ("Earth's Biggest Bookstore.\n\n")
  • / Get User Input /
  • printf ("Enter the three digit book order
    number ")
  • scanf ("d", order_number)
  • printf ("Enter the cover price ")
  • scanf ("f", price)

Now that we are working on bigger programs, its
hard to display them within Powerpoint(!) So,
please bring your Annotated Program packet.
21
Program, Page 1
  • / E-Commerce Program for /
  • / Amazon.com, Version 1.0 /
  • / Created by Ethan Cerami, 1998 /
  • include ltstdio.hgt
  • include ltconio.hgt
  • main ()
  • int order_number, number_books
  • float price, sales

Variable Declaration Note that price and sales
are defined as floats.
22
Program, Page 2
Prompt user for book order, cover price, and
number of copies.
  • / Print Introduction /
  • printf ("Welcome to Amazon.com!\n")
  • printf ("Earth's Biggest Bookstore.\n\n")
  • / Get User Input /
  • printf ("Enter the three digit book order number
    ")
  • scanf ("d", order_number)
  • printf ("Enter the cover price ")
  • scanf ("f", price)
  • printf ("Enter number of copies you would like to
    order ")
  • scanf ("d", number_books)

23
Program, Page 3
Use an if/else statement to determine the
shipping option.
  • / Calculate Total Sales /
  • sales price number_books
  • printf ("\nThank you. Your total order is
    .2f\n", sales)
  • / Determine Shipping Method /
  • if (sales gt 100.0)
  • printf ("Shipping Your order will be shipped
    via Federal Express.")
  • else
  • printf ("Shipping Your order will be shipped
    via U.S. Mail.")
  • printf ("\n\nPress any key to exit...")
  • getch()

24
Reminder
  • All the programs contained in the Annotated
    Programs packet are available on the web site.
  • You can therefore easily download them and try
    them on your own.
  • Learning by example is a great technique.

25
Amazon.com, Version 2.0
  • Feature Set (everything from Version 1.0 plus)
  • Prompt the user for a payment option (e.g.
    Mastercard, Visa, American Express.)
  • Use the if short-cut operator

26
Program, Page 1
  • / Get Credit Card Information /
  • printf ("Amazon.com accepts the following payment
    options\n")
  • printf ("1. American Express\n")
  • printf ("2. Mastercard\n")
  • printf ("3. Visa\n")
  • printf ("Please enter a payment option ")
  • scanf ("d", payment_option)

Here, we are prompting the user for a
payment option. This is stored in the
payment_option integer variable.
27
Program, Page 2
  • / Print Credit Card Confirmation /
  • printf ("This account balance will be charged to
    ")
  • if (payment_option 1)
  • printf ("American Express.\n")
  • else if (payment_option 2)
  • printf ("Mastercard.\n")
  • else if (payment_option 3)
  • printf ("Visa.\n")

Here, we use a series of if/else statements to
print out the payment option.
28
Program, Page 3
  • C Provides a shortcut if/else operator
  • printf ("Shipping Your order will be shipped
    via ")
  • printf ("s", (sales gt 100.0) ? "Federal
    Express." "U.S. Mail.")

s is the format specifier for text strings.
The conditional Statement.
True Option
False Option
? short- cut operator
Write a Comment
User Comments (0)
About PowerShow.com