A statement is a command given to the computer that instructs the computer to take a specific action, such as display to the screen, or collect input. A computer program is made up of a series of statements.
- Decision Making Statements
- Loop Control Statements
Decision Making Statements:
Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Type of Decision Making Statements:
- if statementAn if statement consists of a boolean expression followed by one or more statements.
- if...else statementAn if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
- nested if statementsYou can use one if or else if statement inside another if or else if statement(s).
- switch statementA switch statement allows a variable to be tested for equality against a list of values.
- nested switch statementsYou can use one switch statement inside another switch statement(s).
Loop Control Statements:
You may encounter situations, when a block of code needs to be executed several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times.
Types of Loop:
- while loopRepeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
- for loopExecutes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
- do...while loopIt is more like a while statement, except that it tests the condition at the end of the loop body.
- nested loopsYou can use one or more loops inside any other while, for, or do..while loop
No comments:
Post a Comment