Decision making is one of the integral parts of your day to day life. Consciously or subconsciously, you keep making decisions based on the prevailing conditions. If the condition is favorable, you perform a specific task, otherwise you either don’t perform the task or perform an alternate task. For example, you decide drive only when you need to travel to a distant location. However, if the destination is close enough, you might want to walk down to the destination.
In most of the programming languages, to make a decision, you can use the if construct. If you recall the decision element of the flowchart, it maps to the if construct in a program. The if construct helps you to make decision depending on a condition. Across programming languages, the if construct is built of these components:
Condition, which is used along with the if keyword. In the condition component, you use an expression to check if the condition is true or false, generally by using a relational operator or a Boolean expression. In some of the programming languages, such as C++, Perl and Java, you use the condition is parenthesis. Similarly, in UNIX shell scripts, you enclose the Boolean expression within square brackets. However, is some of the programming languages, such as Python, SQL, and DOS batch files you do not need any parenthesis to enclose the Boolean expression.
After the condition, the if construct has a body that contains various statements that should be run if the condition is true. The body of the if construct is enclosed between delimiters. Different programming languages have their own conventions to use delimiters for the body of the if construct.
As you might have noticed in the decision element of a flowchart, every decision has two aspects – yes and no. Therefore, depending on the evaluation of the condition, you complete one of the set of activities of the flowchart. Similarly, in an if construct, you can use else statement in the if construct. The group of statements you use under the else statement are run only if the condition evaluates to false. This means that if the condition is true, the block of statement immediately after the if statement is run. However, if the condition evaluates to false, the block of statements following the else statement is run.
Irrespective of the condition evaluating to true or false, the statements following the if construct are run as soon as the if construct is completed.
На этой странице сайта вы можете посмотреть видео онлайн Programming Basics – Using the if Construct in Pseudocodes длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Let's Simplify Computers with MrigVats Learning 02 Май 2019, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 5,111 раз и оно понравилось 156 зрителям. Приятного просмотра!