Lecture 2: Logical Problems with Choices

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 3 - Structured Program Development
Lec3: 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.
CMT Programming Software Applications Week 3 Dr. Xiaohong Gao TP Room B107 Control Structures.
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.
 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
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
© 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.
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.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
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 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.
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.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
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
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
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.
Structured Program Development in C
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)
Programming Fundamentals
Lecturer CS & IT Department UOS MBDIN
Programming Fundamentals
Program Control using Java - Theory
How to develop a program?
Structured Program
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Structured Program Development in C
Week 3 – Program Control Structure
Structured Program Development in C
Structured Program Development in C
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

Lecture 2: Logical Problems with Choices Sumber dari : user.engineering.uiowa.edu/~xwu3/epsii/Lecture02/lecture2.ppt

Problem Solving 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 Using good programming principles

Algorithms Computing problems All can be solved by executing a series of actions in a specific order Algorithms: An algorithm is a clearly specified set of simple instructions to be followed to solve a problem Actions to be executed The order in which these actions are to be executed Program control Specify order in which statements are to be executed

Control Structures Sequential execution Transfer of control Normally, statements are 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 C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default Selection structures Three types: if, if … else, and switch Repetition structures Three types: while, do … while and for

Pseudocode Artificial, informal language that helps develop algorithms Similar to everyday English Not actually executed on computers Help “think out” a program before writing it Easy to convert into a corresponding C program Consists only executable statement Definitions are not executable statements

Flowchart Graphical representation of an algorithm Drawn using certain special-purpose symbols connected by arrows called flowlines Special-purpose symbols Rectangle (action) symbol: any type of action. Oval symbol: the beginning or end of a program Small circle (connector) symbol: the entry or exit of a portion of an algorithm. Diamond symbol: indicate that a decision is to be made. Single-entry/single-exit Flowcharting C’s sequence stucture

if Selection Statement Selection structure Used to choose among alternative courses of action Example - Pseudocode: if student’s grade is no less than 60 print “Passed” If condition true Print statement executed and program goes on to next statement. If condition false Print statement is ignored and the program goes onto the next statement condition

if Selection Statement Pseudocode statement in C if ( grade >= 60) printf( “Passed\n” ); C code corresponds closely to the pseudocode Flow chart for the if selection statement 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 condition

if … else Selection Statement Specifies an action to be performed both when the condition is true and when it is false if: Only performs an action if the condition is true. Problem: Pseudocode: Read in 2 numbers and print in non-decreasing order. Read in two numbers, num1 and num2. If num1 is no larger than num2 Print “num1 num2” else Print “num2 num1”

if … else Selection Statement Flowchart begin Flowcharting the double-selection if...else statement Read in two numbers: num1 and num2 num1 <= num2 false true Print “num2 num1” Print “num1 num2” end

if … else Selection Statement C code

Nested if … else Statements Test for multiple cases by placing if…else selection statements inside if…else selection statement Once condition is met, rest of statements skipped Example - Pseudocode If student’s grade is greater than or equal to 90 Print “A” else If student’s grade is greater than or equal to 80 Print “B” else If student’s grade is greater than or equal to 70 Print “C” else If student’s grade is greater than or equal to 60 Print “D” else Print “F”

Nested if … else Statements Flowchart score>=90 true Print “A” false score>=80 Print “B” true false score>=70 true Print “C” false score>=60 true Print “D” false Print “F”

Nested if … else Statements /* Convert a student's score to grade */ #include <stdio.h> /* function main begins program execution */ int main( void ) { int score; / * score of the student */ printf( "Enter the score\n" ); /* prompt */ scanf( "%d", &score ); /* read an integer */ if ( score >= 90 ) printf( "The grade is 'A'.\n" ); else if ( score >= 80 ) printf( "The grade is 'B'.\n" ); if ( score >= 70 ) printf( "The grade is 'C'.\n" ); if ( score >= 60 ) printf( "The grade is 'D'.\n" ); printf( "The grade is 'F'.\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */ C code

Compound Statement Set of statements within a pair of braces Example: if ( grade >= 60 ) printf( "Passed.\n" ); else { printf( "Failed.\n" ); printf( "You must take this course again.\n" ); } Without the braces, the statement printf( "You must take this course again.\n" ); would be executed automatically. A compound statement can be placed anywhere in a program that a single statement can be placed. Common Programming Error: Forgetting one or both of the braces that delimit a compound statement.

In-Class Programming Exercise Write a program that completes the following: Read in three integers and determine the largest. You should only use if/else statements for the logic in your code. Submit your maxnum.c file to the dropbox called Maximum Number and complete Program Quiz 2.