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

5 Key excerpts on "Conditional Statement"

  • Book cover image for: Introduction to Computing Using Python
    eBook - PDF

    Introduction to Computing Using Python

    An Application Development Focus

    • Ljubomir Perkovic(Author)
    • 2012(Publication Date)
    • Wiley
      (Publisher)
    Conditional and iteration control structures are together referred to as execution con- trol structures. Execution control structures are used to control the flow of execution of the statements in a program. In other words, they determine the order in which the state- ments are executed, under what conditions, and how many times. Together with assignment statements, execution control structures are the fundamental building blocks for describing computational solutions to problems and developing algorithms. We introduce Python’s execution control structures in Chapter 3, after having reviewed Python’s core data types in Chapter 2. Chapter Summary This chapter introduces the field of computer science, the work computer scientists and developers do, and the tools that computer scientists and developers use. Computer science studies, on one hand, the theoretical foundations of information and computation and, on the other, the hands-on techniques to implement applications on com- puter systems. Computer application developers use the concepts and techniques of com- puter science in the context of application development. They formulate abstract repre- sentations that model a particular real or imaginary environment, create algorithms that manipulate data in the model, and then implement the algorithm as a program that can be executed on a computer system. The computer science tools include the abstract tools of math and logic and the concrete computer system tools. Computer system tools include the hardware and the software. In particular, they include the programming language and the programming language tools through which the developer ultimately controls the different system components. The abstract tools that computer scientists use are the computational thinking skills, based on logic and mathematics, that are necessary to describe problems, tasks, and pro- cesses through the lens of abstraction and computation.
  • Book cover image for: 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
  • Book cover image for: Introduction to Computing Using Python
    eBook - PDF

    Introduction to Computing Using Python

    An Application Development Focus

    • Ljubomir Perkovic(Author)
    • 2015(Publication Date)
    • Wiley
      (Publisher)
    Conditional and iteration control structures are together referred to as execution con- trol structures. Execution control structures are used to control the flow of execution of the statements in a program. In other words, they determine the order in which the state- ments are executed, under what conditions, and how many times. Together with assignment statements, execution control structures are the fundamental building blocks for describing computational solutions to problems and developing algorithms. We introduce Python’s ex- ecution control structures in Chapter 3, after having reviewed Python’s core data types in Chapter 2. Chapter Summary This chapter introduces the field of computer science, the work computer scientists and developers do, and the tools that computer scientists and developers use. Computer science studies, on one hand, the theoretical foundations of information and computation and, on the other, the hands-on techniques to implement applications on com- puter systems. Computer application developers use the concepts and techniques of com- puter science in the context of application development. They formulate abstract representa- tions that model a particular real or imaginary environment, create algorithms that manipu- late data in the model, and then implement the algorithm as a program that can be executed on a computer system. The computer science tools include the abstract tools of math and logic and the concrete computer system tools. Computer system tools include the hardware and the software. In particular, they include the programming language and the programming language tools through which the developer ultimately controls the different system components. The abstract tools that computer scientists use are the computational thinking skills, based on logic and mathematics, that are necessary to describe problems, tasks, and pro- cesses through the lens of abstraction and computation.
  • Book cover image for: ANSI C Programming
    eBook - ePub

    ANSI C Programming

    Learn ANSI C step by step

    Chapter 1 , we have used sequence control structure in which the various steps are executed sequentially, i.e. in the same order in which they appear in the program. In fact, to execute the instructions sequentially, we don’t have to do anything at all. By default, the instructions in a program are executed sequentially. However, in serious programming situations, seldom do we want the instructions to be executed sequentially. Many a time, we want a set of instructions to be executed in one situation, and an entirely different set of instructions to be executed in another situation. This kind of situation is dealt with in C programs using a decision control instruction. As mentioned earlier, a decision control instruction can be implemented in C using:
    (a) The if statement
    (b) The if-else statement
    (c) The conditional operators Now let us learn each of these and their variations in turn.

    The if Statement

    Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this:
    if ( this condition is true )   execute this statement;
    The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true, then the statement is not executed; instead the program skips past it. But how do we express the condition itself in C? And how do we evaluate its truth or falsity? As a general rule, we express a condition using C’s ‘relational’ operators. The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than the other. Here’s how they look and how they are evaluated in C.
    Figure 4.1
    The relational operators should be familiar to you except for the equality operator == and the inequality operator != . Note that = is used for assignment, whereas, == is used for comparison of two quantities. Here is a simple program, which demonstrates the use of if and the relational operators.
    On execution of this program, if you type a number less than 10, you get a message on the screen through printf( ) . If you type some other number the program doesn’t do anything. The flowchart given in Figure 4.2
  • Book cover image for: Programming with C++
    • Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons, , Kyla McMullen, Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons(Authors)
    • 2021(Publication Date)
    Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. PROGRAMMING WITH C++ 102 Boolean expressions conditional logical operator Conditional Statement control structure decision control structure else-if structure equal operator fall through if-then structure if-then-else structure nested-if structures relational operator switch-case structure truth table Key Terms SUMMARY • A control structure alters the sequential execution of statements in a computer program. A decision control structure alters the sequential flow by branching to specific statements based on a condition or decision. Decision control structures help programs seem intelligent because they can make responses that correspond to user input. • Decision control structures can be classified as if-then, if-then-else, nested-if, else-if, and switch-case. • Decision control structures begin with a Conditional Statement such as if choice == 1. • Conditional Statements include relational operators, such as == != > < >= and <=. • The == equal operator, not the = assignment operator, is used in Conditional Statements. • Relational operators and operands form Boolean expressions, such as choice == 1, that evaluate to True or False. • Boolean expressions have similarities to Boolean data types. Both carry a value of True or False. In practice, however, you use a Boolean expression as part of a Conditional Statement, whereas you use a Boolean data type when making declaration or assignment statements. • Fall through refers to program execution that continues to the next Conditional Statement within a struc- ture. Switch-case structures typically have fall through unless break keywords are added.
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.