Programming Fundamentals

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
UNIT 2. Introduction to Computer Programming
 Control structures  Algorithm & flowchart  If statements  While statements.
ITEC113 Algorithms and Programming Techniques
Chapter 3 - Structured Program Development
Introduction to Computers and Programming Lecture 5 New York University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
CMT Programming Software Applications Week 3 Dr. Xiaohong Gao TP Room B107 Control Structures.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Structured Program Development in C
Lecture 3 Structured Program Development in C
The University of Texas – Pan American
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Computer Organization Six logical units in every.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
 Problem Analysis  Coding  Debugging  Testing.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
The if…else Selection Statement
Algorithm: procedure in terms of
- Standard C Statements
Chapter 2.1 Control Structures (Selection)
Chapter 8 - JavaScript: Control Statements I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Introduction To Flowcharting
Programming Fundamentals
Ch 7: JavaScript Control Statements I.
Lecturer CS & IT Department UOS MBDIN
ALGORITHMS AND FLOWCHARTS
Lecture 2: Logical Problems with Choices
Program Control using Java - Theory
How to develop a program?
MSIS 655 Advanced Business Applications Programming
Structured Program
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
ALGORITHMS AND FLOWCHARTS
3 Control Statements:.
Chapter 3 - Structured Program Development
Introduction to Problem Solving and Control Statements
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

Programming Fundamentals Lecture 5 Ms. Farah Younas

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

Structured Programming Elements of Structured Programming Control Statements Subroutines Blocks

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.

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

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

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

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

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

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

Flow Chart Flow chart symbols Processing Symbol: Decision Symbol:

Flow Chart Symbols

Flow Chart Symbols

Flow Chart Draw a flow chart for adding two numbers

Flow Chart

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

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

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.

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

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

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

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;

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

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.

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;

Questions??