1 of 9 The AI Equations (Part III) By Ian Beardsley Copyright © 2016 by Ian Beardsley
2 of 9 While Loop In Python count.py n=int(raw_input('Count to this integer: ')) x=0 if n>0: while (x!=n): x=x+1 print(str(x)) else: print('Give me a positive integer.’)
While Loop In C cuenta.c #include <stdio.h> int main(void) { int i=0; int n; printf("Give me an integer less than 10: "); scanf("%i", &n); while (n>0) { i=i+1; n=n-1; printf("%i\n", i); } }â&#x20AC;Š
3 of 9
For Loop In Python For Loops in Python and C cuenta.py n=int(raw_input("Give me a positive int: ")) for number in range(1, n+1): print(str(number))
For Loop In C count.c #include<stdio.h> int main (void) { int n; do { printf("Count to this integer: "); scanf("%d", &n); } while (n<=0); for (int i = 1; i<=n; i++) { printf("%d\n", i); } }
Running the For Loop in C (Does same thing as the While Loops) jharvard@appliance (~): cd Dropbox/descubrir jharvard@appliance (~/Dropbox/descubrir): ./count Count to this integer: 5 1 2 3 4 5 jharvard@appliance (~/Dropbox/descubrir): â&#x20AC;Š
4 of 9 print("Teaching the computer what multiplication and division are: "); print(" "); print("1. Multiplication is an adding process"); print("2. Division is a subtracting process"); print(" "); a=int(raw_input("Give me an integer a: ")); b=int(raw_input("Give me an integer b: ")); product=0; n=b; while (n!=0): product=product+a; n=n-1; else: print("The product is: " + str(product)); big=int(raw_input("Give me a big number that divides by a smaller number, evenly: ")); small=int(raw_input("Give me the smaller number: ")); result=0; while (big!=0): result=result+1; big=big-small; else: print(" "); print("The result is: " + str(result)); â&#x20AC;Š
â&#x20AC;©
5 of 9
6 of 9 print("Teach the computer the difference between even and odd:" ); int=int(raw_input("Give me an integer: (if remainder of int divided by 2 is 0) ")); if int%2==0: print("even"); else: print("odd"); â&#x20AC;Š
â&#x20AC;©
7 of 9
8 of 9
9 of 9 The Author