Control Constructs

Source Code from Notes


Example 1


//Program to calculate absolute value of an integer

#include <iostream.h>

main()
{
        int     number;

        //Enter number
        cout << "Enter a number: ";
        cin >> number;

        //test if negative
        if (number < 0)
                number = -number;

        //output results
        cout << "The absolute value is " << number << "\n";
}


Example 2


//Program to test if number is even or odd

#include <iostream.h>

main()
{
        int     number,remainder;

        cout << "Enter a number: ";
        cin >> number;
        
        remainder = number % 2;

        if (remainder ==0)
                cout << "The number is even\n";
        else
                cout << "The number is odd\n";

}


Example 3


//Program to illustrate the use of nested if statements

#include <iostream.h>

main()
{
        int number;

        cout << "Enter a number between 1 and 99: ";
        cin >> number;

        if (number > 0 && number < 100 ) 
        {
                if (number < 10)
                        cout <<  "One digit number\n";
                else
                        cout << "Two digit number\n";
        }
        else
                cout << "Number not in range\n";

}


Example 4



#include <iostream.h>

main()
{
        int number, n_digits = 0;

        cout << "Enter a number: ";
        cin >> number;

	if (number >= 100)
        	n_digits = 3;
	else if (number >=10)
        	n_digits = 2;
	else if (number >=0)
        	n_digits = 1;
	else
        	cout <<  "Value out of range\n";

	if (n_digits > 0)
		cout << "The number has " << n_digits << " digits\n";
}


Example 5



#include <iostream.h>

main()
{
        char    day;


        cout << "Which day?";
        cin >> day

        switch(day){
                case 's': cout << "Weekend\n";
                          break;
                case 'm': cout << "Week day\n";
                          break;
                case 't': cout << "Week day\n";
                          break;
                case 'w': cout << "Week day\n";
                          break;
                case 'f': cout << "Week day\n";
                          break;
                default:  cout << "Not a day\n";
	}
}


Example 6



#include <iostream.h>

main()
{
        char    day;

        cout << "Which day?";
        cin >> day;

        switch(day){
                case 's': case 'S': cout << "Weekend\n";
                          break;
                case 'm': case 'M': cout << "Week day\n";
                          break;
                case 't': case 'T': cout << "Week day\n";
                          break;
                case 'w': case 'W': cout << "Week day\n";
                          break;
                case 'f': case 'F': cout << "Week day\n";
                          break;

                default:  cout << "Not a day\n";
        }
}


Example 7



#include <iostream.h>

main()
{
        int 	input_value;

        cout << "Which day?";
	cin >> input_value;


        switch (input_value){
                case 1: cout << "The day is Monday\n";
                        break;
                case 2: cout << "The day is Tuesday\n";
                        break;
                case 3: cout << "The day is Wednesday\n";
                        break;
                case 4: cout << "The day is Thursday\n";
                        break;
                case 5: cout << "The day is Friday\n";
                        break;
                case 6: cout << "The day is Saturday\n";
                        break;
                case 7: cout << "The day is Sunday\n";
                        break;
                default : cout << "Invalid Input\n";
                        break;
        }
}


Back to Main Page for Lecture 3