Trace & If/Else statements

Can someone show me how I trace these statements and show changes that were made to each location. I have an example of one problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

int a, b, c;

a = 5;
b = 10;
c = -15;
if ( ( a + 20 ) > b )
{
	cout<< "line 1\n";
	a = b + c;
	if ( ( a + 25 ) > b )
		cout<< "line 2\n";
}
else 
	cout<< "line 3\n";
I would guess your looking for something like this

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
#include <iostream>
using namespace std;

int main (int argc, char *argv[])
{
int count =0;
int a, b, c;

a = 5;
b = 10;
c = -15;

cout <<"\t"<< count << "\t"<< "A: " << a << "\tB: " << b << "\tC: " << c << endl;
if ( ( a + 20 ) > b )
	{
		cout<< "L1\t";
		cout << count << "\t"<< "A: " << a << "\tB: " << b << "\tC: " << c << endl;
	
		a = b + c;
		if ( ( a + 25 ) > b )
			cout<< "L2\t";
			cout << count << "\t"<< "A: " << a << "\tB: " << b << "\tC: " << c << endl;
	}
else 
	{
		cout<< "L3\t";
		cout << count << "\t"<< "A: " << a << "\tB: " << b << "\tC: " << c << endl;
	}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int a, b, c;

a = 5;
b = 10;
c = -15;

if ( ( a + 20 ) > b ) //true so line 1 is printed.
{
	cout<< "line 1\n";
	a = b + c;  //changing the value of a to -5
	if ( ( a + 25 ) > b ) //true so line 2 is printed
		cout<< "line 2\n";
}
else  // else part is not executed as if part is already being executed.
	cout<< "line 3\n";


OR


1
2
3
4
5
6
7
8
9
10
11
12
13
int a, b, c;
a = 5;
b = 10;
c = -15;

if(( a + 20 ) > b )
{  cout<< "line 1\n";
   a = b + c; 
   if( ( a + 25 ) > b ) 
  {cout<< "line 2\n";}
}
else 
   cout<< "line 3\n";

Large if else are main.
small if condition is under main if.
So suppose the main if was not executed(if a+20 <=20) then the only output had been
line 3


Simply you can obtain following outputs:
line 1
line 1
line 2
line 3


but you can never obtain this
line 2
Thank you very much guys I appreciate the help, I really needed some info on this
cheers:)
Topic archived. No new replies allowed.