CS 192 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CS 192

Description:

test-condition is an expression that evaluates to true or false ... cout 'Goodbye.' endl; 7. Nested ifs. Another example #include iostream.h int main ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 15
Provided by: Sham4
Category:
Tags: goodbye

less

Transcript and Presenter's Notes

Title: CS 192


1
CS 192
  • Lecture 8
  • Winter 2003
  • December 17-18, 2003
  • Dr. Shafay Shamail

2
The if Statement
  • Its a selection or decision statement
  • if (test-condition) statement
  • test-condition is an expression that evaluates to
    true or false
  • If the condition is true, then the statement will
    execute otherwise not
  • if (10 lt 11) cout ltlt hello ltlt endl
  • cout ltlt I am after if
  • Can have a block of statements instead of just
    one
  • Pitfall if(marks 100) cout ltlt Congrats!
  • Write if(100 marks). Now compiler catches
    single

3
The if else Statement
  • Choice between two alternatives
  • Pseudocode
  • if(ltexpressiongt)
  • lttrue_statementsgt
  • else
  • ltfalse_statementsgt
  • If ltexpressiongt is TRUE (non-zero), then
    lttrue_statementsgt are executed
  • If ltexpressiongt is FALSE (zero), then
    ltfalse_statementsgt are executed

4
The if else Statement
  • Example ATM Withdrawal program
  • include ltiostream.hgt
  • int main()
  • int balance 10000
  • int withdrawal
  • cout ltlt "Please enter the withdrawal amount "
  • cin gtgt withdrawal
  • if(withdrawal lt balance)
  • cout ltlt "Here is your money." ltlt endl
  • else
  • cout ltlt "Sorry, insufficient balance." ltltendl
  • return 0

5
Nested ifs
  • An if statement that is the target of another if
    or an else
  • An else refers to the nearest if statement within
    the same block that doesnt have an else
  • if(i)
  • if(j) statement1
  • if(k) statement2 // this if
  • else statement3 // is associated with //
    this else
  • else
  • statement4 // associated with?

6
Nested ifs
  • The ltstatementsgt inside the braces can contain
    any valid C statements, including other if-else
    statements!
  • if (withdrawal gt balance)
  • cout ltlt "Insufficient funds." ltlt endl
  • cout ltlt "Do you want to see your
    balance?(y/n) "
  • cin gtgt answer
  • if (answer 'y')
  • cout ltlt "Your balance is "ltltbalanceltlt endl
  • else
  • cout ltlt "Ok then. "
  • else

7
Nested ifs
  • Another example
  • include ltiostream.hgt
  • int main()
  • int a, b, c
  • cout ltlt Enter three numbers a, b, c\n
  • cin gtgt a gtgt b gtgt c
  • if (ab)
  • if(bc)
  • cout ltlt a, b, c are equal\n
  • else
  • cout ltlt a and b are different\n
  • return 0
  • Let a2, bc3. Whats printed?

8
The if-else-if Construction
  • More than 2 choices. if else is itself a
    statement, so can follow an else
  • if (ch A)
  • agrade //choice 1
  • else
  • if (ch B) //choice 2
  • bgrade
  • else
  • SoSo //choice 3
  • Easier to read format
  • if (ch A)
  • agrade //choice 1
  • else if (ch B)
  • bgrade //choice 2
  • else
  • SoSo //choice 3

9
The if-else-if Ladder
  • Conditional expressions evaluated top-down The
    first true conditions statements evaluated rest
    of the ladder skipped
  • int num_credits
  • coutltlt"Please enter workload "
  • cingtgtnum_credits
  • if(num_credits lt 0)
  • cout ltlt "Come on, get real" ltlt endl
  • else if (num_credits lt 12)
  • cout ltlt "Part-time student" ltlt endl
  • else if (num_credits lt 18)
  • cout ltlt "Full-time student" ltlt endl

10
The switch Statement
  • If a large number of choices that all depend on
    one expressions value, better to use switch than
    the if-else-if construct
  • switch (expression)
  • case constant1
  • statement(s)
  • break
  • case constant2
  • statement(s)
  • break
  • default
  • statement(s)
  • break // not required
  • expression must evaluate to an integer value
    (includes characters) constant must be integer
    (or character) constant

11
The switch Statement
  • int main()
  • int x1
  • switch (x)
  • case 0
  • coutltlt"zero \n"
  • break
  • case 1
  • coutltltone \n"
  • break
  • default
  • coutltltsorry! \n"
  • break
  • return 0
  • char c M switch(c). What case will match
    now?

12
The switch Statement
  • int main()
  • int x1
  • switch (x)
  • case 0
  • coutltlt"zero \n"
  • break
  • case 1
  • coutltltone \n"
  • default
  • coutltltsorry! \n
  • return 0
  • break necessary to exit from switch, otherwise
    all subsequent executed

13
The switch Statement
  • break and default are optional but important.
  • No two cases can have same value
  • Can have same statements for multiple cases
  • int medal
  • coutltlt"Enter your rank in class "
  • cingtgtmedal
  • switch (medal)
  • case 1 case 2 case 3
  • coutltlt"Wow! Medal winner \n"
  • break
  • default
  • coutltlt"Better luck next time \n"

14
The switch Statement
  • int main()
  • int i
  • for(i0 ilt5 i)
  • switch(i)
  • case 0 cout ltlt "less than 1\n"
  • case 1 cout ltlt "less than 2\n"
  • case 2 cout ltlt "less than 3\n"
  • case 3 cout ltlt "less than 4\n"
  • case 4 cout ltlt "less than 5\n"
  • cout ltlt '\n'
  • return 0
  • Output?
Write a Comment
User Comments (0)
About PowerShow.com