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.

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.


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.


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.


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")




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...