add

Saturday, 4 February 2017

C++ Control Structure & If Structure Explanation

Control Structure-1

A computer can process a program in one of the following ways: in sequence; selectively, by making a choice, which is also called a branch; repetitively, by executing a statement over and over, using a structure called a loop; or by calling a function. Control structures provide alternatives to sequential program execution and are used to alter the sequential flow of execution. The two most common control structures are selection and repetition. In selection, the program executes particular statements depending on some condition in repetition, the program repeats particular statements a certain number of times based on some condition
Although there are only two logical values, true and false, they turn out to be extremely
Useful because they permit programs to incorporate decision making that alters the processing flow. The remainder of this chapter discusses ways to incorporate decisions into a program. In C++, there are two selections, or branch control structures: if statements and the switch structure. This section discusses how if and if. . .else statements can be used to create one-way selection, two-way selection, and multiple selections.

 If Structure:   (One-Way Selection):

A bank would like to send a notice to a customer if her or his checking account balance falls below the required minimum balance. That is, if the account balance is below the required minimum balance, it should send a notice to the customer; otherwise, it should do nothing. Similarly, if the policyholder of an insurance policy is a nonsmoker, the company would like to apply a 10% discount to the policy premium. Both of these examples involve one-way selection. In C++, one-way selections are incorporated using if statement. The syntax of one-way selection is: if (expression) statement
Note the elements of this syntax. It begins with the reserved word if, followed by an expression contained within parentheses, followed by a statement. Note that the parentheses around the expression are part of the syntax. The expression is sometimes called a decision maker because it decides whether to execute the statement that follows it. The expression is usually a logical expression. If the value of the expression is true, the statement executes. If the value is false, the statement does not execute and the computer goes on to the next statement in the program. The statement following the expression is sometimes called the action statement.


Basic Structure of If:


If ( conditions);
Statements
Or
If ( conditions)
{
Statements;
}
The below example explain the basic Structure & implementation of If Structure
//Program:  Absolute value of an integer

#include <iostream>
Using namespace std;
Int main ()
{
Int number, temp;
Cout       << "Line 1: Enter an integer: ";                       //Line 1
cin                >> number;                                                  //Line 2
cout                 << endl;                                                     //Line 3
           temp = number;                                                    //Line 4
if (number < 0)                                                                //Line 5
number = -number;                                                       //Line 6
cout               << "Line 7:  The absolute value of "       
<< temp << " is " << number << endl;                      
return 0;
}

Relational Operators:

To make decisions, you must be able to express conditions and make comparisons. For example, the interest rate and service charges on a checking account might depend on the balance at the end of the month. If the balance is less than some minimum balance, not only is the interest rate lower, but there is also usually a service charge. Therefore, to determine the interest rate, you must be able to state the minimum balance and compare the account balance with the minimum balance (a condition). The premium on an insurance policy is also determined by stating conditions and making comparisons. For example, to determine an insurance premium, you must be able to check the smoking status of the policyholder. Nonsmokers (the condition) receive lower premiums than smokers. Both of these examples involve comparing items. Certain items are compared for equality against a particular condition; others are compared for inequality (greater than or less than) against a particular condition.
. Relational operators C++, as shown in table

Operator
Description
<=
Less than or equal
>=
Greater than or equal
==
Equal to equal to
!=
Not equal to
< 
Less than
> 
Greater than


Logical (Boolean) Operators and Logical Expressions:

This section describes how to form and evaluate logical expressions that are combinations of other logical expressions. Logical (Boolean) operators enable you to combine logical expressions. C++ has three logical (Boolean) operators, as shown in
Table
Operator
Description
&&
Less than or equal
||
Greater than or equal
!
Equal to equal to


0 comments:

Post a Comment