KEMBAR78
ITEC1030 - Ch04 - Control Statments of Programming | PDF | Control Flow | Software Development
0% found this document useful (0 votes)
11 views24 pages

ITEC1030 - Ch04 - Control Statments of Programming

The document discusses control statements in programming, focusing on flow control, sequential statements, selection statements (if and switch), and repetition statements (for, while, do-while). It explains how these statements allow programmers to dictate the execution flow of a program based on conditions and iterations. The document provides syntax examples and explanations for each type of control statement.

Uploaded by

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

ITEC1030 - Ch04 - Control Statments of Programming

The document discusses control statements in programming, focusing on flow control, sequential statements, selection statements (if and switch), and repetition statements (for, while, do-while). It explains how these statements allow programmers to dictate the execution flow of a program based on conditions and iterations. The document provides syntax examples and explanations for each type of control statement.

Uploaded by

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

Welcome

Department of information
Technology
Basic Computer
Programming
_________________________________________
@ 2024 FTVT Institute All Rights Reserved

OF/FTI/ALL /18 Issue No: 1


PPT 1
CHAPTER 4:
Control statements of
Programming

2
Introduction

• A running program spends all of its time executing


instructions or statements in that program.
• The order in which statements in a program are
executed is called flow of that program.
• Programmers can control which instruction to be
executed in a program, which is called flow control.
• This term reflects the fact that the currently executing
statement has the control of the CPU, which when
completed will be handed over (flow) to another
statement.

3
Department of Information AUGUST
• Flow control in a program is typically sequential,
from one statement to the next.
• But we can also have execution that might be divided
to other paths by branching statements. Or
perform a block of statement repeatedly until a
condition fails by Repetition or looping.
• Flow control is an important concept in programming
because it will give all the power to the programmer
to decide what to do to execute during a run and
what is not, therefore, affecting the overall outcome
of the program.

4
Department of Information AUGUST
Sequential Statements

• Are instruction in a program which will executed one


after the other in the order scripted in the program.
• In sequential statements, the order will be
determined during program development and cannot
be changed.

5
Department of Information AUGUST
Selection Statements

• Are statements in a program where there are points


at which the program will decide at runtime
whether some part of the code should or should not
be executed.
• There are two types of selection statements in C+
+, which are the “if statement” and the “switch
statement

6
Department of Information AUGUST
The if Statement
• It is sometimes desirable to make the
execution of a statement dependent upon a
condition being satisfied.
• The different forms of the ‘If” statement
used to decide whether to execute part of
the program based on a condition which will
be tested either for TRUE or FALSE result.
• The different forms of the “If” statement
are:
 The simple if statement
 The If else statement
 The if else if statement

7
Department of Information AUGUST
The simple if statement
• The simple if statement will decide only one part
of the program to be executed if the condition is
satisfied or ignored if the condition fails.
• The General Syntax is:
if (expression)
statements;
• In any “if” statement, first the “expression”
will be evaluated and if the outcome is non zero
(which means TRUE), then the “statements” is
executed.
• Otherwise, nothing happens (the statement will
not be executed) and execution resumes to the
line immediately after the “if” block.

8
Department of Information AUGUST
• To make multiple statements dependent on the same
condition we can use a compound statement, which will be
implemented by embracing the group of instructions within
the left “{“ and right “}” French bracket.

Eg1:
if(age>18)
cout<<”you are an adult”;

Eg2:
if(balance > 0)
{

interest = balance * creditRate;


balance += interest;
}

OF-FTI-ALL -18 9 9
Department of Information AUGUST
• Most of the time “expression” will have relational expressions testing whether
something is equal, greater, less, or different from something else.
• It should be noted that the output of a relational expression is either True
(represented by anything different from zero) or False (represented by Zero).
• Thus any expression, whose final result is either zero or non-zero can be used in
”expression”
Eg:
int x;
cin >>x;
if(x)
cout <<”you are an adult”;
• Thus, expression can be:
 Relational expression,
 A variable,
 A literal constant, or
 Assignment operation, as the final result is whatever we have at the right hand
side and the one at the left hand side is a variable.

OF-FTI-ALL -18 1 10
Department of Information AUGUST 0
The If else statement
• Another form of the “if” is the “if …else” statement.
• The “if else ” statement allows us to specify two alternative
statements:
• One which will be executed if a condition is satisfied and
• Another which will be executed if the condition is not satisfied.
• The General Syntax is:
if (expression)
statement1;
else
statment2;
• First “expression” is evaluated and if the outcome is none zero
(true), then “statements1” will be executed. Otherwise, which
means the “expression” is false “statements2” will be
executed.

1
Department of Information AUGUST 1
E.g.:
if(balance > 0)
{

interest = balance * creditRate;


balance += interest;
}

else
{

interest = balance * debitRate;


balance += interest;
}

OF-FTI-ALL -18 1 12
Department of Information AUGUST 2
“if …else if”
• The “if else if” statement allows us to specify more
than two alternative statements each will be
executed based on testing one or more conditions

The General Syntax is:


if (expression1)
statements1;
else if(expression2)
statements2;
.
.
.
else if(expressionN)
statementsN;
else
statements

