Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<stdio.h>  
int main(){  
  int i=1,j=1;//initializing a local variable    

  for(i=1;i<=3;i++){      
    for(j=1;j<=3;j++){    
      printf("%d &d\n",i,j);    
      if(i==2 && j==2){    
        break;//will break loop of j only    
      }    
    }//end of for loop    

  return 0;  
}


What I have tried:

1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
Posted
Updated 30-Dec-17 1:50am
v2

For starters, lets indent your code so it's more readable:
#include<stdio.h>  
int main()
    {  
    int i=1,j=1;//initializing a local variable    
    for(i=1;i<=3;i++)
        {      
        for(j=1;j<=3;j++)
            {    
            printf("%d &d\n",i,j);    
            if(i==2 && j==2)
                {    
                break;//will break loop of j only    
                }    
            }//end of for loop    
        return 0;  
        }
    }
(I added the last close curly bracket for completeness)
Now you can see what happens: When you break out of the inner loop, the next code the processor sees is
C++
return 0;
so it exits the main function and terminates the application.
Probably, you want the return at the end of the function, outside both loops.

You also need to change this:
printf("%d &d\n",i,j);
To this:
printf("%d %d\n",i,j);
 
Share this answer
 
Comments
yaminipra 29-Dec-17 8:03am    
please explain how dat output comes
OriginalGriff 29-Dec-17 8:22am    
Look at the code: I already explained!
You are "break"ing after printing your values. That's the reason it is being printed even when I=2 & j=2.
 
Share this answer
 
Use the debugger and see your code execute step by step.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900