# Do loop with If condition

I have the following formulas:

1- A cross section:

sigma[beta_,alpha_,Y4_] := c^2/(256*Pi) * (1/2) ( Ytt[beta,alpha,Y4]/ m ) * Abs[A]^2


Where c is a strong coupling constant, m is a mass, A is a loop function and Ytt[beta,alpha,Y4] is a coupling .

2- A final cross sectionfsigma, which equals sigma times a branching ratio BR, that:

fsigma[beta_,alpha_,Y4_,xx_,ms_] := sigma[beta,alpha,Y4] * BR[beta,alpha,Y4,xx,ms]


I 'd like to generate a data file (.dat file), for the values of fsigma runs over the five free parameters, with each parameter has a region, as:

{beta,ArcTan[0.9],ArcTan[2.],0.1}, {alpha,ArcTan[0.1],ArcTan[0.3],0.1}, {Y4,-1,-5,1},{xx,-7,7,1},{ms,350,400,50}

Also can I before making this loop, to set a pre- defined constrain on Ytt[beta,alpha,Y4] => 0.8 < Ytt[beta,alpha,Y4] < 1.2. i.e., to select from the parameters domains only the points which satisfy this constrain.

I tried to use Do loop with If condition, but it doesn’t work for me

You can generate a table from a loop using Reap and Sow

results =
Reap[
Do[
If[0.8 < Ytt[beta, alpha, Y4] < 1.2,
Sow[{beta, alpha, Y4, xx, ms, fsigma[beta, alpha, Y4, xx, ms]}]
],
{beta, ArcTan[0.9], ArcTan[2.], 0.1},
{alpha, ArcTan[0.1], ArcTan[0.3], 0.1},
{Y4, -1, -5, -1},
{xx, -7, 7, 1},
{ms, 350, 400, 50}
]
][[2, 1]];~Monitor~{beta, alpha, Y4, xx, ms};
Export["results.dat", results, "Table"]

• @S.S. Sorry, see edit. Try Export["results.dat", results, "Table"] Commented May 20, 2016 at 15:24
• I found results.dat has been made , but only with one word "Table".
– S.S.
Commented May 20, 2016 at 15:28
• Thanx, it's working now.
– S.S.
Commented May 20, 2016 at 15:30
• @ JasonB. Can I set extra constrain on fsigma[beta, alpha, Y4, xx, ms] to list only the values where fsigma[beta, alpha, Y4, xx, ms] > 0 ?
– S.S.
Commented May 20, 2016 at 16:21
• @S.S. Easy, just use (0.8 < Ytt[beta, alpha, Y4] < 1.2)&&fsigma[beta, alpha, Y4, xx, ms] >0 Commented May 20, 2016 at 18:11

You're probably better off using something like Table than an explicit For or Do loop (I believe this is often the case in MMA). e.g:

Table[{
beta, alpha, Y4, xx, ms,
If[ 0.8 <= Ytt[beta, alpha, Y4] < 1.2, fsigma[beta, alpha, Y4, xx, ms] , 0]},
{beta, ArcTan[0.9], ArcTan[2.], 0.1},
{alpha, ArcTan[0.1], ArcTan[0.3], 0.1},
{Y4, -1, -5, -1},
{xx, -7, 7, 1},
{ms, 350, 400, 50}
]

• I think it's better to use a loop, for my expressions
– S.S.
Commented May 20, 2016 at 8:48

Expanding upon the answers so far you may have a nice format for output on screen (e.g. Dataset) and get your file as you want it.

## Creating a Dataset

data = Table[
(* List elements *)
If[ 0.8 <= Ytt[ beta, alpha, Y4 ] < 1.2,
(* then *) Association[
"beta" -> beta,
"alpha" -> alpha,
"Y4" -> Y4,
"xx" -> xx,
"ms" -> ms,
"fsigma" -> fsigma[ beta, alpha, Y4, xx, ms ]
],
(* else *) Nothing (* which is automatically removed *)
],
(* iterator specification *)
{beta, ArcTan[0.9], ArcTan[2.], 0.1},
{alpha, ArcTan[0.1], ArcTan[0.3], 0.1},
{Y4, -1, -5, -1},
{xx, -7, 7, 1},
{ms, 350, 400, 50}
] // RightComposition[
Flatten[ #, 2]&, (* bring the Table-results to just 2 levels and have Nothing removed *)
Dataset
]


## Exporting the Values to a File

Using Normal and Values you can use Export to export the revant values to a .dat file as demonstrated by JasonB.

(* SetDirectory[ NotebookDirectory[] ] or something else if needed *)
Export[ "data.dat", data[[ All, Values ]]//Normal, "Table" ]

• If you would like to view the dataset but find it is too large, this will help.
– gwr
Commented May 20, 2016 at 11:07