ASSIGNMENT C++
QUESTIONS
1. Who is Written C++
C++ programming is one of the most popular and widely used object-oriented programming
language which was developed by Bjarne Stroustrup in 1979. C++ is derived from C
programming. So, almost all code that run on C runs correctly on C++. If you have good
understanding of basic features of C programming, you will have a head start learning C++.
2. State statements below and give an example
application in C++
a. Go to
// goto_statement.cpp
#include <stdio.h>
int main()
{
int i, j;
for ( i = 0; i < 10; i++ )
{
printf_s( "Outer loop executing. i = %d\n", i );
for ( j = 0; j < 2; j++ )
{
printf_s( " Inner loop executing. j = %d\n", j );
if ( i == 3 )
goto stop;
}
}
// This message does not print:
printf_s( "Loop exited. i = %d\n", i );
stop:
printf_s( "Jumped to stop. i = %d\n", i );
b. While
How while loop works in C++ Programming?
The while loop checks whether the test expression is true or not. If it is true, code/s inside
the body of while loop is executed,that is, code/s inside the braces { } are executed. Then
again the test expression is checked whether test expression is true or not. This process
continues until the test expression becomes false.
Example 1: C++ while Loop
C++ program to find factorial of a positive integer entered by user. (Factorial of n =
1*2*3...*n)
#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;
}
Output
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.
C++ do...while Loop
The do...while Loop is similar to while loop with one very important difference. In while loop,
check expression is checked at first before body of loop but in case of do...while loop, body
of loop is executed first then only test expression is checked. That's why the body of
do...while loop is executed at least once.
Syntax of do...while Loop
do {
statement/s;
}
while (test expression);
How do...while loop works?
The statement/s inside body of loop is executed at least once, that is, the statement/s
inside braces { } is executed at least once. Then the test expression is checked. If the test
expression is true, the body of loop is executed. This process continues until the test
expression becomes false. Since the body of loop is placed before the test expression
in do...while loop, the body of loop is executed at least once.
c. Break and continue
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;
}
Output
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 am writting a program that gives us the whole and the decimal part of the number given by the user.
The program then asks the user if he wants to restart the program, this is a that of the code and if
someone could please explain me what does the while(true) loop do.
1 while (true)
2
{
3
4
if (started != -1)
5
{
6
cout << "Do you want to restart the program? (1 - yes, 0 7 no)" << endl;
8
while (started < 0 || started > 1)
9
cin >> started;
10
if (started == 0)
11
12
break;
}
e. Do/While
C++ while Loop
Syntax of while Loop
while (test expression) {
statement/s to be executed.
How while loop works in C++ Programming?
The while loop checks whether the test expression is true or not. If it is true, code/s inside
the body of while loop is executed,that is, code/s inside the braces { } are executed. Then
again the test expression is checked whether test expression is true or not. This process
continues until the test expression becomes false.
Flowchart of while Loop in C++
Example 1: C++ while Loop
C++ program to find factorial of a positive integer entered by user. (Factorial of n =
1*2*3...*n)
#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;
Output
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.
C++ do...while Loop
The do...while Loop is similar to while loop with one very important difference. In while loop,
check expression is checked at first before body of loop but in case of do...while loop, body
of loop is executed first then only test expression is checked. That's why the body of
do...while loop is executed at least once.
Syntax of do...while Loop
do {
statement/s;
}
while (test expression);
f. Jump / Loop
Outer loop executing. i = 0
Inner loop executing. j = 0
Inner loop executing. j = 1
Outer loop executing. i = 1
Inner loop executing. j = 0
Inner loop executing. j = 1
Outer loop executing. i = 2
Inner loop executing. j = 0
Inner loop executing. j = 1
Outer loop executing. i = 3
Inner loop executing. j = 0
Jumped to stop. i = 3
g. If/ else
C++ if Statement
The if statement checks whether the test condition is true or not. If the test condition is true,
it executes the code/s inside the body of if statement. But it the test condition is false, it
skips the code/s inside the body of if statement.
Working of if Statement
The if keyword is followed by test condition inside parenthesis ( ). If the test condition is
true, the codes inside curly bracket is executed but if test condition is false, the codes
inside curly bracket { } is skipped and control of program goes just below the body of if as
shown in figure above.
Flowchart of if
Example 1: C++ if Statement
C++ Program to print integer entered by user only if that number is positive.
#include <iostream>
using namespace std;
int main() {
int number;
cout<< "Enter an integer: ";
cin>> number;
if ( number > 0) { // Checking whether an integer is positive or not.
cout << "You entered a positive integer: "<<number<<endl;
}
cout<<"This statement is always executed because it's outside if statement." ;
return 0;
Output 1
Enter an integer: 5
You entered a positive number: 5
This statement is always executed because it's outside if statement.
Output 2
Enter a number: -5
This statement is always executed because it's outside if statement.
C++ if...else
The if...else executes body of if when the test expression is true and executes the body
of else if test expression is false.
Working of if...else Statement
The if statement checks whether the test expression is true or not. If the test condition is
true, it executes the code/s inside the body of if statement. But it the test condition is false,
it executes the code/s inside the body of else .
Flowchart of if...else
Example 2: C++ if...else Statement
C++ Program to check whether integer entered by user is positive or negative
(Considering 0 as positive)
#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;
}