The difference between scripting Shell and Python under Linux

Source: Internet
Author: User
Ideas and algorithms can be the same, but the syntax is different AH. For example 1+2+3+4+ ..., and over 10000
Shell,c,python to achieve similar ideas.
1, the shell implementation while loop
#!/bin/sh
Sum=0
I=0
While [$sum-LT 10000];
Todo
i=$ (($i + 1)); #or ((i++));
sum=$ (($sum + $i)); #or ((sum=sum+i));
Done
Echo $i, $sum
2, c implementation while loop
#include "stdio.h"
int main ()
{
int sum=0;
int i=0;
while (sum<10000)
{
i++;
Sum=sum+i;
}
printf ("%d,%d\n", i,sum);
return 0;
}
3, shell implementation for loop
#!/bin/sh
Sum=0
for ((I=1;; i++));
Todo
sum=$ (($sum + $i)) #or ((sum=sum+i))
If [$sum-gt 10000];then
Break
Fi
Done
Echo $i, $sum
4, c implementation for loop
#include "stdio.h"
int main ()
{
int sum=0;
int i;
for (I=1;; i++)
{
Sum=sum+i;
if (sum>10000) {
Break
}
}
printf ("%d\n", sum);
return 0;
}
5, Do/while implementation
#include "stdio.h"
int main ()
{
int sum=0;
int i=0;
Todo
{
i++;
Sum=sum+i;
}while (sum<10000);
printf ("%d,%d\n", i,sum);
return 0;
}
6, Python implementation while loop
#!/usr/bin/python
Sum=0
I=0
While sum < 10000:
i=i+1;
Sum=sum+i;
Print i,sum;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.