Sie sind auf Seite 1von 6

ASSIGNMENT : C++ PROGRAMMING

QUESTIONS
1. Who is written C++ ?
Answers : Bjarne Stroustrup

2. State statements below and give an example


application in C++ Program.
a. Go to
Answers :

Example 1: goto Statement


/* 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;
}

b. While
Answers :

Example:
#include <iostream>
using namespace std;

int main ()
{
// Local variable declaration:
int a = 10;

// while loop execution


while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}

return 0;
}

c. Break and Continue


Answers :
(BREAK):

Example 1: C++ break


C++ program to add all number entered by user until user enters 0.
// 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;

(CONTINUE)

Example 2: C++ continue


C++ program to display integer from 1 to 10 except 6 and 9.
// C++ Program to demonstrate working of continue statement
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; ++i) {
if ( i == 6 || i == 9) {
continue;
}
cout<<i<<"\t";
}
return 0;
}

d. While True
Example :
1 #include <iostream>
2 using namespace std;
3
4 int main (int argc, char * const argv[]) {
5
6
int counter = 9;
7
8
while (true) {
9
10
cout << "Counter: " << counter << endl;;
11
counter--;
12
13
if (counter == 0) {
14
break;
15
}
16
17
}
18
19
return 0;
20
}
21

e. Do/While
Anwers :
Example :
// do_while_statement.cpp
#include <stdio.h>
int main()
{
int i = 0;
do
{
printf_s("\n%d",i++);
} while (i < 3);
}

f. Jump/Loop
example :
break;

Edit & Run

continue;
return [expression];
goto identifier;

g. If/else
EXAMPLE :
#include <iostream>
using namespace std;
int main(){
int mark;
cout << "What mark did you get in the test?" << endl;
cin >> mark;
if(mark >= 90)
{
cout << "You got an A*" << endl;
cout << "You Passed!" << endl;
}
else if(mark >= 80)
{
cout << "You got an A" << endl;
cout << "You Passed!" << endl;
}
else if(mark >= 70)
{
cout << "You got an B" << endl;
cout << "You Passed!" << endl;
}
else if(mark >= 60)
{
cout << "You got an C" << endl;
cout << "You Passed!" << endl;
}
else if(mark >= 50)
{
cout << "You got an D" << endl;
cout << "You Failed!" << endl;
}
else if(mark >= 40)
{

cout << "You got an E" << endl;


cout << "You Failed!" << endl;
}
else
{
cout << "You got an U" << endl;
cout << "You Failed!" << endl;
}
return 0;
}

Das könnte Ihnen auch gefallen