0% found this document useful (0 votes)
4 views

Chapter 3 Computer Programmingodp

Chapter Three of the document covers control statements in computer programming, including conditional structures like 'if' and 'else', loops, and the 'switch' statement. It explains how these structures allow for decision-making and repetition in code execution. Additionally, it discusses control flow mechanisms such as 'break', 'continue', and 'goto' instructions.

Uploaded by

birhanua056
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 3 Computer Programmingodp

Chapter Three of the document covers control statements in computer programming, including conditional structures like 'if' and 'else', loops, and the 'switch' statement. It explains how these structures allow for decision-making and repetition in code execution. Additionally, it discusses control flow mechanisms such as 'break', 'continue', and 'goto' instructions.

Uploaded by

birhanua056
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Computer Programming

[ECEg-1052]

Chapter Three:
Control Statements

Prepared by Electronics and Computer Engineering Stream.


Outline
 Introduction
 Conditional Structure: If and else
 Repetitive Structures or loops
 Bifurcation of control and jumps.
 The selective Structure: switch
4.1. Introduction

Control statements allow decision making within programs.

Control statements come in several different forms:

Conditional statements: to optionally execute C++ statements

Loops: execute statements repeatedly until the exit condition is
satisfied.

Branch: jump to a certain statement and continue execution.

3
4.2. Conditional Structure: if and else
 It is used to execute an instruction or block of instructions only if a
condition is fulfilled.
 if can be used in different forms depending upon nature of conditional test.
1. if
2. if…else
3. Nested – if

4
Simple if statement
General form: Entry

if (test expression)
Test expression False
{ ?

statement-block;
True
} Statement-block
statement-x;
Statement-x

General form Flowchart of simple if statement

5
Cont’d...
 The ‘statement-block’ may be a single statement or a group of statements.
 If the test expression is true, the statement-block is executed.
 If it is false, statement-block is ignored (not executed) and the program
continues on the next instruction after the conditional structure.

6
Cont’d...
\* To display number is positive.*/ Sample output 1:
#include <iostream> Enter a number: 3

using namespace std; number is positive


Sample Output 2:
int main(){
Enter a number:-3
int num;
cout<<"Enter a number:";
cin>>num;
if (num>0)
cout<<" number is positive ";
return 0;
} 7
Cont’d...

For a block of instructions, we use curly brackets { }:
Example:
if (x == 100){
cout << "x is ";
cout << x;
}

But for a single, we can ignore {}.
Example:
if (x == 100)
cout << "x is 100";
8
The if – else statement
Form:
if (condition)
Entry
{
statement block 1; True False
Condition
} ?

else
{ statement block statement block
statement block 2; 1 2

}
statement-x;

statement-x

9
Cont’d...
For example:
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";

10
Nested if statement

When a series of decisions are involved, use more than one if…else
statement.
Example: if (x>0)
cout<<"The given number is +ve"<<endl;
else if (x<0)
cout<<"The given number is negative"<<endl;
else
cout<<"The given number is zero";
Exercise
Write a program that find whether the given number is even or odd. 11
Cont’d...
Solution
// To find whether given number is even or odd.
#include<iostream>
using namespace std;
int main(){
int no;
cout<<"enter a number";
cin>>no;
if ( no %2 == 0 )
cout<<no<<"is an even number";
else
cout<<no<<"is an odd number";
}
12
4.2 Repetitive Structures or loops
 Loops have as objective to repeat a statement a certain number of
times while a condition is fulfilled.

The while loop

The do-while loop

The for loop

13
The while loop
Format:
while(expression)
statements;

While
(expression) statements
? True

False

Out of loop

14
Cont’d...
Example: i=2;
/* Find the sum of even while (i <= n){
numbers between 1 to n */ sum = sum + i;
i = i + 2;
#include <iostream>
}
using namespace std;
cout<<"sum= "<<sum;
int main(){ }
int i, n, sum=0;
cout<<"enter the end number:";
cin>>n;
15
The do-while loop
do
Format:
do
statements; statements
while (condition);

Condition
?
True

False

Out of loop
16
Cont’d...

Example
sum = 0;
i = 1;
do{
sum += i;
i += 2;
} while (i <= 100);
cout<<“Sum of odd integers up to 100 is”<<sum;
Output:
Sum of odd integers up to 100 is 2500
17
The for loop
 Its format is:
