-
April 7th, 2016, 08:38 AM
#1
For/if
Hello,
Tying to understand using a for without brackets...not sure why they are used. This example A executes differently than b. Thanks much
ex a
Code:
for (j=0;j<3;j++)
if (j!=i)
A= A-B;
ex b
Code:
for (j=0;j<3;j++)
{
if (j!=i)
A= A-B;
}
-
April 7th, 2016, 09:29 AM
#2
Re: For/if
Brackets are used to group statements.
There's no syntactic difference in your example, but consider
Code:
if(a == b)
c = d;
e = f;
or
if(a == b)
{
c = d;
e = f;
}
In the first example, e = f is executed unconditionally. In the second, only if the if statement is true.
-
April 7th, 2016, 09:33 AM
#3
Re: For/if
This example A executes differently than b
How? In what way? Have you traced through the code with the debugger? In both cases does i, A and B start with the same values? Post the actual code used where a executes differently to b.
Consider
Code:
#include <iostream>
using namespace std;
int main()
{
int A;
int j;
const int B = 10;
const int i = 2;
A = 100;
for (j = 0; j < 3; j++)
if (j != i)
A = A - B;
cout << "a) " << A << endl;
A = 100;
for (j = 0; j < 3; j++)
{
if (j != i)
A = A - B;
}
cout << "b) " << A << endl;
}
This displays
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 7th, 2016, 09:56 AM
#4
Re: For/if
Sorry for the psudo code...
so when I run this I get the correct answers...if you remove the remarks it doesn't work correctly. Im not sure why when I add the brackets it wont work...
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double z;
double a[3][4] ={1,3,1,10,1,2,-1,6,2,1,2,10};
double b[3];
for (int i=0;i<3-1;i++)
for (int k=i+1;k<3;k++)
{ z=a[k][i] / a[i][i];
for(int j=0;j<=3;j++)
a[k][j] = a[k][j] - z*a[i][j];
}
for (int i=3-1;i>=0;i--)
{ b[i]=a[i][3];
for (int j=0;j<3;j++)
//{
if (j!=i)
//{
b[i]=b[i]-a[i][j] * b[j];
b[i]=b[i]/a[i][i];
//}
//}
}
int k = 1;
for(int i=0;i<3;i++)
{cout<<"A"<<k<<" = "<<b[i]<<endl;
k++;
}
system ("pause");
return 0;
}
-
April 7th, 2016, 10:11 AM
#5
Re: For/if
I've already explained why. The second statement following your if will execute regardless whether the if statement evaluates true or false. Indentation is only for readability. It has no effect on the actual code.
In your first example, you only have one statement following each for or if. In the second, you have two statements after the if, but only one is controlled by the if.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|