Dont understand what's happening in the blocks

closed account (yR9wb7Xj)
I'm doing a problem I found on this website for practice to understand Local variables, scopes & duration.

Write a program that asks the user to enter two integers, the second larger than the first. If the user entered a smaller integer for the second integer, use a block and a temporary variable to swap the smaller and larger values. Then print the value of the smaller and larger variables. Add comments to your code indicating where each variable dies.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
int main()
{

	int smaller;
	int larger;

	cout << "Enter two integers." << endl;
	cin >> smaller >> larger;

	if(smaller > larger){

// I do not understand the following below inside the block
// Can someone please explain I want to learn.

		int temp = larger;
		larger=smaller;
		smaller=temp;




	} // temp dies here
	cout << "The smaller integer is" << smaller << endl;
	cout << "The larger integer is " << larger<< endl;






	return 0;
}
closed account (E0p9LyTq)
Your question seems to be all about scope. What parts of you program can use what variables.

smaller and larger are defined so every part of main() can use them. The if block defines temp and is only usable in the block, in this case defined by the curly braces.
Last edited on
When you use that opening curly brace (line 12) you enter a higher level of scope. This means you can access the variables from lower levels of scope and you can create variable that are localised to this level of scope and above. Because you declare temp inside the block of the if, that is its scope level and when that scope level ends (line 24), any variables which are localised to that scope are destroyed. Hence, you would not be able to access temp after line 24. Temp is used because if you didn't, you'd lose the value of either smaller or larger when you assigned them the other's value.
This block of code creates a local integer named temp to:
1. Hold the value of larger, then
2. Assign the value of smaller to larger, then
3. Assign the value of temp (what larger was) to smaller.
closed account (E0p9LyTq)
A quick and dirty example for understanding scope:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

// a globally scoped variable
int aVariable = 1;

void aFunction()
{
   // a variable only usable within the function
   int aVariable = 123;

   std::cout << "aFunction's aVariable: " << aVariable << "\n";
   std::cout << "Global aVariale: " << ::aVariable << "\n\n";
}

int main()
{
   // a variable scoped to all of main
   int aVariable = 12;

   std::cout << "main's aVariable: " << aVariable << "\n";
   std::cout << "Global aVariale: " << ::aVariable << "\n\n";

   aFunction();

   std::cout << "main's aVariable: " << aVariable << "\n";
   std::cout << "Global aVariale: " << ::aVariable << "\n\n";

   {
      // a variable scoped only to this block
      int aVariable = 1234;

      std::cout << "local block's aVariable: " << aVariable << "\n";
      std::cout << "Global aVariale: " << ::aVariable << "\n\n";

   }

   std::cout << "main's aVariable: " << aVariable << "\n";
   std::cout << "Global aVariale: " << ::aVariable << "\n\n";

   return 0;
}


The output would be:
main's aVariable: 12
Global aVariale: 1

aFunction's aVariable: 123
Global aVariale: 1

main's aVariable: 12
Global aVariale: 1

local block's aVariable: 1234
Global aVariale: 1

main's aVariable: 12
Global aVariale: 1
closed account (yR9wb7Xj)
Thank you guys it make sense now!
Topic archived. No new replies allowed.