While statement in C, python and Matlab programming language | applied electronics engineering

Latest

While statement in C, python and Matlab programming language

By Applied Electronics - Thursday, March 9, 2017 No Comments
While loop are used to perform certain task repeatedly. In view of high level language like C, Python or Matlab, the execution is some lines of statements while the same thing in terms of assembly language it is repeated execution of instructions.

C programming language


An example code of using while statement in C is as follows.

 A while loop in c programming language uses while keyword followed by the condition within parenthesis. The block of codes for repeated execution is written within curly braces.

Matlab Programming language


Here is an example of how to use while statement with Matlab,


In matlab programming language, the while keyword is used like in c programming language. After the while keyword the condition is written which does not require to be written within parenthesis. This is different from C programming language where the condition must written within parenthesis. Next the block of codes or statements to be executed repeatedly according to the condition are written. Again here there is difference between matlab and c while loop. The codes to be repeated must not be contained within curly braces in Matlab as in case of C programming language. Finally there is another difference. In matlab the end keyword is used to end the while loop. This is not required in C.

Python programming language

Finally lets see hotw while loop is written in Python Programming Language. An example of using while statement in python is as follows.

.
Again like in C and Matlab programming language, the keyword while is used. This while keyword is again followed by the condition for the execution of the while loop block, like in C and Matlab. The difference now is that in Python, the colon : is used to specify the beginning of the while block of codes. Then there is also another major difference between python and Matlab and C. In python the indentation is used to specify the block of codes within the while loop. Here you can see that print x and x +=1 are two while loop statements and they are indentated. Normally while using Python IDE the IDE automatically creates the indentation. So in python there is no need for parenthesis for condition and curly braces for block of codes like in C.Also there is no need for end statement like in Matlab programming language.

See also comparison if statement in C, Matlab and Python programming languages and comparison if else statement in C, Matlab and Python programming languages.

No Comment to " While statement in C, python and Matlab programming language "