Questions tagged [procedural-programming]

Questions about Mathematica's procedural programming paradigm.

Procedural programming is on of the many programming paradigms supported by the Wolfram Language. Characteristic for a procedural programming style is the use of looping constructs (like Do, For, and While) and conditionals (like If, Which, and Switch).

Questions about rewriting working procedural Mathematica code in a functional style may use both and .

Useful links:

351 questions
8
votes
3 answers

How do I achive the function of Continue using Catch and Throw?

Procedural programming is considered a bad practice in writing Mathematica code. But for education's sake, is there any way I can do Continue using Catch and Throw? According to WRI's documentation, the function of Break and Continue both can be…
btwiuse
  • 567
  • 7
  • 11
5
votes
7 answers

How to use AppendTo in a While loop?

I am trying to code a while loop that uses AppendTo to append the loop variable to an empty list. even = {} i = 1; While[i <= 5, Print[i]; AppendTo[even, i]; i++] This code does not give any output. Can someone please tell what I am doing…
4
votes
1 answer

how can i get the following figure using the for loop

how can i get the following figure using the for loop. n=5 For[i=1,i<5,i++,For[j=1,j
hazal
  • 43
  • 3
4
votes
1 answer

While loop in a function

f[n_] := ( While[n < 10, n = n + 1]; n ) I am trying to learn mathematica on my own. I am trying to write a function that contains a while loop, but I can't get anywhere. When I key in f[3] I don't get 10, instead I get 3. Can anyone please…
Bernoulli
  • 183
  • 5
4
votes
3 answers

Writing my own code to find the power set of a set

(The Goal is to create a program that given an input set, yields an output showing the Power Set of the entered set without using the built-in mathematica function" "When I enter this code into Mathmetica and run it, it ONLY GOES THROUGH THE LOOP…
3
votes
2 answers

What's wrong with While? Is it possible to use double While in a single code input?

I am supposed to get 2 in the output, but it is still -3 instead. ClearAll[t, n]; n = -10; While[True, If[n^2 + n - 6 == 0, Break[]]; n++]; t = n; While[True, If[t^2 + t - 6 == 0, Break[]]; t++]; t Is it possible to abort the 1st root(-3) and get…
kile
  • 1,671
  • 5
  • 10
2
votes
2 answers

For loop error: Tag Times is Protected

i want to use For loop into my analytical physics theory but my For loop is not working.? so please help me .here is my simple (for example code) For[n = 1, n < Ns, n++ Ns=100; dtr = π/180; χv = 7.2*dtr; χ = N[ArcTan[a*Tan[χv]]]; …
M.M Umber
  • 69
  • 7
2
votes
1 answer

How to create a loop in Mathematica?

Yup, im a newbie, you guessed it. But I have been having fun solving Project Euler problems with Mathematica. I am on problem #8: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. The 1000…
SugarFoot
  • 53
  • 5
2
votes
1 answer

Why is this Do-Loop taking so much time?

I have this simple Do Loop that, for some reason, takes a really long time for $h<\frac{1}{15}$. Why is this so? Is there a more efficient way in programming such a recursion? h = 1/20; v1[0] = 1; v2[0] = 0; Do[v1[j + h] = h v2[j] + v1[j]; v2[j +…
korni1990
  • 307
  • 1
  • 8
2
votes
2 answers

Simple Goto problem

In[1] Goto["place2"]; Label["place1"]; Return[10]; Label["place2"]; Return[20]; Out[1] Return[20] In[2] f[x_] := (Goto[x]; Label["place1"]; Return[10]; Label["place2"]; Return[20];) In[3] f["place2"] Out[3] 20 Q1) Can you tell me why Out[1] is…
imida k
  • 4,285
  • 9
  • 17
2
votes
3 answers

Changing iteration variable in Do loop

I want to generate random positive hermitian matrices, I start with such a code rho11R = Table[RandomReal[], {i, 1, 4}]; rho10R = Table[RandomComplex[], {i, 1, 4}]; Do[If[rho11R[[i]] - rho11R[[i]]^2 - rho10R[[i]]*Conjugate[rho10R[[i]]] >= 0, …
Agnieszka
  • 677
  • 4
  • 13
2
votes
3 answers

Evaluating number of iteration with a certain map with While

Beeing used to programming in C-like languages I am struggling with iterations and loops with mathematica. I am trying not to use the For command, as already lots of people recommended. I am trying to solve the following problem: Given the map…
2
votes
1 answer

Run a do loop from a high iterator value to lower

I wanna have a do loop that runs from the a high number to a smaller one. That is, if we have Do[Print[n],{n,1,4}] I would get 1,2,3,4 in the output. I rather wanna have 4,3,2,1. That is, morally I want to implement Do[Print[n],{n,4,1}] which…
PhoenixPerson
  • 553
  • 2
  • 10
2
votes
2 answers

Looping over two variables

Suppose I have $t=4, 2, 5$ and $v=5, 7, 4, 8, 0, 5$ and $equ=A v/t$. I would like to calculate $equ$ at a single value of $t$ by using all $v$ values and then move to next value of $t$. How can I make a loop for this?
Rabia
  • 33
  • 4
2
votes
1 answer

Confused why my program won't define this

I legitimately think the problem is that pwgenerator isn't getting defined, but im' not sure why. Any ideas? On mine, when i run this to define it, pwgenerator stays blue for some reason, and it didn't before. pwgen[length_Integer:5,…
Shinaolord
  • 445
  • 2
  • 9
1
2 3 4