Hello Python World

This entry is part 2 of 8 in the series Python

First I have to tell you about the Python interpreter.  The Python language has a built in programming environment which makes testing code changes and possibilities very simple.  As long as you have python in your PATH, you can open a command prompt (Windows, Mac, and Linux OS’s all have it), and type python.  The output of running python is below:

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>>

Continuing my harp on the cleanliness of Python, the Hello World example is very simple.

>>> print 'Hello codecrawl.com World'
Hello codecrawl.com World
>>> 

That’s it, the print function in Python 2.7 and lower looks like this and prints output to the console.  There are also many ways to format output and make it more readable if you happen to need to print variables along with static text.

For example:

>>> first_name = 'First'
>>> last_name = 'Last'
>>> 
>>> print 'Hello %s %s.  Welcome to codecrawl.com!' % (first_name, last_name)
Hello First Last.  Welcome to codecrawl.com!
>>>

first_name and last_name are variables which can get their data from the developer or from user input.  Either way, you can use print to display a static message with dynamic data from variables.  There are many different possibilities.

We’ll discuss more about variables in future posts, but this was your gentle introduction into the world of Python.

C++: Combining while loop and if statement

This entry is part 13 of 61 in the series C++

The while loop will check for a condition and will continue to run the loop if the condition is not met.

For certain condition, you might want to perform a specific task.  You can use if statement to check for this condition.

Consider the following example.

#include <iostream>
using namespace std;

void main()
{
	int i = 0;

	cout << "Please enter a value between 0 and 10" << endl;

	cin >> i;

	while ((i != 1) && (i != 10))
	{	

		if (i > 10)
		{
			cout << "You have entered a right value that is larger than 10.";
		}

		cout << "Please enter a value between 0 and 10" << endl;
		cin >> i;

	}

	cout << "You have entered the right value";
}

If 1 or 10 is entered, it will exit the while loop.  If a value larger than 10 is entered, a message will be printed out and it will still continue to run the while loop.

In line 12, we have introduced 2 new operators.  The != which is not equal and the && which is the and operator.

You can refer to C++ operator in this wiki page.

whileif

C++: The while loop

This entry is part 12 of 61 in the series C++

The if statement check for validity of an expression and execute a block of code once.

We use a while statement to run a block of code if an expression is evaluated to true.

Say you are a rock fan and you are collecting rock albums and would like to stop when you have collected 10 albums.

#include <iostream>
#include <string>
using namespace std;

void main()
{

	int album = 0;

	while (album < 10) {
		album++;
		cout << "I have " << album << " albums\n";
	}
	cout << "\n";
	cout << "I have collected 10 rock albums\n";
	cout << endl;

}

Let’s first look at the statement album++.

The ++ operators are put after the album and this is called post-increment.  Since album is an integer, it will add a value of 1 to it.

For example, in the above example, the album starts with 0 but after running album++ in line 11, the first line printed is:

I have 1 albums.

It is worthwhile to figure out why the last album printed out is 10 when the while expression is (album < 10) in line 10.

whilestatement

 

C++: The if-else statement

This entry is part 11 of 61 in the series C++

The if…else statement allows you to specify an action to perform when the condition is true and a different action to perform when the condition is false.

#include <iostream>
#include <string>
using namespace std;

void main()
{
	int grade = 70;

	if (grade >= 60)
		cout << "Passed";
	else
		cout << "Failed";

	cout << endl;
}

In the above code line 9 will evaluate to true and so the string Passed will be printed out.

ifelse

 

 

 

C++: Flow control with if statement

This entry is part 10 of 61 in the series C++

Like most languages, C++ provides an if statement that supports conditional execution.

We can use an if to check for each condition.  You can actually stack up the many if statements to check for the different conditions.

The example below checks for 2 conditions.

#include <iostream>
#include <string>
using namespace std;

void main()
{
	string singer = "Taylor Swift";
	int count = 6;

	if (singer == "Taylor Swift")
	{
		cout << "I am a fan.";
	}

	if (count == 5)
	{
		cout << "This is her fifth album.";
	}

	cout << endl;

}

For an if statement, if an expression evaluates to true, it will proceed to execute other statements inside the curly braces.

The first expression singer==”Taylor Swift” will evaluate to true and so the line inside the curly braces is printed out.

For the second expression count==5, as we have set count to be 6 earlier, this expression will not evaluate to true.  So it will not execute the cout statement inside the loop.

ifstatement

 

Posted in