CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: For/if

  1. #1
    Join Date
    Feb 2016
    Posts
    29

    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;
                    
            }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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
    Code:
    a) 80
    b) 80
    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)

  4. #4
    Join Date
    Feb 2016
    Posts
    29

    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; 
    	
    }

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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
  •  





Click Here to Expand Forum to Full Width

Featured