I want to know how a portfast will cause loop. I tried connecting a switch with portfast . As soon as another switch connected it enter to normal switch and has the block port.
Portfast is applied to final stations ports because you suppose that only one host will be connected. In this case, the host will go to Forwading state inmediately after getting link.
If porfast is not applied, final station has to transition from all STP states (blocking, listening, learning, etc. Depends on STP version.). The consequence is that the host spend about 45 seconds to have connectivity.
Is porfast causing the loop? NO but it amplifies consequences. The L2 loop will exist in both cases but with porfast enabled you will have immediately consequences.
You should only use Portfast on either A)A port that will only have a single device connected to it, or B) if you know EXACTLY what you are doing, and have planned accordingly. Otherwise, loops can happen, and potentially knock down your network. In this case, Portfast just makes the port go instantly to forwarding, but it still sees the BPDUs coming from your other switch, which makes the port go into 'blocking' state.
I'd recommend that you remove the portfast command on the port facing another switch, and configure those ports (on Switch A and B) as trunk ports, then run Rapid Spanning Tree.
Portfast just makes the port basically bypass the blocking listening and learning phases of stp and goes straight to forwarding.
Always use "bpdu guard" in conjunction with porfast on your access interfaces, this will protect your network from loops, if a rogue switch is connected, by immediately putting the interface into error disabled state.