Control Structures: if Statement
Control Structures:
A computer can process a program in one of the following ways:1) In sequence
2) Selectively, by making a choice, which is also called a branch
3) Repetitively, by executing a statement over and over, using a structure called a loop
or
4) by calling a functionThe Figure illustrates the first three types of program flow.
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(s). In repetition, the program repeats particular statements a certain number of times based on some
condition(s).
Selection control structure:
Figures (b) and (c) show that the execution of a selection or a repetition statement requires the execution of a logical expression. Therefore, first we need to learn about logical expressions and how to evaluate them.
Logical expression: An expression that evaluates to true or false is called a logical expression.
For example, because “8 is greater than 3” is true, the expression 8 > 3 is a logical
expression. Note that > is an operator in C++, called the “greater than” and is an example of a relational operator.
1) The if statement:
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 the if statement. The syntax of one-way selection is:
if(Expression)
{
body of if 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. The statement following the expression is sometimes called the action statement.
Our next program, t17, provides an example.
// t17.cpp
// demonstrates IF statement
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Enter a number: ";
cin >> x;
if( x > 100 )
cout << "That number is greater than 100\n";
return 0;
}
Here’s an example of the t17 program’s output when the number entered by the user is greater than 100:
Enter a number: 2000
That number is greater than 100
If the number entered is not greater than 100, the program will terminate without printing the second line.
Click here to download source code....
Subscribe our channel now...
Comments
Post a Comment