×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Looping | Find output programs | Set 4

This section contains the C++ find output programs with their explanations on C++ Looping (set 4).
Submitted by Nidhi, on June 06, 2020

Program 1:

#include <iostream>
using namespace std;

int A = 5;

int fun()
{
    return A--;
}

int main()
{
    int A = 5;

    while (fun()) {
        cout << A + ::A << " ";
    }

    return 0;
}

Output:

9 8 7 6 5

Explanation:

In the above program, we defined a function in which we are accessing the value of the global variable A and decreases it using the post-decrement operator.

In the main() function, we also declared a local variable A. Both local and global variables contain the same value that is 5.

Now look to the while loop, here we used the return value of fun() as a condition in the loop, as we know that condition is true till the returned value is non-zero.

Note: fun() can't access the local variable A while in the main() global variable A is accessing using the Scope resolution operator (::).

Interation1: 
The function fun() returns 5, condition is true, and global 'A' become 4. Then
		cout<<A+::A<<" ";
Here the above statement will print 5+4 that is 9. 

Interation2: 
The function fun() returns 4, condition is true, and global 'A' become 3. Then
		cout<<A+::A<<" ";
Here the above statement will print 5+3 that is 8. 

Interation3: 
The function fun() returns 3, condition is true, and global 'A' become 2. Then
		cout<<A+::A<<" ";
Here the above statement will print 5+2 that is 7. 

Interation4: 
The function fun() returns 2, condition is true, and global 'A' become 1. Then
		cout<<A+::A<<" ";
Here the above statement will print 5+1 that is 6. 

Interation5: 
The function fun() returns 1, condition is true, and global 'A' become 0. Then
		cout<<A+::A<<" ";
Here the above statement will print 5+0 that is 5. 

Interation6: 
The function fun() returns 0, and then condition will be false.

Then the final output will be "9 8 7 6 5".

Program 2:

#include <iostream>
using namespace std;

int A = 5;

int& fun()
{
    return A--;
}

int main()
{
    int A = 5;

    while (fun()) {
        cout << A + ::A << " ";
    }
    return 0;
}

Output:

main.cpp:8:13: error: invalid initialization of non-const 
reference of type ‘int&’ from an rvalue of type ‘int’
     return A--;
            ~^~

Explanation:

Here, we defined a function that returning the reference.

In C++, A function that returns reference will be used as an LVALUE, but in the above program, we are using fun() as an RVALUE.

Read: What do 'lvalue' and 'rvalue' mean in C/C++?

Program 3:

#include <iostream>
#define MACRO(VAL) (char*)(&VAL + 1) - (char*)(&VAL)

using namespace std;

int main()
{
    int i = 1;
    int num = 0;

    num = MACRO(num);

    while (i <= num) {
        cout << "India ";
        i++;
    }
    return 0;
}

Output:

India India India India

Explanation:

In the above program, we defined a macro MACRO that will calculate the size in bytes of a specified variable just like sizeof() operator, here we calculate the displacement of variable, subtract the current address by next address, then we got different that denotes the size of the variable.

Here we used a 32-bit system, then MACRO will return 4 because here we passed an integer variable to it.

num = MACRO(num);

Then the value of num will be 4. Then the loop will execute 4 times and print "India " 4 times.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.