Swift: While statement

This entry is part 4 of 41 in the series Swift

If you are new to Apple’s swift like me, you might not know how to turn on the console to view the output.  For XCode, you can go to View -> Debug Area -> Activate Area.

Here we show how to use the while statement.

import UIKit


var number = 2

while (number <= 10)
{
    print(number)
    number = number + 1
    
}

swift

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

 

Java: Conditional while loop

This entry is part 8 of 54 in the series Learn Java

The while loop runs through the code block only if the condition is true, and will continue looping for as long as the condition remains true.

The loop below will print out the numbers 0 to 9.

package javaapplication7;

public class JavaApplication7 {

    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            System.out.print(i++);
        }
    }
}

In the example above, i is first set to 0.  As long as i is less than 10, it will print out i.

i is incremented using i++.

while