Preview

Simple Program for Student C++

Good Essays
Open Document
Open Document
283 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Simple Program for Student C++
Control Structures |

| |A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what has to be done by our program, when and under which circumstances.
With the introduction of control structures we are going to have to introduce a new concept: the compound-statement or block. A block is a group of statements which are separated by semicolons (;) like all C++ statements, but grouped together in a block enclosed in braces: { }:
{ statement1; statement2; statement3; }
Most of the control structures that we will see in this section require a generic statement as part of its syntax. A statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block), like the one just described. In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block.

Conditional structure: if and else

The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is: if (condition) statement
Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.
For example, the following code fragment prints x is 100 only if the value stored in the x variable is indeed 100:
|if (x == 100) |

You May Also Find These Documents Helpful

  • Good Essays

    Declares > this is the statements that declare variables, constants, and other code elements, which can then be used within that block…

    • 193 Words
    • 1 Page
    Good Essays
  • Satisfactory Essays

    SD1230 Lab 1

    • 239 Words
    • 2 Pages

    2. What type of instruction is used to execute different code based on a condition? -Jumps…

    • 239 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Homework Unit 3

    • 354 Words
    • 2 Pages

    4. AND operator- combines more than one condition in a program. Makes a compunf Boolean operation.…

    • 354 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    unit 7 assignment 1

    • 431 Words
    • 2 Pages

    5. What three actions do count-controlled loops typically perform using the counter variable? Initialization: Before the loop begins, the counter variable is initialized to a starting value. Test : The loop tests the counter variable by comparing it to a maximum value. If the counter variable is less than or equal to the maximum, the loop iterates. If the counter is greater than the maximum value, the program exits the loop. Increment : To increment a variable means to increase its value. During each iteration, the loop increments the counter variable by adding a predetermined amount to it…

    • 431 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    DQ 1: Review the definition of control structure on p. 45 in Extended Prelude to Programming: Concepts and Design (2nd ed.). Then, think about the pseudocode algorithm you would write for a simple task (making a peanut butter sandwich, for example) as well as three simple control structures that could be used to create this algorithm.…

    • 437 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Nt1310 Unit 1

    • 4209 Words
    • 17 Pages

    If two steps are indented at the same level, the uppermost statement is executed first. ans)All of the above…

    • 4209 Words
    • 17 Pages
    Powerful Essays
  • Satisfactory Essays

    Pt1420 Assignment

    • 1058 Words
    • 5 Pages

    Because by indenting the statements in the body of the loop you visually set them apart from the surrounding code. This makes your program easier to read and debug…

    • 1058 Words
    • 5 Pages
    Satisfactory Essays
  • Powerful Essays

    EAS230Syllabus

    • 1748 Words
    • 8 Pages

    C++ programming: editing, compiling, user I/O, variables (ints, doubles, char, strings, booleans), loops, decisions, functions, pointers, arrays, tables, databases, sorting.…

    • 1748 Words
    • 8 Pages
    Powerful Essays
  • Satisfactory Essays

    IT 210 appendix d

    • 264 Words
    • 2 Pages

    Match the activity or purpose on the left with the appropriate description on the right by typing in the corresponding letter under the Answer column.…

    • 264 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Intro the Programming

    • 386 Words
    • 5 Pages

    The ________ is a collection of statements enclosed inside a set of curly braces that are performed when the method is…

    • 386 Words
    • 5 Pages
    Satisfactory Essays
  • Good Essays

    III. Insert a line within the loop structure to determine the value of the variable such as in the following Basic language code:…

    • 733 Words
    • 4 Pages
    Good Essays
  • Good Essays

    1. The logical structure in which one instruction occurs after another with no branching is a ______A______.…

    • 765 Words
    • 4 Pages
    Good Essays
  • Satisfactory Essays

    The expression 1 To 5 in the Case 1 To 5 clause specifies the range of numbers from ____.…

    • 595 Words
    • 3 Pages
    Satisfactory Essays
  • Good Essays

    Loop

    • 7924 Words
    • 32 Pages

    isual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.…

    • 7924 Words
    • 32 Pages
    Good Essays
  • Good Essays

    int function1(char x) { //parameter x passed to the function, function returns an integer value…

    • 1596 Words
    • 7 Pages
    Good Essays