for loop iterative statements
Executing set of statements using for iterative statement in C++ and Python is quite different.
First, lets look how C++ implement iterative method using the for method. In C++, for loop is better suites for situations where number of iterations are known.
Syntax
for (initialization : condition : modifier )
{
statement 1;
statement 2;
...
}
The initialization, condition, and modifier do not need to be placed inside the brackets.
Example
for (int a=0 ; a<=10 ; a++)
{
cout<<a<<" x2 = "<<a*2<<endl;
}
or
int a = 0;
for ( ; a<=10 ; )
{
cout<<a<<" x2 = "<<a*2<<endl;
a++;
}
In Python, for loop can be used in various ways as shown in the following examples
Syntax
for variable in sequence:
statement 1
statement 2
...
example 1
for num in range (1,10)
print num
This will print number 1,2,3,4,5,6,7,8,9
The range function use the following syntax : range (start, end, step).
for num in range (2,10,2)
print num
output
2
4
6
8
example 2
for letter in 'Monday':
print 'letter value : ', letter
output
letter value : M
letter value : o
letter value : n
letter value : d
letter value : a
letter value : y
example 3
weekdays = ['Monday','Tuesday','Wednesday','Thursday','Friday']
for name in weekdays:
print 'weekday - ',name
output
weekday - Monday
weekday - Tuesday
weekday - Wednesday
weekday - Thursday
weekday - Friday
example 4
for m in range(2,15):
a = 0
for n in range (1, m/2+1):
if m%n == 0:
a = a+1
if a==1:
print 'prime number - ',m
output
prime number - 2
prime number - 3
prime number - 5
prime number - 7
prime number - 11
prime number - 13
Programming with C++ and Python
Monday, January 1, 2018
Sunday, December 31, 2017
Iterative statements in C++ and Python (Part 1)
while iterative statement
Set of statements can be executed repeatedly until a condition is met.
Set of statements can be executed repeatedly until a condition is met.
| C++ | Python | ||
|---|---|---|---|
| Syntax while (condition) { statement1; statement2; ... } |
Syntax while condition: statement1; statement2; ... | ||
Example int num = 1; while (num<=10) { cout<<num<<"x2="<<num*2; num = num+1; } | Example num = 1 while num<=10: print("%d x2 = %d"%(num,num*2)) num = num + 1 |
Sunday, December 3, 2017
Conditional statements in C++ and Python (part 3)
if else if conditional statement
Multiple conditions can be checked using if else if (in C++) and if elif (Python). In the following example, two numbers are compared and three possible conditions are checked. The else is used to check the last or the default condition if all the other conditions have failed.
Multiple conditions can be checked using if else if (in C++) and if elif (Python). In the following example, two numbers are compared and three possible conditions are checked. The else is used to check the last or the default condition if all the other conditions have failed.
| C++ | Python | ||
|---|---|---|---|
| Syntax if (condition1) { statement1; statement2; ... } else if (condition2) { statement1; statement2; ... } else { statement1; statement2; ... } |
Syntax if condition1: statement1; statement2; ... elif condition2: statement1; statement2; ... else: statement1; statement2; ... |
||
Example int a = 10; int b = 15; if (a<b) { cout<<"a < b."; } else if (a>b) { cout<<"a > b."; } else { cout<<"a = b."; } | Example a,b = 10, 15; if a<b: print("a < b") elif a>b: print("a > b") else: print("a = b") |
Saturday, December 2, 2017
Conditional statements in C++ and Python (part 2)
if else conditional statement
In the if else conditional statement, one set of statement(s) is executed if the condition is true, otherwise, the other set of statement(s) is executed.
In the if else conditional statement, one set of statement(s) is executed if the condition is true, otherwise, the other set of statement(s) is executed.
| C++ | Python | ||
|---|---|---|---|
| Syntax if (condition) { statement1; statement2; ... } else { statement1; statement2; ... } |
Syntax if condition: statement1; statement2; ... else: statement1; statement2; ... |
||
Example int a = 10; int b = 15; if (a<b) { cout<<"a < b."; } else { cout<<"a >= b."; } | Example a,b = 10, 15; if a<b: print("a < b") else: print("a >= b") |
Conditional Statements in C++ and Python (part 1)
if conditional statement
A condition is checked and execute statement/block of statements if the condition is true.
A condition is checked and execute statement/block of statements if the condition is true.
| C++ | Python | ||
|---|---|---|---|
| Syntax if (condition) { statement1; statement2; ... } |
Syntax if condition: statement1; statement2; ... |
||
Example int a = 10; int b = 15; if (a<b) { cout<<"a < b."; } | Example a,b = 10, 15; if a<b: print("a < b") |
Thursday, November 30, 2017
Indentation in C++ and Python
C++
Indentation has no effect on the program but make it more organize and reader friendly. The braces or curly brackets makes the statement blocks for the tasks.
if (10 < 20)
cout<<"10 is less than 20";
else
cout<<"10 not greater than 20";
cout<<"This is second line";
Output
10 is less than 20
This is second line
Python
Indentation makes the statement blocks. No need to use the braces.
if 10 < 20:
cout<<"10 is less than 20";
else:
cout<<"10 not greater than 20";
cout<<"This is second line";
Output
10 is less than 20
The output will be different if the indentation of the above python code is changed as follows.
if 10 < 20:
cout<<"10 is less than 20";
else:
cout<<"10 not greater than 20";
cout<<"This is second line";
Output
10 is less than 20
This is second line
Indentation has no effect on the program but make it more organize and reader friendly. The braces or curly brackets makes the statement blocks for the tasks.
if (10 < 20)
cout<<"10 is less than 20";
else
cout<<"10 not greater than 20";
cout<<"This is second line";
Output
10 is less than 20
This is second line
Python
Indentation makes the statement blocks. No need to use the braces.
if 10 < 20:
cout<<"10 is less than 20";
else:
cout<<"10 not greater than 20";
cout<<"This is second line";
Output
10 is less than 20
The output will be different if the indentation of the above python code is changed as follows.
if 10 < 20:
cout<<"10 is less than 20";
else:
cout<<"10 not greater than 20";
cout<<"This is second line";
Output
10 is less than 20
This is second line
Comments in C++ and Python
Comments are put in a program to make it more readable and easy to understand when someone reads it.
C++
There are two commenting styles
1. Line comment
Put two slashes (//) and whatever is been typed will become a comment and will be ignored by the compiler.
E.g.
int total = 0; // integer variable to store the total value
2. Block comment
This consist two parts: Starting with slash and star (/*) and ending with the reverse order , which is star and a slash (*/). Everything typed between these two symbols become comments and ignored by the compiler. Multiple comment lines can be made with the block comment method.
E.g.
/*-----------------------------
Author : Randima
Date : 2017 January 01
-----------------------------*/
Python
A line starts with hash symbol (#) is a comment. Unlike block commenting in the C++, hash symbol (#) should be used in every comment line.
E.g.
# Author : Randima
# Date : 2017 January 01
C++
There are two commenting styles
1. Line comment
Put two slashes (//) and whatever is been typed will become a comment and will be ignored by the compiler.
E.g.
int total = 0; // integer variable to store the total value
2. Block comment
This consist two parts: Starting with slash and star (/*) and ending with the reverse order , which is star and a slash (*/). Everything typed between these two symbols become comments and ignored by the compiler. Multiple comment lines can be made with the block comment method.
E.g.
/*-----------------------------
Author : Randima
Date : 2017 January 01
-----------------------------*/
Python
A line starts with hash symbol (#) is a comment. Unlike block commenting in the C++, hash symbol (#) should be used in every comment line.
E.g.
# Author : Randima
# Date : 2017 January 01
Subscribe to:
Posts (Atom)
Iterative statements in C++ and Python (Part 2)
for loop iterative statements Executing set of statements using for iterative statement in C++ and Python is quite different. First, l...
-
First program Display Hello World in the terminal C++ #include <iostream> using namespace std; int main() { cout...
-
while iterative statement Set of statements can be executed repeatedly until a condition is met. C++ Python Synta...
-
if conditional statement A condition is checked and execute statement/block of statements if the condition is true. C++ ...