CSC113: Computer Programming (Theory = 03, Lab = 01)

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
Chapter 3 - Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
 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
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
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.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
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.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
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.
Branching statements.
Chapter 4 – C Program Control
The if…else Selection Statement
Algorithm: procedure in terms of
Control Structures Sequential execution Transfer of control
- Standard C Statements
Chapter 2.1 Control Structures (Selection)
Programming Fundamentals
Programming Fundamentals
Chapter 2 - Control Structures
Lecture 2: Logical Problems with Choices
Program Control using Java - Theory
Structured Program
Chapter 3 - Structured Program Development
-2- Introduction to C Programming
3 Control Statements:.
Chapter 3 - Structured Program Development
Chapter 2 - Control Structures
Capitolo 2 - Control Structures
Chapter 2 - Control Structures
2.6 The if/else Selection Structure
Programs written in C and C++ can run on many different computers
-2- Introduction to C Programming
Control Statements Paritosh Srivastava.
Dale Roberts, Lecturer IUPUI
Chapter 2 - Control Structures
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

CSC113: Computer Programming (Theory = 03, Lab = 01) Momina Moetesum Computer Science Department Bahria University, Islamabad

Control structures – I if / if-else statements Week # 3

Decision Making: Equality and Relational Operators Lower precedence than arithmetic operators

2 // Using if statements, relational 1 // Fig. 1.14: fig01_14.cpp 2 // Using if statements, relational 3 // operators, and equality operators 4 #include <iostream> 5 6 using std::cout; // program uses cout 7 using std::cin; // program uses cin 8 using std::endl; // program uses endl 9 10 int main() 11 { 12 int num1, num2; 13 14 cout << "Enter two integers, and I will tell you\n" 15 << "the relationships they satisfy: "; 16 cin >> num1 >> num2; // read two integers 17 18 if ( num1 == num2 ) 19 cout << num1 << " is equal to " << num2 << endl; 20 21 if ( num1 != num2 ) 22 cout << num1 << " is not equal to " << num2 << endl; 23 24 if ( num1 < num2 ) 25 cout << num1 << " is less than " << num2 << endl; 26 27 if ( num1 > num2 ) 28 cout << num1 << " is greater than " << num2 << endl; 29 30 if ( num1 <= num2 ) 31 cout << num1 << " is less than or equal to " 32 << num2 << endl; 33 1. Load <iostream> 2. main 2.1 Initialize num1 and num2 2.1.1 Input data 2.2 if statements Notice the using statements. Enter two integers, and I will tell you the relationships they satisfy: 3 7 The if statements test the truth of the condition. If it is true, body of if statement is executed. If not, body is skipped. To include multiple statements in a body, delineate them with braces {}. 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7

2.3 exit (return 0) Program Output 34 if ( num1 >= num2 ) 35 cout << num1 << " is greater than or equal to " 36 << num2 << endl; 37 38 return 0; // indicate that program ended successfully 39 } 2.3 exit (return 0) Program Output Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7

Precedence and associativity of operrators Operators Associativity Type ( ) Left to Right Parentheses * / % Multiplicative + - Additive << >> Stream insertion / extraction < <= > >= Relational == != Equality = Right to Left Assignment

Common programming errors Syntax error will occur if there is a space between ==,<=,>= or != Another syntax error is to reverse their order Confusing equality operator (==) with assignment operator (=) causes logical error

Program control Specifies the order in which statements are to executed Sequential execution 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

Structured-Programming Programs are easier to understand, test, debug and, modify. Rules for structured programming Only single-entry/single-exit control structures are used (Bohm and Jacopini) Rules: 1) Begin with the “simplest flowchart”. 2) Any rectangle (action) can be replaced by two rectangles (actions) in sequence. 3) Any rectangle (action) can be replaced by any control structure (sequence, if, if/else, switch, while, do/while or for). 4) Rules 2 and 3 can be applied in any order and multiple times.    

Control Structures All programs can be broken down into 3 control structures Sequence structure Built into C++. Programs executed sequentially by default. Selection structures C++ has three types - if, if/else, and switch Any selection can be rewritten as an if statement Repetition structures C++ has three types - while, do/while, and for Any repetition structure can be rewritten as a while statement

Flowchart Flowchart single-entry/single-exit control structures Graphical representation of an algorithm Drawn using certain special-purpose symbols connected by arrows called flowlines. Rectangle symbol (action symbol) Indicates any type of action. Oval symbol indicates beginning or end of a program, or a section of code (circles). 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.

Structured-Programming Representation of Rule 3 (replacing any rectangle with a control structure) Rule 3

The if Selection Structure Single selection structure used to choose among alternative courses of action Pseudocode example: If student’s grade is greater than or equal to 60 Print “Passed” If the condition is true print statement executed and program goes on to next statement If the condition is 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 Translation of pseudocode statement into C++: if ( grade >= 60 ) cout << "Passed"; 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 if structure is a single-entry/single-exit structure  

The if Selection Structure Flowchart of pseudocode statement true false grade >= 60 print “Passed” A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 is true

Self Practice Test if the value of the variable count is greater than 10. If it is, print “Count is greater than 10”. Test if the variable number is not equal to 7, print “The variable number is not equal to 7”.

The if/else Selection Structure Double Selection Structure if Only performs an action if the condition is true if/else A different action is performed when condition is true and when condition is false Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";  

The if/else Selection Structure true false print “Failed” print “Passed” grade >= 60

Ternary conditional operator (?:) Takes three arguments condition, value if true, value if false Our pseudocode could be written: cout << ( grade >= 60 ? “Passed” : “Failed” );

Self practice Pseudocode Write C++ statement using: If/else statement if person’s age is less than or equal to 18 print “Child” else print “Adult” Write C++ statement using: If/else statement Ternary conditional operator

The if/else Selection Structure Nested if/else structures Test for multiple cases by placing if/else selection structures inside if/else selection structures. 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” Once a condition is met, the rest of the statements are skipped

The if/else Selection Structure Compound statement: Set of statements within a pair of braces Example: if ( grade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } Without the braces, cout << "You must take this course again.\n"; would be automatically executed Block Compound statements with declarations

Errors Syntax errors Logic errors Errors caught by compiler Errors which have their effect at execution time Non-fatal logic errors program runs, but has incorrect output Fatal logic errors program exits prematurely

Self Practice -Identify and correct errors 1) if (gender == 1) cout<<“woman”<<endl; else; cout<<“man”<<endl; 2) if (c< 7); cout<< “c is less than 7 \n”; 3) if (c=> 7) cout<< “c is equal to or greater than 7 \n”;

Self Practice Write a C++ program that takes two positive integers from the user and then prints the larger number followed by the words “is larger”. If the numbers are equal, prints the message “These numbers are equal”.