1
Department of Information AUGUST 3
The Switch Statement
• Another C++ statement that implements a selection
control flow is the switch statement (multiple-choice
statement).
• Provides a way of choosing between a set of alternatives
based on the value of an expression.
• The switch statement has four components:
Switch
Case
Default
Break
• Where Default and Break are Optional.

1
Department of Information AUGUST 4
The General Syntax is:
switch(expression)
{

case constant1:
statements;
.
.
case constant n:
statements;
default:
statements;
}

• First expression (called the switch tag) is evaluated, and the outcome is compared to
each of the numeric constants (called case labels), in the order they appear, until a
match is found.
• The statements following the matching case are then executed. Note the plural: each
case may be followed by zero or more statements (not just one statement).
• Execution continues until either a break statement is encountered or all intervening
statements until the end of the switch statement are executed.
• The final default case is optional and is exercised if none of the earlier cases provide a
match.

OF-FTI-ALL -18 1 15
Department of Information AUGUST 5
• For example, suppose we have parsed a binary arithmetic operation into its three
components and stored these in variables operator, operand1, and operand2. The
following switch statement performs the operation and stores the result in result.
switch (operator) {
case '+’:
result = operand1 + operand2;
break;
case '-’:
result = operand1 - operand2;
break;
case '*’:
result = operand1 * operand2;
break;
case '/’:
result = operand1 / operand2;
break;
default:
cout << "unknown operator: " << ch << '\n';
break;
}

• As illustrated by this example, it is usually necessary to include a break statement at the end of
each case.

• The break terminates the switch statement by jumping to the very end of it.
OF-FTI-ALL -18 1 16
Department of Information AUGUST 6
Repetition Statements
• Control a block of code to be executed repeatedly for
a fixed number of times or until a certain condition
fails.
• There are three C++ repetition statements:
1) The For Statement or loop
2) The While statement or loop
3) The do…while statement or loop

1
Department of Information AUGUST 7
The for statement / loop
• The “for” statement is used to repeatedly execute a block of
instructions until a specific condition fails.
• The General Syntax is:
for(expression1 ; expression2 ; expression3)
statements;
Expression1: is one or more statements that will be executed only once
and before the looping starts.
Expression2: is the part that decides whether to proceed with executing
the instructions in the loop or to stop.
Expression3: is one or more statements that will be executed after each
iteration.
• The general format can be expressed as follows for the sake of
clarity:
for(initialization ; condition ; increase/decrease)
statement;
1
Department of Information AUGUST 8
E.g. : What is the output of the following code:
int main()
{

for(int i=10;i>0;i--)
{

cout<<n<<“,”;
}

cout<< “HELLO!”;
return 0;
}

OF-FTI-ALL -18 1 19
Department of Information AUGUST 9
The while statement
• The while statement provides a way of repeating a statement or a block as long
as a condition holds / is true.
The general form of the while loop is:
while(expression)
statements;
• First expression (called the loop condition) is evaluated. If the outcome is non zero
then statement (called the loop body) is executed and the whole process is repeated.
Otherwise, the loop is terminated.
#include <iostream>
using namespace std;
int main ()
{
int count = 1;
while (count < 11)
{
cout<<"Count is: " <<count<<"\n";
count++;
}
}
2
Department of Information AUGUST 0
Suppose that we wish to calculate the sum of all numbers from 1 to some integer value n. this can be expressed as:

Eg1// adds the numbers between 0 and any given number n i=1;
sum = 0;
while(i<= n)
sum += i++;
Eg2:
//adds the numbers between 0 and 100 number=1;
sum=0;
while(number <= 100)
{

sum += number;
number++;
}

E.g.3:

// adds the numbers between 0 and any given number n i=1;


sum = 0; while(i<= n)
sum += i++; Eg2:
//adds the numbers between 0 and 100 number=1;
sum=0;
while(number <= 100)
{

sum += number; number++;


}

OF-FTI-ALL -18 2 21
Department of Information AUGUST 1
Do…while loop.
• The do statement is similar to the while statement, except that
its body is executed first and then the loop condition is
examined.
• In do…while loop, we are sure that the body of the loop will be
executed at lease once. Then the condition will be tested.
The general form is:
do
{
statement;
}
while(expression);

• First statement is executed and then expression is evaluated.


If the outcome of the expression is nonzero, then the whole
process is repeated. Otherwise the loop is terminated.
2
Department of Information AUGUST 2
#include <iostream>
using namespace std;
int main ()
{
int count = 1;
do {
cout<<"Count is: " << count<<"\n";
count++;
} while (count < 11);
}

2
Department of Information AUGUST 3
The break statement
• A break statement may appear inside a loop (while, do, or for) or a
switch statement. It causes a jump out of these constructs, and
hence terminates them.
• Like the continue statement, a break statement only applies to
the “loop” or “switch” immediately enclosing it. It is an error to
use the break statement outside a loop or a switch statement.
Eg:
for(n=10;n>0;n--)
{
cout<<n<< “,”; if(n = = 3)
{
cout<< “count down aborted!!”; break;
}
}

OF-FTI-ALL -18 2 24
Department of Information AUGUST 4

You might also like