for (initialization; condition; increase/decrease)
statement;
 Following steps are involved in the code:

Initialization condition checking body of the loop increment/ decrement


again condition checking

18
Cont’d...

initialization

False Condition
?

Out of loop
True

statement

increase/decrease

19
Cont’d...
Example 1:
sum = 0;
for (i=0 ; i <= 100 ; i++)
sum += i;
Example 2: Write a program to list a number and its square in two columns
for integers 1 up to 10.

20
Cont’d...

#include<iostream>
using namespace std;
int main(){
for(int i=1;i<=10;i++) {
cout<<"The number is:"<<i;
int square=i*i;
cout<<" and its square is:"<<square;
cout<<endl;
}
} 21
4.3 Bifurcation of control and jumps.
The break instruction
 The format of the break statement is simply
break;
 Causes an immediate exit even if the condition for its end is not fulfilled.
 It can be used to end an infinite loop, or to force it to end before its
natural end.

22
Cont’d...

Examples
i=1;
while (1){
cout<<i<<" ";
if (i==10)
break;
i=i+1;
}
Output: 1 2 3 4 5 6 7 8 9 10
23
Cont’d...

// break loop example


#include <iostream>
Output
using namespace std; 10, 9, 8, 7, 6, 5, 4, 3, countdown
int main (){ aborted!
int n;
for (n=10; n>0; n--) {
cout << n << ", ";
if (n==3){
cout << "countdown aborted!";
break;
}
}
return 0;
} 24
The continue instruction
Format: continue;
 The continue statement tells the compiler, “Skip the following statements
and continue with the next iteration’’.
 In while and do loops, continue causes the control to go directly to the
test-condition and then to continue the iteration process.
 In the case of for loop, the increment section of the loop is executed before
the test-condition is evaluated.

25
Cont’d...
while (---------)
{
---------
---------
if (condition)
continue;
---------
---------
}
--------
26
Cont’d...
// to add only positive numbers
#include<iostream>
using namespace std;
int main(){
int i, n, sum=0;
cout<<"enter any 10 numbers\n";
for(i=1;i<=10;i++) {
cin>>n;
if (n==7)
continue;
sum += n;
}
cout << " sum of +ve numbers = "<<sum;
}
27
Cont’d...
// example Output:
#include <iostream> 10, 9, 8, 7, 6, 4, 3, 2, 1, End!
using namespace std;
int main (){
for (int n=10; n>0; n--) {
if (n==5)
continue;
cout << n << ", ";
}
cout << "End!";
return 0;
} 28
The goto instruction
 Allows making an absolute jump to another point in the program.
 The destination point is identified by a label, which is then used as an
argument for the goto instruction.
 A label is made of a valid identifier followed by a colon (:).

29
Cont’d...
// goto loop example Output:
#include <iostream> 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, End!
using namespace std;
int main (){
int n=10;
loop:
cout << n << ", ";
n--;
if (n>0)
goto loop;
cout << "End!";
return 0;
}
30
The exit function
 A function defined in cstdlib(stdlib.h) library.
 The purpose of exit is to terminate the running program with an specific
exit code.
 Its prototype is:
void exit (int exit code);
 if exit code= 0 means that the program finished normally.

31
Cont’d...
// exit function example
#include <iostream>
#include <cstdlib>
using namespace std;
int main (){
int n=10;
while(n>0) {
cout << n << ", ";
n--;
if (n==6)
exit(0);
}
cout <<"End!";
return 0; Output:
} 10, 9, 8, 7, 32
The selective Structure: switch
General form:  Inclusion of break at the end of each
switch (expression) block is necessary because if, for
{ example, we did not include it after
case constant1: block of instructions 1 the program
block of instructions 1; would not jump to the end of the
break; switch selective block (}) and
case constant2: continue to execute the rest of the
block of instructions 2; blocks of instructions until the first
break; appearance of break.

default:
default block of instructions;
} 33
Cont’d...
Example 1: Example 2:
switch (x) { switch (x) {
case 1: case 1:
cout << "x is 1"; case 2:
break;
case 3:
case 2:
cout << "x is 1, 2 or 3";
cout << "x is 2";
break; break;
default: default:
cout << "value of x unknown"; cout << "x is not 1, 2 nor 3";
} } 34
Thank You !

You might also like