Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals

Similar presentations


Presentation on theme: "Programming Fundamentals"— Presentation transcript:

1 Programming Fundamentals
Lecture 5 Ms. Farah Younas

2 Agenda of Lecture Introduction to structured programming Algorithms
Pseudocode Control Structures The if Selection Structure The if/Else Selection Structure

3 Structured Programming
Elements of Structured Programming Control Statements Subroutines Blocks

4 Structured Programming
Control Statements Sequence: ordered statements executed in a sequence. Selection/Conditional: one or a number of statements is executed depending on the state of the program. This is usually expressed with keywords such as  If , then , else, endif. Iteration: a statement or block is executed until the program reaches a certain state, or operations have been applied to every element of a collection. While , repeat, dowhile, for Recursion: a statement is executed by repeatedly calling itself until termination conditions are met. While similar in practice to iterative loops, recursive loops may be more computationally efficient, and are implemented differently.

5 Introduction to Algorithms
Algorithm is a step-by –step procedure developed to solve a problem before writing actual program Before writing a program: Have a thorough understanding of the problem Carefully plan an approach for solving it While writing a program: Know what “building blocks” are available Use good programming principles

6 Algorithms Algorithm is a complete procedure or plan that describes the logic of program. Computing problems All can be solved by executing a series of actions in a specific order Algorithm: procedure in terms of Actions to be executed The order in which these actions are to be executed Program control Specify order in which statements are to executed

7 Pseudocode Pseudocode
Artificial, informal language that helps us develop algorithms Similar to everyday English Not actually executed on computers Helps us “think out” a program before writing it Easy to convert into a corresponding C++ program Consists only of executable statements

8 Control Structures Sequential execution Transfer of control
Statements executed one after the other in the order written Transfer of control When the next statement executed is not the next one in sequence Overuse of goto statements led to many problems All programs written in terms of 3 control structures Sequence structures: Built into C. Programs executed sequentially by default Selection structures: C has three types: if, if/else, and switch Repetition structures: C has three types: while, do/while and for

9 Control Structures Flowchart
Graphical representation of an algorithm Drawn using certain special-purpose symbols connected by arrows called flow lines Rectangle symbol (action symbol): Indicates any type of action Oval symbol: Indicates the beginning or end of a program or a section of code Single-entry/single-exit control structures Connect exit point of one control structure to entry point of the next (control-structure stacking) Makes programs easy to build

10 Flow Chart The written form of algorithm (not in computer language) is called as pseudocode. Pseudocode expresses the basic logic structure of an algorithm in a systematic and clear way Flow chart symbols Flow line: A line with an arrow head represents the flow of control. Between various symbols of flow chart Terminal symbols: Input/output Symbols: Start Stop

11 Flow Chart Flow chart symbols Processing Symbol: Decision Symbol:

12 Flow Chart Symbols

13 Flow Chart Symbols

14 Flow Chart Draw a flow chart for adding two numbers

15 Flow Chart

16 Decision Making in C++ Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C++ handles decision-making by supporting the following statements, If statement switch statement conditional operator statement goto statement

17 Decision Making with If statement
The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are: Simple if statement If…else statement Nested if…else statement else if statement

18 Simple If statement The general form of a simple if statement is,
If (expression) { Statement-inside; Statement-outside; If expression is true, then statement-inside is executed, other wise only statement- outside will be executed.

19 The if Selection Structure
Used to choose among alternative courses of action Pseudocode: If student’s grade is greater than or equal to 60 Print “Passed” If condition true Print statement executed and program goes on to next statement If false, print statement is ignored and the program goes onto the next statement Indenting makes programs easier to read C++ ignores whitespace characters

20 The if Selection Structure
Pseudocode statement in C++: If grade >= 60 Print “Passed” if ( grade >= 60 ) cout<<"Passed\n"; C++ code corresponds closely to the pseudocode Diamond symbol (decision symbol) Indicates decision is to be made Contains an expression that can be true or false Test the condition, follow appropriate path

21 The if Selection Structure
if structure is a single-entry/single-exit structure true false grade >= 60 print “Passed” A decision can be made on any expression. zero - false nonzero – true Example: 3 - 4 is true

22 Simple If statement - Example
#include<iostream.h> int main() { int x,y; x=15; y=13; If(x > y) cout<<“x is greater than y”; } return 0;

23 The if/else Selection Structure
The general form of a simple if…else statement is,

24 The if/else Selection Structure
The general form of a simple if statement is, If (expression) { Statement-block1; else } If expression is true, then statement-block1 is executed, other wise only statement- block2 will be executed.

25 The if/else Selection -Example
#include<iostream.h> int main() { int x,y; x=15; y=13; If(x > y){ cout<<“x is greater than y”; } else{ cout<<“y is greater than x”} return 0;

26 Questions??


Download ppt "Programming Fundamentals"

Similar presentations


Ads by Google