C Programming Language: Practice 6 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

C Programming Language: Practice 6

Description:

C Programming Language: Practice 6. November 23, 2004. Overview. Loops. for and while loops ... www.iota-six.co.uk/c/ sizeof() operator ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 15
Provided by: WSE995
Category:

less

Transcript and Presenter's Notes

Title: C Programming Language: Practice 6


1
C Programming Language Practice 6
  • November 23, 2004

2
Overview
  • Loops
  • for and while loops
  • breaking from loops
  • Fundamental Data Types
  • sizeof() operator
  • casting examples
  • Counting Characters Example

3
Loops (for and while)
  • for ( expr1 expr2 expr3 ) statement
  • expr1while (expr2)   statement  expr3

for (i 0 i lt 10 i) printf ("i is d\n",
i)
No semicolon
i0while(ilt10) printf ("i is d\n", i)
i
4
Loops (while and do-while)
  • while (expr2)   statement  expr3

while(ilt10) printf ("i is d\n", i)
i
No semicolon
do  statement  expr3 while (expr2)
do printf ("i is d\n", i) i
while(ilt10)
Semicolon
5
Breaking from Loops
  • Break statement allows to exit out of a loop.
  • Useful when you want a loop to stop running
    because a condition has been met other than the
    loop end condition.

includeltstdio.hgtint main()    int
i0   while (i lt 10)      i      if (i
5)         break      return 0
Breaks from the lowest-level loop
6
Continue Statement
  • Continue statement allows to skip the rest of the
    current loop and start from the top again while
    incrementing the loop variable again.

includeltstdio.hgtint main() int i
for(i -10 i lt 10 i) if (i 0)
continue printf("f\n", 15.0/i)
return 0
7
include ltstdio.hgt include ltctype.hgt int
main(void) int c0 int line_num, count for
(line_num 0 (line_num lt 5) count0,
line_num) while ( ( c getchar() )
!'\n' ) if (c EOF) break if
(isalpha(c)) count printf(
"Line d contains d letters\n" , line_num, count
) return 0
Find an error in the code below
Breaks from the lowest-level loop
8
include ltstdio.hgt include ltctype.hgt int
main(void) int c 0 int line_num, count
0 for (line_num 0 ((line_num lt 5) (c !
EOF)) count0, line_num) while ( ( c
getchar() ) !'\n' ) if (c EOF)
break if (isalpha(c)) count
printf( "Line d contains d letters\n" ,
line_num, count ) return 0
The corrected code
Exits the outer loop
9
Fundamental Data Types
10
Casting
  • Casting means explicitly converting a variable by
    preceding the variable name by the desired type
    in parentheses.

int sum, countfloat average, pwaverage
(float)sum / countpw (float)pow((double)averag
e, 2.0)
11
Casting
int divided by int
int divided by float
Casting of 1
Casting of the integer result
www.iota-six.co.uk/c/
12
sizeof() operator
The sizeof() function gives the amount of
storage, in bytes, associated with a variable or
a type.
13
sizeof() operator
14
  • include ltstdio.hgt
  • int main(void)
  • int blank_cnt 0, c, digit_cnt 0,
    letter_cnt 0, nl_cnt 0, other_cnt 0
  • while ((c getchar()) ! EOF)
  • if (c ' ')
  • blank_cnt
  • else if (c gt '0' c lt '9')
  • digit_cnt
  • else if (c gt 'a' c lt 'z' c gt 'A'
    c lt 'Z')
  • letter_cnt
  • else if (c '\n')
  • nl_cnt
  • else
  • other_cnt
  • printf("10s10s10s10s10s10s\n\n",
  • "blanks", "digits", "letters", "lines",
    "others", "total")
  • printf("10d10d10d10d10d10d\n\n",
Write a Comment
User Comments (0)
About PowerShow.com