Programming Fundamentals

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 3 - Structured Program Development
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
true (any other value but zero) false (zero) expression Statement 2
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Structured Program Development in C
Lecture 3 Structured Program Development in C
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
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. 1 Structural Program Development: If, If-Else Outline.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
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.
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.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
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.
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.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
Control Statements: Part1  if, if…else, switch 1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
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
- Standard C Statements
JavaScript: Control Statements I
Chapter 2.1 Control Structures (Selection)
TMF1414 Introduction to Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
Structured Program
Chapter 4 - Program Control
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Chapter 4: Control Structures I (Selection)
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
Branching statements Kingdom of Saudi Arabia
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Lecture 9: Implementing Complex Logic
Structural Program Development: If, If-Else
Presentation transcript:

Programming Fundamentals Lecture 6 Ms. Farah Younas

Agenda of Lecture The if Selection The if/Else Selection Nested if/else Selection

Relational Operators

Relational Operators

Relational Operators

Confusing Equality (==) and Assignment (=) Operators Dangerous error Does not ordinarily cause syntax errors Any expression that produces a value can be used in control structures Nonzero values are true, zero values are false Example using ==: if ( payCode == 4 ) cout<<"You get a bonus!\n"; Checks paycode, if it is 4 then a bonus is awarded Example, replacing == with =: if ( payCode = 4 ) cout<<"You get a bonus!\n" ; This sets paycode to 4 4 is nonzero, so expression is true, and bonus awarded no matter what the paycode was Logic error, not a syntax error

The Logical Operators && ( logical AND ) || ( logical OR ) Returns true if both conditions are true || ( logical OR ) Returns true if either of its conditions are true ! ( logical NOT, logical negation ) Reverses the truth/falsity of its condition Unary operator, has one operand Useful as conditions in loops Expression Result true && false false true || false true !false true

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.

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

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

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 Test for multiple cases by placing if/else selection structures inside if/else selection structures

The if/else Selection Structure Pseudocode: If student’s grade is greater than or equal to 60 Print “Passed” Else Print “Failed” C++ code: if ( grade >= 60 ) cout <<"Passed\n"; else cout << "Failed\n"; Ternary conditional operator (?:) cout << ( grade >= 60 ? "Passed" : "Failed" ); grade >= 60 ? cout << "Passed" : cout << "Failed";

The if/else Selection Structure The general form of a simple if/else statement is, If (expression) { Statement-block1; } else Statement-block2; 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;}

Nested if…else Selection - Statement(general-form) if (expression) { if (expression1) Statement-block1; } else Statement-block2; Statement-block3; If ‘expression’ is false the ‘statement- block3’ will be executed, otherwise it continues to perform the test for ‘expression 1’ . If the ‘expression 1’ is true the ‘statement block1’ is executed otherwise ‘statement-block2’ is executed

Else…if Selection The expression is tested from the top (of the ladder) downwards. As soon as the true condition is found, the statement associated with it is executed.

Else…if Selection

Dangling - else Problem The C++ compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({ and }). This behavior can lead to what’s referred to as the dangling-else problem. (int x = 35; int y = 3; )

Dangling - else Problem

Blocks The if selection statement expects only one statement in its body. Similarly, the if and else parts of an if…else statement each expect only one body statement. To include several statements in the body of an if or in either part of an if…else, enclose the statements in braces ({ and }). A set of statements contained within a pair of braces is called a compound statement or a block

Questions??