Math03
Δομές - ΑΠΟΦΑΣΕΙΣ & ΕΠΑΝΑΛΗΨΕΙΣ

Clear["Global`*"]

Λογικές αποφάσεις - Logical decisions

Logical operators

x>3   (* >=, < , <=, ==,  != *)
x≠5  
x==5
!(x>3)
x>3||x<-3
x>3&&x<-3
Xor[x>2, x>6]

x=5  (* αναθεση τιμής *)
y=x==5  
z=x>10

Decisions

a=100
If[a>0, b=1, b=-1]

If[a>0, b=100; Print["Positive number"],  b=-100; Print["Negative number"]];
b

(* maximum of two numbers *)  
a1=4; a2=3; 
If[a1>a2, max=a1, max=a2]; 
Print["max(",a1,",",a2,")=",max]

Επαναλήψεις - Loops

Βρόγχοι επανάληψης

Do

x=100
Do[x=x/2, {5}]     (* μετρητής *)
x

x0=100;
Do[x=x0/2;y=Random[];Print[x," ", y," ", x*y];x0=x, {5}]

Μετρητές (Counters)

Do[x=Sqrt[i];  Print[i,"    x = ",x], {5}]

Do[ x=Sqrt[i];  Print[i,"    x = ",x], {i,5}]

Do[ x=Sqrt[i];  Print[i,"    x = ",x], {i,3,5}]

Do[ x=Sqrt[i];  Print[i,"    x = ",x],{i,0,5,0.5}]

For

x0=1;
For[k=1,k<=5,k=k+1, x=x0/2; Print[k,"  x = ",x];x0=x ]

While

x=100
Print["Αρχική τιμή του x=",x]
While[x>2, x=x/2; Print[N[x]]]