# Using a condition in a do loop

If I have the following code which was written by @creidhne here Problem using TableForm and Do loops:

numberofrows = 40;
deltat = 0.00000001;
Tref = {353.15, 333.15};
nref = {0.830144995, 0.654953157};
kref = {1.541030575, 0.016538198};
Earef = {106310.1492, 261971.1364};
initialxt = 1*^-12;(*shortcut for 1*10^-12*)
q = {0.1, 0.3, 1, 3, 10, 30, 100, 300, 600, 1000};
rowNames = {"Delta t (s)", "q (K/s)"};
colNames = {"Time(s)", "T[C]", "K(T)=k^(1/n)", "dx/dT", "x(t)",
"DH,aged-DH,unaged (J/g)", "Check dx"};
Clear[tbl];(*because numberofrows or Length[q] can change*)
Do[
Clear[a, b, c, d, e, f, g];
b = deltat;
b = q[[j]];
tbl[j] = Join[{{
a = 0,
b = 90.01,
c = (kref[]*Exp[(-Earef[]/8.314)*
((1/(90.01 + 273.15)) - (1/Tref[]))])^(1/nref[]),
d = (c*nref[]*(1 - initialxt)*
(-Log[1 - initialxt])^((nref[] - 1)/nref[]))/q[[j]],
e = (b - b)*d + initialxt,
f = e*90.01,
g = e}},
Table[{
a[i] = a[i - 1] + b,
b[i] = ((b[i - 1] + 273.15) - b*a[i]) - 273.15,
c[i] = (kref[]*Exp[(-Earef[]/8.314)*
((1/(b[i] + 273.15)) - (1/Tref[]))])^(1/nref[]),
d[i] = (c[i + 1]*nref[]*(1 - e[i - 1])*
(-Log[1 - e[i - 1]])^((nref[] - 1)/nref[]))/q[[j]],
e[i] = (b[i] - b[i + 1])*d[i] + e[i - 1],
f[i] = e[i]*90.01,
g[i] = e[i] - e[i - 1]},
{i, 7, numberofrows - 9 + 1}]
],(*end Join*)
{j, Length[q]}];
(*remove last row from all tables*)
(tbl[#] = tbl[#][[;; -2]]) & /@ Range[Length[q]];
(*combine all tables*)
ttall = Flatten[Join[tbl /@ Range[Length[q]]], 1];
Print[Column[{
TableForm[{deltat, q[[#]]}, TableHeadings -> {rowNames, None}],
" ",
TableForm[tbl[#], TableHeadings -> {None, colNames}],
" "
}]] & /@ Range[Length[q]];


How can I introduce a condition to this code that depends on the value of b[i] (e.g. temperature) such as if the value of b[i] is greater than 74, I need to run the code as it is but if the value of b[i] is lower than 74 but greater than 40, then kref[],nref[] and Earef[] need to be change to kref[],nref[] and Earef[].

I tried using something like:

Which[b[i]]>74,Join[{{ a = 0, b = 90.01...etc] but it does not work.

EDIT: The solution developed by youyou works great using Sequence@@. I wanted to updated to ask what If I want to introduce an extra condition such as when b[i] is the closest or nearest to 74, then d[i] = (c[i + 1]* nref[]*(1 -initialxt)*(-Log[1 - initialxt])^((nref[] - 1)/nref[]))/q[[j]] and the rest stays the same, how could I do that?. Notice that depending of numberofrows (say 4000) b[i] goes through 74 and below, and I am interested in using the condition for the closest value to 74.

Thank you very much in advanced

I think this should work for tb1[j]:

tbl[j] = Join[{{
a = 0,
b = 90.01,
c = (kref[]*Exp[(-Earef[]/8.314)*
((1/(90.01 + 273.15)) - (1/Tref[]))])^(1/nref[]),
d = (c*nref[]*(1 - initialxt)*
(-Log[1 - initialxt])^((nref[] - 1)/nref[]))/q[[j]],
e = (b - b)*d + initialxt,
f = e*90.01,
g = e}},
Table[{
a[i] = a[i - 1] + b,
b[i] = ((b[i - 1] + 273.15) - b*a[i]) - 273.15,
Sequence@@If[40 < b[i] < 74,
{c[i] = (kref[]*Exp[(-Earef[]/8.314)*
((1/(b[i] + 273.15)) - (1/Tref[]))])^(1/nref[]),
d[i] = (c[i + 1]*nref[]*(1 - e[i - 1])*
(-Log[1 - e[i - 1]])^((nref[] - 1)/nref[]))/q[[j]]},
{c[i] = (kref[]*Exp[(-Earef[]/8.314)*
((1/(b[i] + 273.15)) - (1/Tref[]))])^(1/nref[]),
d[i] = (c[i + 1]*nref[]*(1 - e[i - 1])*
(-Log[1 - e[i - 1]])^((nref[] - 1)/nref[]))/q[[j]]
}],
e[i] = (b[i] - b[i + 1])*d[i] + e[i - 1],
f[i] = e[i]*90.01,
g[i] = e[i] - e[i - 1]},
{i, 7, numberofrows - 9 + 1}]
]


I replaced c[i] = ..., d[i] = ...

by

Sequence@@If[40<b[i]<74,
{c[i] = [expression with kref, etc.], d[i] = [expression with kref, etc.]},
{c[i ] = [expression with kref, etc.], d[i] = [expression with kref, etc.]}]

• thank you for your help and code. For some reason when I do this modification the last column g does not longer compute except for the first value. All the calculations seem to get erased. Does this happen to you? and how to modify it so that this does not happen? Thanks again
– John
May 7, 2020 at 17:40
• I think what is happening is that in the way the code is contructed some of the values of the d column are going towards the c column making the c column have two values and that's the reason that at the end the last column does not have any value except the first one.
– John
May 7, 2020 at 18:18
• oups indeed there was a problem. Try to change If[40 < b[i] < 74 into Sequence@@ If[40 < b[i] < 74 in the code, it seems to work for me. May 7, 2020 at 18:30
• youyou You are right Sequence@@ makes it work now! Thank you for your help !
– John
May 7, 2020 at 18:46
• I edited my answer with the Sequence stuff. I will look at your new question tomorrow ^^ May 7, 2020 at 21:09