Your example is a little odd because A and B are the same so I changed the initial example a little bit. This code is how I would do it in Base R. R is cool because it's vectorized and you often don't have to use loops.
df<- data.frame("A" = c("a","f","f","a","a"),"B" = c("a","f","b","b","c"))
df$G <- ifelse(df$A=="f", "f", df$B) #test, yes, no
df
#> A B G
#> 1 a a a
#> 2 f f f
#> 3 f b f
#> 4 a b b
#> 5 a c c
Created on 2021-12-17 by the reprex package (v2.0.1)