CS 192 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

CS 192

Description:

print numbers until a key is pressed. for(i=0; !kbhit(); i ) cout ... No hard rule; all interchangeable mostly; matter of style. 10. The continue Statement ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 23
Provided by: Sham4
Category:

less

Transcript and Presenter's Notes

Title: CS 192


1
CS 192
  • Lecture 9
  • Winter 2003
  • December 19, 2003
  • Dr. Shafay Shamail

2
The for Loop
  • Repetition
  • construct to execute a section of code a
    specified number of times
  • for (initialization test-condition increment)
  • statement(s)
  • (1) Set initial value of counter.
  • (2) Perform test to see if loop should continue.
  • (3) Execute statements.
  • (4) Update counter.
  • (5) Repeat from (2).
  • include ltiostream.hgt
  • int main()
  • int count //loop control variable
  • for (count1 countlt100 count)

3
The for Loop
  • Output?
  • for (count10 count lt 5 count)
  • cout ltlt count
  • A square root calculating program
  • include ltiostream.hgt
  • include ltcmathgt // for older compilers, use
    ltmath.hgt
  • int main()
  • int num
  • for(num1 num lt 100 num)
  • cout ltlt num ltlt " " ltlt sqrt((double) num) ltlt
    '\n'
  • return 0

4
Variations of the for Loop
  • int main()
  • for(int i100 i gt -100 i i-5)
  • cout ltlt i ltlt ' '
  • return 0
  • for(x0, y10 xlt10 x, --y)
  • cout ltlt x ltlt ' ' ltlt y ltlt '\n'
  • include ltiostream.hgt
  • include ltconio.hgt
  • int main()
  • int i

5
Variations of the for Loop
  • int x
  • for(x0 x ! 123 )
  • cout ltlt "Enter a number "
  • cin gtgt x
  • int x 0
  • for( xlt10 )
  • cout ltlt x ltlt ' '
  • x
  • for()
  • body of loop

6
The while Loop
  • Repetition construct
  • its a for loop stripped of the initialization
    and update parts
  • while (test-condition)
  • statement(s)
  • for ( test-condition ) statement(s)
  • initialize counter
  • while (test-condition)
  • statement(s)
  • update counter

7
The while Loop
  • Might use it if do not know beforehand when to
    stop repeating
  • int n 99 //why initialize?
  • cout ltlt Guess my fav number
  • while(n ! 0)
  • cin gtgt n
  • cout ltlt Yes, I was looking for 0 \n
  • unsigned char ch
  • ch 1
  • while(ch)
  • //when does it terminate?
  • cout ltlt ch
  • ch
  • //displays what?

8
The do-while Loop
  • Similar to while, except that tests condition
    after the body of the loop i.e. statements
    executed at least once
  • do
  • statement(s)
  • while (test-condition)
  • int num
  • do
  • cout ltlt "Enter a number (100 to stop) "
  • cin gtgt num
  • while (num ! 100)

9
Which Loop to Use?
  • if (number of repetitions known)
  • use for
  • else if (body to be executed once at least)
  • use do while
  • else use while
  • No hard rule all interchangeable mostly matter
    of style

10
The continue Statement
  • continue, break and goto are jump statements.
  • continue used in loops and causes program to skip
    rest of loops body and start the next iteration
    immediately. Actually takes you to the closing
    brace of the loop body
  • long dividend, divisor
  • char ch
  • do
  • cout ltlt "Enter dividend " cin gtgt dividend
  • cout ltlt "Enter divisor " cin gtgt divisor
  • if (divisor 0) //divide by zero error
  • cout ltlt "Illegal divisor\n"
  • continue
  • cout ltlt "Quotient is " ltlt dividend/divisor
  • cout ltlt " , remainder is " ltlt
    dividenddivisor
  • cout ltlt "\nDo another? (y/n) "
  • cin gtgt ch
  • while (ch ! 'n')

11
The continue Statement
  • Output of following?
  • include ltiostream.hgt
  • int main()
  • int x
  • for(x0 xlt100 x)
  • if(x2)
  • continue
  • cout ltlt x ltlt ' '
  • return 0

