Algorithms and Data Structures : Control statement - Control statements can made a program more flexible. With control statements we can define the program line to be executed if a condition is met, and is not executed if the condition is not met. There are two control statement in C++. First, we can use if...else statement and the second is to use switch case.
If ... Else
The general form of the if statement isif (condition)
{
statement1;
statement2;
....
statementN;
}
atau :
if (condition)
{
statement1;
statement2;
....
statementN;
}
else if (condition)
{
statement1;
statement2;
....
statementN;
}
........
else
{
statement1;
statement2;
....
statementN;
}
The level of if...else statement can be define as needed. The condition that must be met can be formed from logical operator and relational operator. Logical operators that can be used, among other
Symbol | Meaning |
---|---|
&& | AND |
|| | OR |
! | NOT |
While the relational operator, among other
Symbol | Meaning |
---|---|
== | equal to |
>= | greater or equal to |
> | more than |
<= | less than or equal to |
< | less than |
!= | not equal to |
Sample Program
#include <iostream>
using namespace std;
int main()
{
int b;
float amount = 0;
cout << "enter the value of b = ";
cin >> b;
if (b > 10);
amount = amount + b;
else
amount = amount - b;
cout << "amount = " << amount <<endl;
return 0;
}
Sample Source Code using relational operators
In the above example if the variables are weighted more than 10 then the statement amount = amount + b will be executed. However, if the entered value equal to 10 or less than 10, then the else statement is executed. Because only one line statement after if then braces can be removed.
The compiler will check if condition. If true, then it will execute amount = amount + b, and skip the else statement. However, if false, then the else statement will be checked.
In designing control statement we should use the number line to make it easier to find true area of each condition. For example, suppose we have the following control statement:
if (b >= 20)
....
else if (b < 20) AND (b >= 10)
....
else if (b <= 5) AND (b >= 0)
....
else
....
In above example, there are 4 condition with each number line as follow
The first condition will be checked first before the others. If the condition is true, then the other condition won’t be checked by compiler. However if it false, then the second condition is checked. If it true, then the following condition will be skipped. But if it false, the third condition will be checked. And so on until the fourth condition. This occurs because the control statement are still in one block.
The following example illustrates the importance of using number line if
(b >= 20)
....
else if (b < 20) AND (b >= 25)
....
The number line is as follows
There is a regional disparities between the first conditions and the second one. Inequality occurs at b >= 25. If both first and second conditions are meets. Only the statement in the first condition that will be executed, because the first conditions encountered first by the compiler and the second condition was skipped.
Block condition begins with the word if, and ends with the words else or new block condition. for example
if (condition)
....
if (condition)
....
else if (condition)
....
else if (condition) AND (condition)
....
else
....
if (condition)
....
else if (condition)
....
In the example above there are three blocks of control statement. Block 1 is the first row, block 2 is the line 25, and block 3 is the line 67. Block condition different with nested control statement. Each block has a equal position.
Here's an example of a nested control statement
if (condition)
{
statement;
....
if (condition)
statement;
else if (condition)
statement;
else if (condition) AND (condition)
{
if (condition)
statement;
else if (condition)
statement;
else
statement;
}
else
statement;
}
The example above has a different block control statement. But the three blocks of control statement is not the same level. The third block is part of the second block and the second block is part of the first block.
Another Sample Program
#include <iostream>
using namespace std;
int main()
{
int input;
cout << "insert day to- = ";
cin >> input;
if (input == 1)
{
cout << "you enter Sunday";
}
else if (input == 2)
{
cout << "you enter Monday";
}
else if (input == 3)
{
cout << "you enter Tuesday";
}
else if (input == 4)
{
cout << "you enter Wednesday";
}
else if (input == 5)
{
cout << "you enter Thursday";
}
else if (input == 6)
{
cout << "you enter Friday";
}
else if (input == 7)
{
cout << "you enter Saturday";
}
else
{
cout << "not input the day/wrong input";
}
return 0;
}
Source code example using Conditional IF
This example will be compared with the switch ... case control statement.
Switch ... Case
Switch ... case control statement can only be used in condition using equal relation. In the last if ... else example, all the condition use equal relation. Therefore, we can use switch ... case instead of if ... else. Examples are as follows.#include <iostream>
using namespace std;
int main()
{
int input;
cout << "insert day to- = ";
cin >> input;
switch(input)
{
case 1 : cout << "Sunday";
break;
case 2 : cout << "Monday";
break;
case 3 : cout << "Tuesnday";
break;
case 4 : cout << "Wednesday";
break;
case 5 : cout << "Thursday";
break;
case 6 : cout << "Friday";
break;
case 7 : cout << "Saturday";
break;
default : cout << "not input the day/wrong input";
break;
}
return 0;
}
Source code example using Conditional CASE
switch ... case simplifies repetitive writing if else and conditions. Typographical errors can be reduced. In addition checking the conditions would become easier. There are additional commands ie break. It used to get out of the switch statement if one of the conditions have been met. With the break, when one case is met then the other cases below are not checked. Therefore, with the break we could distinguish whether the condition is still in a block or not.
Default is the replacement of the previous examples else. If no conditions are met then the default will be executed.
In a different example
#include <iostream>
using namespace std;
int main()
{
int input;
cout << "insert day to- = ";
cin >> input;
switch(input)
{
case 1 : cout << "Sunday";
case 2 : cout << "Monday";
case 3 : cout << "Tuesnday";
case 4 : cout << "Wednesday";
case 5 : cout << "Thursday";
case 6 : cout << "Friday";
case 7 : cout << "Saturday";
default : cout << "not input the day/wrong input";
}
return 0;
}
Source code example using Conditional CASE DEFAULT
Since the break in each statement is omitted, then the blocks of each condition will be expanded include blocks of the underlying condition. If any of these conditions are met then the statement on the underlying condition will be executed as well. For example we inputing 4, then the output will be rabu kamis jumat sabtu bukan input hari/salah masukan, all five from condition 4.
Exercise
1. Create a program that can convert a number value that the user inputted into the value of the letter, with conditions:
- The value of 85-100, got A.
- The value of 70-84 got B.
- The value of 40-69 got C.
- The value of 20-39 got D.
- other than that got E.
2. Create a program to determine the greatest number of 3 pieces of numbers inputted by the user.
for example:
enter the 1st number: 15
enter the second number: 7
enter the third number: 22
EmoticonEmoticon