Changing Digital Pin from Read to Write in Routine

How does one change a Digital Pin from Read to Write in the program loop. Is this simply a matter of redefining the pin. Can these states be flexible due to incoming parameters and data?
For example...would it be possible to write a Digital Pin state conditional to a sensor reading....if sensorA is high then digitalpin2 Read sensorB....if sensorA is low then digitalpin2 Write High.
i know the syntax is quite wrong here...but i`m curious to know if it is a common thing that can be done....the same pin changing state mid routine based on other variables. Can someone steer me in the right direction as to the "how" it can be done if possible? thanks, arduiYES

The mode of a pin (read/write) can be changed using pinMode(), in loop(). The pinMode command is not limited to use in setup().

The mode of the pin is changed frequently for the ultrasonic sensors. Set the mode to output to generate the pulse, then set it back to input to read the echoed pulse.

that sure is super news PaulS. much thanks