KEMBAR78
03 CP Lecture-03 - C++ Control Statements | PDF | Control Flow | Boolean Data Type
0% found this document useful (0 votes)
11 views54 pages

03 CP Lecture-03 - C++ Control Statements

The document provides an overview of C++ control statements, including if, if-else, if-else if, nested if statements, switch statements, and looping structures such as while, do-while, and for loops. It explains the syntax and functionality of these control structures, along with examples and flow charts to illustrate their operation. Additionally, it covers loop control statements like break, continue, and goto, emphasizing their usage and best practices in programming.

Uploaded by

onetechet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views54 pages

03 CP Lecture-03 - C++ Control Statements

The document provides an overview of C++ control statements, including if, if-else, if-else if, nested if statements, switch statements, and looping structures such as while, do-while, and for loops. It explains the syntax and functionality of these control structures, along with examples and flow charts to illustrate their operation. Additionally, it covers loop control statements like break, continue, and goto, emphasizing their usage and best practices in programming.

Uploaded by

onetechet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Computer Programming

Using

Course Code: CoSc 1012

April-2025
Chapter III
C++ Control Statements

2
Cont…
If the Boolean expression evaluates to true, then the block of
code inside the if statement will be executed.

If Boolean expression evaluates to false, then the first set of


code after the end of the if statement (after the closing curly
brace) will be executed.

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 and 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. 3
Cont…
The following flow chart shows the simple if statement:

Let’s look the following example

4
Cont…
 For example, the following code fragment prints x is 100 only
if the value stored in the x variable is indeed 100:

 If we want more than a single statement to be executed in


case that the condition is true we can specify a block using
braces { }:

5
Cont…
B. The if else statement.

We can additionally specify what we want to happen if the


condition is not fulfilled by using the keyword else.

An if statement can be followed by an optional else


statement, which executes when the Boolean expression is
false.

6
Cont….
If the Boolean expression evaluates to true, then the if block of
code will be executed, otherwise else block of code will be
executed. The following flow chart shows the if else statement:

7
Example

When this code is compiled


and executed, it produces
the following result:
a is not less than 20;
value of a is : 100

8
Cont…
C. The if else if statement.

An if statement can be followed by an optional else if...else


statement, which is very useful to test various conditions using
single if...else if statement.

When using if, else if, else statements there are few points to
keep in mind.
 An if can have zero or one else's and it must come after any
else if's.
 An if can have zero to many else if's and they must come
before the else.
 Once an else if succeeds, none of he remaining else if's or
else's will be tested. 9
Cont…
When we compile and execute this program, the following four
lines are displayed on the screen.

When the above code is compiled and


executed, it produces the following result:
Value of a is not matching
Exact value of a is : 100 10
Cont…
Let’s look the following example which a number from
keyboard and checks weather the number is negative or
positive.

N.B: in case that we want more


than a single statement to be
executed, we must group them in
a block by enclosing them in
braces { }, otherwise using braces
for single statement is optional.

11
Cont…
D. The nested if statement.
It is always legal to nest if-else statements, which means you
can use one if or else if statement inside another if or else if
statement(s).

One or more if statements can be nested with in another if


statement. The nesting will be used to test multiple conditions
to perform a task. It is always recommended to indent nested if
statements to enhance readability of a program.

12
Cont….

→ StatementsN will be executed if and only if


“expression1” and “expression2” are evaluated
and if the outcome of both is none zero (TRUE).
→ StatementsM will be executed if and only if
“expression1” is TRUE and “expression2” is
FALSE.
→ StatementsR will be executed if and only if
“expression1” is FALSE and “expression3” is
TRUE.
→ StatementsT will be executed if and only if
“expression1” is FLASE and “expression3” is
FALSE. 13
Example

When this code is compiled


and executed, it produces the
following result:
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200

14
Cont…
II. The switch statement
A switch statement allows a variable to be tested for equality
against a list of values.

Each value is called a case,

and the variable being

switched on is checked for

each case.

15
Cont…
The following rules apply to a switch statement:
 The expression and case_value used in a switch statement must have an
integral or enumerated type.

 You can have any number of case statements within a switch. Each case
is followed by the value to be compared to and a colon.
 The case_value for a case must be the same data type as the variable in the
switch.
 When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
 When a break statement is reached, the switch terminates, and the flow of
control jumps to the next line following the switch statement.
 Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
 A switch statement can have an optional default case, which must appear at the
end of the switch. The default case can be used for performing a task when 16
Cont…
 Note: It is almost always a good idea to have a default case in switch statements.
If you have no other need for the default, use it to test for the impossible case, and
print out an error message; this can be a tremendous aid in debugging.
Consider the following example:

17
Cont….
Now let us see the effect of including a break statement in the
switch statement.

18
Cont…
The above scenario can be expressed diagrammatically as
follows:

The break terminates the switch statement by jumping to the very end of it.
19
Example

20
Cont…
Because case ‘x’ has no break statement (in fact no statement
at all!), when this case satisfied, execution proceeds to the
statements of the next case and the multiplication is
performed.

Switch evaluates expression and compares the result to each


of the case values. Relational and Boolean operators can be
used in switch tag if and only if the expected output is either 0
to represent False or 1 to represent true as that is the only
possible output from such operators.
21
Looping (Iterative) statements
(WHILE, DO … WHILE, FOR)
Looping (Iterative) Statements
(WHILE, DO … WHILE, FOR)

22
Introduction to Looping
Statements
The great power of computers is their ability to perform a task
over and over again. With a phenomenal speed, accuracy and
reliability. There may be a situation, when you need to execute
a block of code 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 and following is the general form of a loop
statement in most of the programming languages:

