Computer Science

Conditional Statement

A conditional statement in computer science is a programming construct that allows for different actions to be taken based on whether a certain condition is true or false. It typically consists of an "if" clause that specifies the condition to be evaluated, and may also include an "else" clause to define an alternative action if the condition is not met.

Written by Perlego with AI-assistance

2 Key excerpts on "Conditional Statement"

  • Processing
    eBook - ePub

    Processing

    An Introduction to Programming

    4 Conditional Programming with if
    Up to now, all our programs have been what are sometimes known as straight-line programs, because all of the statements in such a program are simply executed in the order in which they are written, from first to last.
    In this chapter, we wish to look at statements that are conditional in that they involve one or more actions that are performed only when a certain condition is true. Such Conditional Statements are also known as examples of selection , because they specify actions that are performed only selectively .
    Recipe Analogy
    A cooking recipe can provide a helpful analogy when we think about what a computer program is. Like a recipe, a program involves performing a certain set of individual actions in a particular sequence, from beginning to end, to achieve a specific end result. In computer programming, such a sequence of actions that produces a desired end result is also known as an algorithm .
    However, there are times when we might wish to perform one or more of the individual actions in such a sequence selectively —that is, only if a certain condition is true. Consider our recipe analogy. Butter can be purchased salted or unsalted. Thus, a recipe might include a statement such as
    If you are using un salted butter, add ¼ teaspoon of salt.
    According to this instruction, we do not always want to add this ¼ teaspoon of salt to the recipe. Rather, this action is conditional . Specifically, if the condition is true (the butter is indeed unsalted), then we do want to perform the action of adding salt. However, if our condition is false (the butter is not unsalted, but rather is salted butter), then we do not perform the action of adding salt.
    Such a conditional action is sometimes represented as a flowchart :
    Similarly, a computer program may include conditional statements. Such statements are performed selectively , only if a certain condition
  • Python Made Simple
    eBook - ePub

    Python Made Simple

    Learn Python programming in easy steps with examples

    Decision control statements are also called selection control statements. The decision control statement alters the normal sequential execution of the statement of the program depending on the test condition to be carried out at a particular point in the program. The decision to be taken regarding where the control should transfer depends on the outcome of the test condition. The following decision control statements are supported by Python, which are explained in the following sections.

    The if statement

    The simple if statement is the most simple and powerful decision control statement that allows Python to control the sequence of execution of the statements. The if statement is used to execute one or more statements only if the condition is true. The syntax of the if statement is as follows:
    if condition:   statement
    The keyword if tells the interpreter that a decision control statement needs to be executed. The condition following the if keyword includes any relational or logical expression which is always followed by a colon (: ) symbol. The statements in the if blocks are written only after the colon. If the condition is true, then the statements in the if block are executed. If the condition is false, then the statements in the block are ignored and the control passes to the next statement following the if construct. If a block is created using indentation. To create a block of the if statement, we need to use proper indentation with every statement inside the if block. This means that if the first statement inside the if block is written with four spaces, then the other statements inside the same if block should be written with the same number of spaces. Any changes made in the number of spaces of statements inside the if block results in an error. The following diagram shows the flowchart of a simple if
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.