12
The break Statement
  • Causes exit from loop or switch
  • include ltiostream.hgt
  • include ltcmathgt
  • int main()
  • double x
  • while (1)
  • cout ltlt "Enter number for square root "
  • cin gtgt x
  • if (x lt0.0)
  • break //exit loop if x is -ve
  • cout ltlt sqrt(x) ltlt endl
  • // break jumps to here
  • cout ltlt "Sorry, can't do that\n"

13
The break Statement
  • int t
  • for(t0 tlt100 t)
  • if(t10)
  • break
  • cout ltlt t ltlt ' '
  • for(i0 ilt1000 i)
  • // do something
  • if(kbhit())
  • break
  • // keypress stops execution
  • break causes exit from the innermost loop or
    switch, and not from the enclosing loops or
    switches

14
The goto Statement
  • Unconditional branch statement that jumps to a
    label (identifier)
  • goto label
  • label statement(s)
  • int x 1
  • loop1
  • cout ltlt x ltlt endl
  • x
  • if(x lt 100)
  • goto loop1
  • goto end
  • int i 10 //error goto skips initialization
  • end

15
Example
  • Read and understand the magic number program
    given at the end of the chapter 4

16
while
  • include ltiostream.hgt
  • int main()
  • int count_down
  • cout ltlt "How many greetings do you want? "
  • cin gtgt count_down
  • while (count_down gt 0)
  • cout ltlt "Hello "
  • count_down count_down - 1
  • cout ltlt endl
  • cout ltlt "That's all!\n"
  • return 0

17
do .. while
  • include ltiostream.hgt
  • int main()
  • char ans
  • do
  • cout ltlt "Hello\n"
  • cout ltlt "Do you want another greeting?\n"
  • ltlt "Press y for yes, n for no,\n"
  • ltlt "and then press return "
  • cin gtgt ans
  • while (ans 'y' ans 'Y')
  • cout ltlt "Good-Bye\n"
  • return 0

18
Counting Space Characters(while)
  • void countSpaces1(void)
  • int ch 0
  • int numofspaces 0
  • printf("Enter a sentence ")
  • ch getchar()
  • while ( ch ! '\n' )
  • if ( ' ' ch )
  • numofspaces
  • ch getchar()
  • cout ltlt "\nThe number of spaces is " ltlt
    numofspaces ltlt endl

19
Counting Space Characters(do..while)
  • include ltstdio.hgt // getchar()
  • void countSpaces2(void)
  • int ch 0
  • int numofspaces 0
  • printf("Enter a sentence ")
  • do
  • ch getchar()
  • if ( ' ' ch )
  • numofspaces
  • while ( ch ! '\n' )
  • cout ltlt "\nThe number of spaces is " ltlt
    numofspaces ltlt endl

20
Make Number from Digits
  • include ltstdio.hgt // getchar()
  • include ltctype.hgt // isdigit()
  • void makeInteger(void)
  • int num0
  • int digit0
  • printf("Enter a number ")
  • digit getchar()
  • for( isdigit(digit) digitgetchar() )
  • num num 10
  • num num (digit - '0')
  • cout ltlt "The number is " ltlt num ltlt endl

21
Skip Spaces
  • include ltctype.hgt // header file for isspace()
  • for (int getchar() isspace( c ) c
    getchar() )
  • ungetc( c, stdin ) // put the non-space char
  • // back in the input buffer

22
Skip Spaces
  • include ltstdio.hgt // getchar()
  • include ltctype.hgt // isdigit(), isspace()
  • void skipSpaces(void)
  • int c 0
  • int count 0
  • cout ltlt "\nSkipping Spaces\n\n\n"
  • cout.flush()
  • for ( cgetchar() isspace(c) cgetchar() )
  • count
  • ungetc(c, stdin)
  • cout ltlt count ltlt " spaces skipped\n"
  • What is the behavior of this program
  • What is the function of cout.flush()
  • What will happen if it is removed
  • What does ungetc() do
Write a Comment
User Comments (0)
About PowerShow.com