Control Flow Statements In Python
Control Flow Statements In Python
What are the Control Flow Statements In Python?
Re: Control Flow Statements In Python
Python Language Loops
In general, statements are executed sequentially in Computer programming,
Programming languages provide various control structures that allow for more
complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times.
Python programming language provides two loop structures,
1) while loop
2) for loop to handle loop requirements…
———————————
1) while loop
It repeatedly executes statements while condition is true…
Syntax:
while (condition/expression):
statement(s);
Example 1 – Print 1 to 10 Numbers.
Example 2: Read a Number, and calculate sum, and Zero to quit from the loop
a=1;
sum=0;
print (“Enter a Number to add to the sum: “);
print (“Enter 0 to quit.”);
while a != 0:
print (“Current Sum: “, sum);
a= float(input(“Enter Number: “));
a=float(a);
sum += a;
print(“Total Sum is: “, sum);
———————————-
3) Example 3: Print 1 to 5 Numbers in reverse order…
val=5;
while (val >= 1):
print(val);
val = val-1;
a=1;
while (a<=10):
print(a);
a=a+1;
2) for loop
for loop iterates over the items of any sequence, such as a list or a string.
Syntax:
for iterating_var in sequence:
statement(s);
Example 1:
a= [2, 4, 6, 8, 10, 12, 14];
for num in a:
print(num);
Example 2:
for num in range(5):
print(num);
For More Info Visit :
http://www.gcreddy.com/2017/06/python-loops.html
In general, statements are executed sequentially in Computer programming,
Programming languages provide various control structures that allow for more
complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times.
Python programming language provides two loop structures,
1) while loop
2) for loop to handle loop requirements…
———————————
1) while loop
It repeatedly executes statements while condition is true…
Syntax:
while (condition/expression):
statement(s);
Example 1 – Print 1 to 10 Numbers.
Example 2: Read a Number, and calculate sum, and Zero to quit from the loop
a=1;
sum=0;
print (“Enter a Number to add to the sum: “);
print (“Enter 0 to quit.”);
while a != 0:
print (“Current Sum: “, sum);
a= float(input(“Enter Number: “));
a=float(a);
sum += a;
print(“Total Sum is: “, sum);
———————————-
3) Example 3: Print 1 to 5 Numbers in reverse order…
val=5;
while (val >= 1):
print(val);
val = val-1;
a=1;
while (a<=10):
print(a);
a=a+1;
2) for loop
for loop iterates over the items of any sequence, such as a list or a string.
Syntax:
for iterating_var in sequence:
statement(s);
Example 1:
a= [2, 4, 6, 8, 10, 12, 14];
for num in a:
print(num);
Example 2:
for num in range(5):
print(num);
For More Info Visit :
http://www.gcreddy.com/2017/06/python-loops.html