23
Cont…
The following flow chart shows the general flow of loop
statements

Three facilities offered by C++ for performing repetition are:


the while loop, the do-while loop, and the for loop 24
I. The while loop
A while loop statement repeatedly executes a target statement
as long as a given condition is true. The while statement (also
called while loop) provides a way of repeating a statement or a
block as long as a condition holds / is true.
Note: Here, statement(s) may be a single
statement or a block of statements. The
condition may be any expression, and true
is any non-zero value. The loop iterates
while the condition is true. When the
condition becomes false, program control
passes to the line immediately following
the loop.
25
Cont…
The following Flow Chart shows the while loop

Note: Here, key point of


the while loop is that the
loop might not ever run.
When the condition is
tested and the result is
false, the loop body will be
skipped and the first
statement after the while
loop will be executed.
26
Cont..
Let’s look the following example: which adds numbers from 1
to 10:

After execution of the


following program will
display 55

27
II. The do…while loop
Unlike the while loop, which test the loop condition at the top of
the loop, the do...while loop checks its condition at the bottom
of the loop. A do...while loop is similar to a while loop, except
that a do...while loop is guaranteed to execute at least one time.
Notice that: the conditional expression
appears at the end of the loop, so the
statement(s) in the loop execute once
before the condition is tested. If the
condition is true, the flow of control jumps
back up to do, and the statement(s) in the
loop execute again. This process repeats
until the given condition becomes false. 28
Cont….
The Following Flow Chart which shows the do while loop:

29
Cont…
Consider the following example: Table of Content

When this code is compiled and


executed, it produces the following
result:
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

30
III. The for loop
A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific number
of times.

31
Cont…
Here is the flow of control in a for loop:
1. The initialization step is executed first, and only once. This
step allows you to declare and initialize any loop control
variables. You are not required to put a statement here, as
long as a semicolon appears.

2. Next, the condition is evaluated. If it is true, the body of the


loop is executed. If it is false, the body of the loop does not
execute and flow of control jumps to the next statement just
after the for loop.

32
Cont..
3. After the body of the for loop executes, the flow of control
jumps back up to the increment/decrement statement. This
statement allows you to update any loop control variables. This
statement can be left blank, as long as a semicolon appears
after the condition.

4. The condition is now evaluated again. If it is true, the loop


executes and the process repeats itself (body of loop, then
increment/decrement step, and then again condition). After the
condition becomes false, the for loop terminates.

33
Cont…
The general flow diagram for the for loop is like this:

34
Cont…
Let’s try to consider the following example:
When this code is compiled
and executed, it produces
the following result:
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

35
IV. Nested loop statements
A loop can be nested inside of another loop. C++ allows at
least 256 levels of nesting

36
Cont…

37
Cont…

38
Other Statements Like:
(BREAK, CONTINUE & EXIT)

39
Flow Control Statements
Loop control statements change execution from its normal
sequence. When execution leaves a scope, all automatic objects
that were created in that scope are destroyed. C++ supports
the following control statements.
 The break statement

 The continue statement

 The goto statement

40
I. The break statement
The break statement ends execution of the nearest enclosing
loop or conditional statement in which it appears. Control
passes to the statement that follows the end of the statement,
if any.

The break statement is used with the conditional switch


statement and with the do, for, and while loop statements. In
a switch statement, the break statement causes the program
to execute the next statement outside the switch statement.

41
Cont…
Without a break statement, every statement from the matched
case label to the end of the switch statement, including the
default clause, is executed.

In loops, the break statement ends execution of the nearest


enclosing do, for, or while statement. Control passes to the
statement that follows the ended statement, if any.

Within nested statements, the break statement ends only the


do, for, switch, or while statement that immediately encloses
it. You can use a return or goto statement to transfer control
from more deeply nested structures.
42
Cont…
Consider the following

43
Rules For Naming Variable
Example, the following code shows how to use the break
statement in a for loop.
The output of this program is
1
2
3
4

44
How are variables used?
The following code shows how to use break in a while loop and
a do loop.

45
II. The continue statement
Forces transfer of control to the controlling expression of the
smallest enclosing do, for, or while loop.

Any remaining statements in the current iteration are not


executed. The next iteration of the loop is determined as
follows:
 In a do or while loop, the next iteration starts by reevaluating the
controlling expression of the do or while statement.
 In a for loop (using the syntax for(initialization; condition;
increment/decrement)), the increment/decrement clause is
executed. Then the condition clause is reevaluated and,
depending on the result, the loop either ends or another iteration
occurs.
46
Cont…
Consider the following:

47
Cont…
Example: C++ program to display integer from 1 to 10 except
6 and 9 In this program, when i
is 6 or 9, execution of
statement
cout<<i<<"\t";
is skipped inside the
loop using continue;
statement.

48
III. The goto statement
In C++ programming, goto statement is used for altering the
normal sequence of program execution by transferring control to
some other part of the program. A goto statement provides an
unconditional jump from the goto to a labeled statement in the
same function
Where label is an identifier
that identifies a labeled
statement.
A labeled statement is any
statement that is preceded by
an identifier followed by a
colon (:).
49
Cont…
 The labeled statement designated by identifier must be in
the current function. Labels must be written as a valid
identifiers. A statement label is meaningful only to a goto
statement; otherwise, statement labels are ignored. Labels
cannot be re declared.

 Note: It is good programming style to use the break,


continue, and return statements instead of the goto
statement whenever possible. However, because the break
statement exits from only one level of a loop, you might have
to use a goto statement to exit a deeply nested loop.
50
Cont…
Consider the following.

51
Cont…
Example

52
Cont…
The out put of the above program is:

53
54

You might also like