Escolar Documentos
Profissional Documentos
Cultura Documentos
Loops
1
The for Loop
• Repetition
– construct to execute a section of code a specified number of times
int main()
{
int num;
return 0;
}
3
Variations of the for Loop
int main()
{
for(int i=100; i >= -100; i = i-5)
cout << i << ' ';
return 0;
}
================================
#include <iostream.h>
#include <conio.h>
int main()
{
int i;
return 0;
}
4
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
}
5
The while Loop
• Repetition construct
– it’s a for loop stripped of the initialization and update parts
while (test-condition)
{
statement(s)
}
initialize counter
while (test-condition)
{ statement(s);
update counter;
}
6
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”;
================================
7
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);
8
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;}
int main()
{
int x;
#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;
} 12
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
13
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:
14
;
Example
15
#include <iostream.h>
int main()
{
int count_down;
while
cout << "How many greetings do you want? ";
cin >> count_down;
return 0;
} 16
#include <iostream.h>
int main()
{
char ans; do .. while
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');
return 0;
}
17
void countSpaces1(void) Counting Space
{
int ch = 0;
Characters
int numofspaces = 0; (while)
while ( ch != '\n' )
{
if ( ' ' == ch )
numofspaces++;
ch = getchar();
}
cout << "\nThe number of spaces is: " << numofspaces << endl;
}
18
#include <stdio.h> // getchar()
Counting Space
void countSpaces2(void)
Characters
{
int ch = 0;
(do..while)
int numofspaces = 0;
do {
ch = getchar();
if ( ' ' == ch )
numofspaces++;
} while ( ch != '\n' );
cout << "\nThe number of spaces is: " << numofspaces << endl;
} 19
#include <stdio.h> // getchar()
#include <ctype.h> // isdigit() Make Number
void makeInteger(void) from Digits
{
int num=0;
int digit=0;
digit = getchar();
cout << "The number is: " << num << endl; 20
}
Skip Spaces
21
#include <stdio.h> // getchar()
#include <ctype.h> // isdigit(), isspace()
Skip Spaces
• What is the
void skipSpaces(void)
{ behavior of this
int c = 0; program
int count = 0;
• What is the
cout << "\nSkipping Spaces\n\n\n"; function of
cout.flush()
for ( c=getchar(); isspace(c); c=getchar() ) • What will happen
{
if it is removed
count++;
}
• What does
ungetc() do
cout << count << " spaces skipped\n";
} 22