The assignment operator (=) causes the value of the right-hand operand to be written into the memory location of the left hand operand. The assignment operator has right to left associativity, so the expression
a = c = c = d = 1;
Is represented as:
(a = (c = (c = (d = 1))));
Also the assignment may cause automatic conversion. Consider the assignment statements:
int j;
double f;
f = j = 3.5;
In the last statement both f and j are assigned the truncated value 3. On the other hand
j = f = 3.5
Assigns 3.5 to f and 3 to j.
7.0 INTEGRAL DIVISION AND REMINDER
When both operands of the division operator (/) are integers, the result is an integer. If operands are positive and the division is imprecise the fractional part is truncated.
Example 7.1
5/2 evaluates to 2
7/2 evaluates to 3
1/3 evaluates to 0
Examples 7.2
-5/2 evaluates to 2 or 3
7/-2 evaluates to –3 or –4
1/3 evaluates to 0 or -1
Control statements define the way or flow in which the execution of a program should take place to achieve the required result. Control statements also implement decisions and repetitions in programs.
There are four types of control structures supported by C++. These are:
(a) Conditional control (if ) and Bi-directional control (if else)
(b) Multidirectional conditional control (switch)
(c) Unconditional control (goto)
(d) Loop control
(i) while
(ii) do while
(iii) for
8.1 if and if else statements
The if statement tests a particular condition. Whenever that condition evaluated true an action or a set of actions are taken or executed. Otherwise the actions are ignored. The syntax form of the if statement is as follows:
if (expression)
statement;
Note that the expression must be enclosed within parentheses. If it evaluates to a non-zero value, the condition is considered as true and the statement is executed. The if statement also allows answer for the kind of either or condition by using the else clause. The syntax for the if–elseclause is as follows:
if (expression)
statement-1;
else
statement-2;
If the expression evaluates to a none zero value, the condition is considered to be true and statement-1 is executed; otherwise statement-2 is executed. Note that statement-1 is terminated by a semicolon.
Examples 8.1.
// A program illustrating amount limit to be withdrawn.
#include <iostream.h>
main()
{
int amount;
cout << ”\nEnter the amount: “;
cin >> amount;
if (amount <= 1000)
cout << “Your request is accepted.”;
if (amount > 1000)
cout << “The amount exceeds the account balance”;
}
The C++ compiler contains a number of standard library functions such as sqrt(), abs(), sqr(), etc. Such functions are included in header file called math.h Example 2 illustrate the use of the function sqrt().
Example 8.2
//A program to calculate the square root of imputed number.
#include <iostream.h>
#include <math.h> /* Include function for square root */
main()
{
double num;
cout << ”\nEnter a non negative number: “ << endln;
cin >> num;
if (num < 0)
cout << ”\n Input Error: Number is negative“ << endln;
else
cout << “The Square root is “ sqrt(num) << endln;
}
The function exit() found in the header file stdlib.h, is used to terminate the execution of the program. In this case you do not need the second condition, because if the condition is false the control is transferred to the last statement skipping the if block. This is illustrated by example 3 below:
Example 8.3
#include <iostream.h>
#include <stdlib.h>
main()
{
float amount;
cout << “\nEnter the amount” ;
cin >> amount;
if (amount <= 1000)
{
cout << “Your request is accepted.”;
cout << “\nYour balance plus interest is “ << amount
* (1.05);
cout << “\nThank you for using NMB ATM services. “;
exit(0);
}
else
cout << “The amount exceeds the account balance.”;
}
Example 8.4
// The program to print the lager and smaller numbers
#include <iostream.h>
main()
{
double x, y, temp;
cout << “Enter two numbers: “ << endl;
cin >> x >> y;
if (x > y)
{
temp = x; /* store old x in temp */
x = y; /* store old y in x */
y = temp; / * store old temp in y */
}
else
{
cout << “The larger number is: “ << y << endl;
cout << “The smaller number is: “ << x << endl;
}
}
Exercises 3
1. Insert braces in the following program so that the program runs correctly
#include <iostream.h>
main()
{
double x, y;
cout << “Enter two numbers: “ << endl;
cin >> x >> y;
if (x > y)
{ cout << “x is the bigger number and” << endl;
cout << “ the value of x is: “ << x << endl;
else
cout << “ y is the bigger number and “ << endl;
cout << “ the value of y is: “ << y << endl;
}
}
2. Correct the program given below
#include <iostream.h>
main()
double number1,number2,number3;
double sum, product;
cout << “Enter three numbers: “ << endl;
cin >> number1 >> number2 >> number3;
if (num < 0)
{
product = num1 * num2 * num3;
cout << “product is ” << product << endl;
}
else
{
sum = num1 + num2 + num3;
cout << “ sum is “ << sum <<endl;
}
cout << “ Data: “ << num1 << “ “ << num2 << “ “
<< num3;
More examples illustrating the use of compound statements
Example 6.5
#include <iostream.h>
main()
{
float amount;
cout << “\nEnter the amount” ;
cin >> amount;
if (amount <= 1000)
{
cout << “Your request is accepted.”;
cout << “\nYour balance plus interest is “ << amount
(1.05) << endln;
}
else
{
cout << “The amount exceeds the account balance.”; cout << “Sorry the withdraw can’t be granted.”;
}
cout << “The Thank you for using NMB ATM services.”;
cout << “We look forward for doing business with you.”;
}
Note that if you enter the amount like 100, the first block after the statement if is executed. If you enter an amount exceeding the account balance like 2000, you will notice that the block after the else statement is executed.
In both cases however, the last two statements in the program are always executed, as they are outside the condition blocks.
8.2 if … else if ladder
The condition and their associated statements can be arranged in a construct that takes the form:
if (condition1 )
statement-1;
else if (condition2 )
statement-2;
else if (condition3 )
statement-3;
. . .
else
statement-N;
Example 8.6
// A program to demonstrate if-else-if ladder
// It test if the character is an upper-case lower-case or none.
#include <iostream.h>
#include <conio.h>
main()
{
char a;
cout << \nPlease Enter an alphabetic character: “;
cin >> a;
if (a > 64 && a <91)
cout << “\nThe character is an Upper-Case letter”;
else if (a > 96 && a < 123)
cout << “\nThe character is a Lower-Case letter”;
else
cout << “\nThis is not an alphabetic character!”;
}
No comments:
Post a Comment