0% found this document useful (0 votes)
3 views10 pages

Week 9 FlowControl While

The document explains the concept of while loops in programming, detailing how they execute a block of code repeatedly as long as a specified condition is true. It provides several examples, including calculating the sum of numbers, finding the greatest common divisor, and implementing a guessing game. Additionally, it briefly compares while loops with for loops and introduces the do-while loop, which guarantees at least one execution of the loop body.

Uploaded by

luaa960324
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Week 9 FlowControl While

The document explains the concept of while loops in programming, detailing how they execute a block of code repeatedly as long as a specified condition is true. It provides several examples, including calculating the sum of numbers, finding the greatest common divisor, and implementing a guessing game. Additionally, it briefly compares while loops with for loops and introduces the do-while loop, which guarantees at least one execution of the loop body.

Uploaded by

luaa960324
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

while loop

1
while: If the condition is true, then do the loop. It can perform work
repeatedly (calculation, output and input...)

while ( A )
{

B;

}
• Repeat B when A is true

• If A is false at the
beginning, then B will not
be executed

2
Example
int main()
{
int a = 10;
while (a > 0)
{
cout << "hi, " ;
}
How to stop it?
}

3
Example

int i = 1, sum = 0; //起始值設定


while ( i <= 100 ) //當 i 不等於100則繼續執行
{
sum += i ;
i=i+1;
}
cout << “The sum is ” << sum

4
Example: greatest common divisor (GCD) 最大公因數
int a = 210, b = 150, temp = 0; // a: Dividend, b: Divisor, temp: Remainder

while ( b != 0 )
{
temp = a % b; // Find the remainder of dividing a by b
a = b; // Assign b (divisor) to a (dividend)
b = temp; // Assign the remainder to b (divisor)
}

cout << “GCD is ” << a ;

// finally, remainder=0, and b=temp=0, the loop is terminated.


5
例:計算全班平均成績
int score,sum;
cout<<"please input score (-1 to stop) "<<endl;
cin>>score;
sum=0;
while (score !=-1) ////////////


{
sum=sum+score;
cout<< " temp sum= " << sum<<endl;
cout<< " please input next score" ;
cin>> score;
}

cout<< " total score is " << sum;

6
You can use while or for.

while for
int i , n; int i , n;
cin >> n ; cin >> n ;
i = 1 ; for( i = 1 ; i <= n ; i = i+1 )
while ( i <= n ){ {
cout << i*i << endl ;
cout << i*i << endl }
;
i = i+1;
}

7
do-while

do{
B B
} while( A );
true

■ Do B first, if A is true, continue to do b A


until A is false
false
■ B will be executed at least once

8
Example: Guess the number
#include<iostream>
#include <time.h>
using namespace std;
int main()
{
srand( time(NULL) ); /* 設定亂數種子 */
int no , guess , i=0 ;
no = rand() % 5 + 1 ;
do {
cout << " Plese guess an integer between 1 and 5" << endl;
cin >> guess ;
i=i+1 ;
if ( no != guess ) cout << "Guess wrong" ;
} while( no != guess );
cout << "Yes, you answered "<< i << " times" << endl;
return 0;
} 9
Option: to find the square root of m.

10

You might also like