Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Modular

P r ogramming/
Deci sion Str uctur es

if C ondition
What is modular programming?

- Is the proc ess of subdividing a


computer program into separate sub-
programs. A module is a separate 2

software c omponent. It c an often be


used in a variety of applications and
functions with other components of the
system.
C oncept of M odular izations

One of the most important concepts of


programming is the ability to group some lines of
3
code into a unit that can be included in our program.
The original wording for this was a sub-program.
Other names include: macro, sub-routine, procedure,
module and function.
2 Function of Modular Programming

Program Control – func tions used to simply sub-divide


and control the program. These functions are unique to
the program being written. Other programs may use 4

similar functions, maybe even functions with the same


name, but the c ontent of the func tions are almost always
very different.
2 F u n c t i o n of M o d u l a r P r o g r a m m i n g

Specific Task – functions designed to be used with several


programs. These func tions perform a spec ific task and thus
are usable in many different programs bec ause the other 5
programs also need to do the spec ific task. Spec ific task
func tions are sometimes referred to as building bloc ks.
Because they are already coded and tested, we can use them
with c onfidenc e to more effic iently write a large program.
The main program must establish the existence of functions
used in that program. Depending on the programming language,
there is a formal way to:

define a function (its definition or the code it


will execute)
call a function
declare a function (a prototype is a
declaration to a compiler)
Program Control functions normally do not
c ommunic ate information to eac h other but use a
c ommon area for variable storage. Spec ific Task
func tions are c onstruc ted so that data c an be
c ommunic ated between the c alling program piec e
(whic h is usually another func tion) and the func tion
being c alled. This ability to c ommunic ate data is
what allows us to build a spec ific task func tion that
may be used in many programs.
PROGRAM LAYOUT
Most programs have several items before the functions,
including:
1. Documentation – Most programs have a comment area at
the start of the program with a variety of comments
pertinent to the program.
2. Include or import statements used to access standard
library functions.
3. Language-specific code such as namespace references or
function prototypes.
4. Global or module-level constants and variables, when
required.
IF C O N D I T I O N
In computer programming, we use the if...else statement to
run one block of code under certain conditions and another
block of code under different conditions.
For example, assigning grades (A, B, C) based on marks
obtained by a student.
if the percentage is above 90, assign grade A
if the percentage is above 75, assign grade B
if the percentage is above 65, assign grade C
There are three forms of
if...else statements in C++.
1. if statement
2. if...else statement
3. if...else if...else statement
C++ if Statement
The syntax of the if statement is:
if (condition) {
/ / body of if statement
}
The if statement evaluates the condition inside the parentheses ( ).
If the condition evaluates to true, the code inside the body of if is
executed.
If the condition evaluates to false, the code inside the body of if is
skipped.
Note: The code inside { } is the body of the if statement.
/ / Program to print positive number entered by the user
/ / If the user enters a negative number, it is skipped

#include <iostream>
using namespace std;

int main() {

int number;

cout <<"Enter an integer: ";


cin >>number;

/ / checks if the number is positive


if (number >0) {
cout <<"You entered a positive integer: " <<number <<endl;
}
cout <<"This statement is always executed.";

return 0;
}
Output 1

Enter an integer:5
You entered a positive number:5
This statement is always executed.
When the user enters 5, the condition number >0 is evaluated to true
and the statement inside the body of if is executed
Output 2

Enter a number:-5
This statement is always executed.
When the user enters -5, the condition number >0 is evaluated to false
and the statement inside the body of if is not executed.
Thank You
!

You might also like