Академический Документы
Профессиональный Документы
Культура Документы
Programming
a)
b)
c)
d)
e)
f)
g)
Go to
While
Break and Continue
While True
Do / While
Jump / Loop
If / Else
a.
# include <iostream>
using namespace std;
int main() {
float num, average, sum = 0.0;
int i, n;
cout<<"Maximum number of inputs: ";
cin>>n;
jump:
average=sum/(i-1);
cout<<"\nAverage = "<<average;
return 0;
Average = 3.95
b.
// while_statement.cpp
#include <string.h>
#include <stdio.h>
The test of expression takes place before each execution of the loop; therefore,
a while loop executes zero or more times. expression must be of an integral type, a
pointer type, or a class type with an unambiguous conversion to an integral or pointer
type.
A while loop can also terminate when a break,goto or return within the statement
body is executed. Use continue to terminate the current iteration without exiting
the while loop.continue passes control to the next iteration of the while loop.
c.
#include <iostream>
using namespace std;
int main() {
float number, sum = 0.0;
while (true) {
// test expression is always true
cout<<"Enter a number: ";
cin>>number;
if (number != 0.0) {
sum += number;
}
else {
break; // terminating the loop if number equals to 0.0
}
}
cout<<"Sum = "<<sum;
return 0;
}
Enter a number: 4
Enter a number: 3.4
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6
d.
{
while(true)
I think that this may be easier to read and is definitely the standard for use in C#:
e.
#include <iostream>
1.
2.
3.
4.
5.
f.
The following example shows how to write a simple for loop in C++/CLI. This
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Build and run the application. The application asks you to enter five dates, as
before.
g.
#include <iostream>
Enter an integer: -4
You entered a negative integer: -4
This statement is always executed because it's outside if...else statement.
The end