Basic Programming Chapter Four - Handout
Basic Programming Chapter Four - Handout
CONTROL STATEMENTS
Introduction
A C++ control statement redirects the flow of a program in order to execute additional code. These
statements come in the form of conditionals (if-else, switch) and loops (for, while, do-while). Each of them
relies on a logical condition that evaluates to a Boolean value in order to run one piece of code over another.
4.1 Selection Statements: if and switch statements
4.1.1 THE IF STATEMENT
Normally the execution of a program flows line by line in the order in which it appears in the source code.
The if statement enables the programmer to test for a condition (such as whether two variables are equal)
and branch to different parts of the code depending on the result. The following if statement
if (x > 0.0)
cout<< "The value of x is positive";
will print out the message ` The value of x is positive' if x is positive.
The general form of the if statement is:
if (condition)
statement;
- OR -
if (condition) {
statement;
}
where condition is any valid logical expression or a Boolean expression. The statement can be a single C++
statement of any kind and must be terminated by a semi-colon. It can also be a compound statement, which
is a sequence of statements enclosed in left and right braces ({ and }) and acts as a single statement. The
closing right brace (}) is not followed by a semi-colon.
Following is the general form of a typical decision-making structure found in most of the programming
languages
1 while loop: Repeats a statement or group of statements while a given condition is true. It
tests the condition before executing the loop body.
2 for loop: Execute a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
3 do...while loop: Like a ‘while’ statement, except that it tests the condition at the end of the
loop body.
4 nested loops: You can use one or more loop inside any another ‘while’, ‘for’ or ‘do..while’
loop.
Syntax:
initialization expression;
do {
// statements
update_expression;
} while (test_expression);
Note: Notice the semi – colon(“;”) in the end of loop.
#include <iostream>
using namespace std;
int main()
{
int i = 2; // Initialization expression
do
{
// loop body
loop, on line 16, initializes a counter (i) to 0, and then the body of the outer for loop is run.
Sample dialogue/run
Enter 10 negative
-1 -2 -3 4 -5 -6 -7 -8 -9 -10
ERROR: positive number or zero was entered as the
4th number! Input ends with the 4th number.
4th number was not added in
-6 is the sum of the first 3 number
Analysis: In the play shown in the output the user lost; small became larger than the large before the target
number of 6 was reached
c. The exit statement
The exit statement is written
exit(integer_value);
when the exit statement is executed, the program ends immediately. Any integer value may be used, but
by convention, 1 is used for a call to exit that is caused an error, and 0 is used in other cases. The exit
statement is a call to the function exit, which is in the library with header file named stdlib.h. Therefore
any program that uses the exit statement must contain the following include directive:
#include<stdlib.h>
d. The goto statement:
The goto statement in C/C++ also referred to as unconditional jump statement can be used to jump from
one point to another within a function.
Output:1 2 3 4 5 6 7 8 9 10
Remark: return statement is used mainly in functions which will be discussed and demonstrated in a
lesson which is part the next chapter.