Вы находитесь на странице: 1из 6

Assignment C++

Programming
a)
b)
c)
d)
e)
f)
g)

Go to
While
Break and Continue
While True
Do / While
Jump / Loop
If / Else

a.

/* C++ program to demonstrate the working of goto statement. */

/* This program calculates the average of numbers entered by user. */


/* If user enters negative number, it ignores that number and
calculates the average of number entered before it.*/

# include <iostream>
using namespace std;
int main() {
float num, average, sum = 0.0;

int i, n;
cout<<"Maximum number of inputs: ";
cin>>n;

for(i=1; i <= n; ++i) {


cout<<"Enter n"<<i<<": ";
cin>>num;

if(num < 0.0) {


goto jump; /* Control of the program moves to jump; */
}
sum += num;
}

jump:
average=sum/(i-1);
cout<<"\nAverage = "<<average;
return 0;

Maximum number of inputs: 10


Enter n1: 2.3
Enter n2: 5.6
Enter n3: -5.6

Average = 3.95

b.

// while_statement.cpp

#include <string.h>
#include <stdio.h>

char *trim( char *szSource )


{
char *pszEOS = 0;
// Set pointer to character before terminating NULL
pszEOS = szSource + strlen( szSource ) - 1;
// iterate backwards until non '_' is found
while( (pszEOS >= szSource) && (*pszEOS == '_') )
*pszEOS-- = '\0';
return szSource;
}
int main()
{
char szbuf[] = "12345_____";
printf_s("\nBefore trim: %s", szbuf);
printf_s("\nAfter trim: %s\n", trim(szbuf));
}

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.

// C++ Program to demonstrate working of break statement

#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)

//Do My Loop Stuf

I think that this may be easier to read and is definitely the standard for use in C#:

e.

#include <iostream>

using namespace std;


int main() {
int number, i = 1, factorial = 1;
cout<< "Enter a positive integer: ";
cin >> number;
while ( i <= number) {
factorial *= i;
//factorial = factorial * i;
++i;
}

cout<<"Factorial of "<<number<<" = "<<factorial;


return 0;

Enter a positive integer: 4


Factorial of 4 = 24
In this program, user is asked to enter a positive integer which is stored in variable
number (supposed user entered 4). Here is the working of while loop in this program:

1.

Initially, i = 1, test expression i <= number is true, factorial becomes 1.

2.

Variable i is updated to 2, test expression is true, factorial becomes 2.

3.

Variable i is updated to 3, test expression is true, factorial becomes 6.

4.

Variable i is updated to 4, test expression is true, factorial becomes 24.

5.

Variable i is updated to 5, test expression is false and while loop is


terminated.

f.

The following example shows how to write a simple for loop in C++/CLI. This

example has exactly the same efect as the while loop.


for (int count = 1; count <= 5; count++)
{
Console::WriteLine(count * count);
}
Console::WriteLine("The end");
The parentheses after the for keyword contain three expressions separated by
semicolons. The first expression performs loop initialization, such as initializing the loop
counter. This initialization expression is executed once only, at the start of the loop.

1.
2.

Continue working with the project from the previous exercise.


Modify the code in the main function to use a for loop rather than a while loop, as
shown here:

3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

Console::WriteLine("Welcome to your calendar assistant");


for (int count = 1; count <= 5; count++)
{
Console::Write("\nPlease enter date ");
Console::WriteLine(count);
int year = GetYear();
int month = GetMonth();
int day = GetDay(year, month);
DisplayDate(year, month, day);
}
Notice that there is no count++ statement after displaying the date. This is because
the forstatement takes care of incrementing the loop counter.

14.

Build and run the application. The application asks you to enter five dates, as
before.

g.

#include <iostream>

using namespace std;


int main() {
int number;
cout<< "Enter an integer: ";
cin>> number;
if ( number >= 0) {
cout << "You entered a positive integer: "<<number<<endl;
}
else {
cout<<"You entered a negative integer: "<<number<<endl;
}
cout<<"This statement is always executed because it's outside if...else
statement.";
return 0;
}

Enter an integer: -4
You entered a negative integer: -4
This statement is always executed because it's outside if...else statement.

The end

Вам также может понравиться