1 cs 192 lecture 9 winter 2003 december 19, 2003 dr. shafay shamail
Post on 21-Dec-2015
216 views
TRANSCRIPT
![Page 1: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/1.jpg)
1
CS 192
Lecture 9
Winter 2003
December 19, 2003
Dr. Shafay Shamail
![Page 2: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/2.jpg)
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 <iostream.h>int main(){
int count; //loop control variablefor (count=1; count<=100; count++)
cout << count << " ";return 0;
}
![Page 3: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/3.jpg)
3
The for Loop• Output?
for (count=10; count < 5; count++) cout << count;
• A square root calculating program #include <iostream.h> #include <cmath> // for older compilers, use <math.h> int main() { int num;
for(num=1; num < 100; num++) {
cout << num << " " << sqrt((double) num) << '\n';}
return 0; }
![Page 4: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/4.jpg)
4
Variations of the for Loop int main() {
for(int i=100; i >= -100; i = i-5) cout << i << ' ';
return 0; } ================================
for(x=0, y=10; x<=10; ++x, --y) cout << x << ' ' << y << '\n'; ================================
#include <iostream.h> #include <conio.h> int main() { int i;
// print numbers until a key is pressed for(i=0; !kbhit(); i++) cout << i << ' ';
return 0; }
![Page 5: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/5.jpg)
5
Variations of the for Loop int x; for(x=0; x != 123; ) { cout << "Enter a number: "; cin >> x; } ================================
int x = 0;for( ; x<10; ){
cout << x << ' '; ++x;
} ================================
for(;;) {
body of loop }
![Page 6: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/6.jpg)
6
The while Loop• Repetition construct
– it’s a for loop stripped of the initialization and update parts
while (test-condition){
statement(s)}
for ( ;test-condition; ) { statement(s) }
initialize counterwhile (test-condition){ statement(s);
update counter;}
![Page 7: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/7.jpg)
7
The while Loop• Might use it if do not know beforehand when to stop
repeating int n = 99; //why initialize? cout << “Guess my fav number”; while(n != 0) cin >> n; cout << “Yes, I was looking for 0 \n”;
================================
unsigned char ch; ch = 1; while(ch) { //when does it terminate? cout << ch; ch++; } //displays what?
![Page 8: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/8.jpg)
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 << "Enter a number (100 to stop): ";
cin >> num;
} while (num != 100);
![Page 9: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/9.jpg)
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
![Page 10: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/10.jpg)
10
The continue Statement• continue, break and goto are jump statements.
• continue used in loops and causes program to skip rest of loop’s 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 << "Enter dividend: "; cin >> dividend;
cout << "Enter divisor: "; cin >> divisor;
if (divisor == 0) //divide by zero error
{
cout << "Illegal divisor\n";
continue;
}
cout << "Quotient is " << dividend/divisor;
cout << " , remainder is " << dividend%divisor;
cout << "\nDo another? (y/n): ";
cin >> ch;
} while (ch != 'n');
![Page 11: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/11.jpg)
11
The continue Statement• Output of following? #include <iostream.h> int main() { int x;
for(x=0; x<=100; x++) { if(x%2) continue; cout << x << ' '; } return 0; }
![Page 12: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/12.jpg)
12
The break Statement• Causes exit from loop or switch #include <iostream.h> #include <cmath> int main() { double x;
while (1) { cout << "Enter number for square root: "; cin >> x;
if (x <0.0) break; //exit loop if x is -ve cout << sqrt(x) << endl; }// break jumps to here cout << "Sorry, can't do that\n"; return 0; }
![Page 13: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/13.jpg)
13
The break Statement int t;
for(t=0; t<100; t++)
{
if(t==10)
break;
cout << t << ' ';
} ================================
for(i=0; i<1000; 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
![Page 14: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/14.jpg)
14
The goto Statement• Unconditional branch statement that jumps to a label (identifier)• goto label; …
label: statement(s); ================================
int x = 1; loop1: cout << x << endl; x++; if(x < 100) goto loop1; ================================
goto end; int i = 10; //error: goto skips initialization end: ;
![Page 15: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/15.jpg)
15
Example
Read and understand the magic number program given at the end of the chapter 4
![Page 16: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/16.jpg)
16
while
#include <iostream.h>int main(){ int count_down;
cout << "How many greetings do you want? "; cin >> count_down;
while (count_down > 0) { cout << "Hello "; count_down = count_down - 1; }
cout << endl; cout << "That's all!\n";
return 0;}
![Page 17: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/17.jpg)
17
#include <iostream.h>int main(){ char ans;
do { cout << "Hello\n"; cout << "Do you want another greeting?\n" << "Press y for yes, n for no,\n" << "and then press return: "; cin >> ans; } while (ans == 'y' || ans == 'Y');
cout << "Good-Bye\n";
return 0;}
do .. while
![Page 18: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/18.jpg)
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 << "\nThe number of spaces is: " << numofspaces << endl;}
![Page 19: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/19.jpg)
19
Counting Space Characters(do..while)
#include <stdio.h> // getchar()
void countSpaces2(void)
{
int ch = 0;
int numofspaces = 0;
printf("Enter a sentence: ");
do {
ch = getchar();
if ( ' ' == ch )
numofspaces++;
} while ( ch != '\n' );
cout << "\nThe number of spaces is: " << numofspaces << endl;
}
![Page 20: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/20.jpg)
20
Make Number from Digits
#include <stdio.h> // getchar()#include <ctype.h> // isdigit()
void makeInteger(void){
int num=0;int digit=0;
printf("Enter a number: ");
digit = getchar();
for( ; isdigit(digit) ; digit=getchar() ){
num = num * 10;num = num + (digit - '0');
}
cout << "The number is: " << num << endl;}
![Page 21: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/21.jpg)
21
Skip Spaces
#include <ctype.h> // header file for isspace()
for (int = getchar(); isspace( c ) ; c = getchar() );
ungetc( c, stdin ); // put the non-space char
// back in the input buffer
![Page 22: 1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d635503460f94a466e6/html5/thumbnails/22.jpg)
22
Skip Spaces#include <stdio.h> // getchar()#include <ctype.h> // isdigit(), isspace()
void skipSpaces(void){
int c = 0;int count = 0;
cout << "\nSkipping Spaces\n\n\n";cout.flush();
for ( c=getchar(); isspace(c); c=getchar() ){
count++;}ungetc(c, stdin);
cout << count << " 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