Control structures Algorithm Development Conditional Expressions Selection Statements 1.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

If Statements & Relational Operators Programming.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
C++ for Engineers and Scientists Third Edition
Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 5 Structured Program.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection Structures (if & switch statements) (CS1123)
Chapter 4 Selection Structures: Making Decisions.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
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.
Previously Repetition Structures While, Do-While, For.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CPS120: Introduction to Computer Science Decision Making in Programs.
1 CS 101 Fall 2001 Lecture 3. 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y,
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- 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.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
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 3 Selection Statements
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Engineering Problem Solving with C++, Etter/Ingber
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Lecture 2: Logical Problems with Choices
Compound Assignment Operators in C++
Control Structures: Selection Statement
3 Control Statements:.
Chapter#3 Structured Program Development in C++
Control Statements Paritosh Srivastava.
Control Structures: Selection Statement
Branching statements Kingdom of Saudi Arabia
Structural Program Development: If, If-Else
Presentation transcript:

Control structures Algorithm Development Conditional Expressions Selection Statements 1

Algorithm Algorithm: a sequence of steps/actions needed to solve a problem. It is a set of sequential steps/actions that a C++ program should perform in order to solve the problem. These steps will eventually correspond to your C++ statements. –Top-down design of your Algorithm: – Start with a “big picture”, i.e., broad description of a problem solution in sequential step (i.e. order is important) –Then, refine the sequential steps until they are simple enough to translate to C++ language statements. The solution steps of your Top-Down design define your algorithm. 2

Algorithm Algorithm corresponds to Step 4 (Solution development) of the 5-Step Problem solving methodology How to describe your TOP-Down design (or algorithm)? Two techniques: –Pseudo Code: describes algorithm steps using English-like statements. –Flowchart: describes algorithm steps using a diagram/graphical notation

Top-Down Design – Example Problem : Increase salaries lower then 500 by 30 Omani Rials and display all salaries! 4 True False salary<500 Start main read salary salary=salary+30 Stop main print salary Program start/end symbol Input/output symbol Comparison symbol Computation symbol Program start/end symbol Input/output symbol Or NO Or Yes Pseudo CodeFlow Chart Broad Pseudo Code: Read salary value Increase salary values <500 by 30 Print salary Step 2 Needs Refinement: Refined Pseudo Code: Read salary value If ( salary <500) salary=salary+30 Print salary

Structured Programming – Sequence: Statements executed in order sequentially, one after another. This is the default structure in C++ programs –Selection: if, if-else, if-else if, switch One set of statements is executed if a given condition is true, a different set of statements is executed if the condition is false. –Repetition: while, do/while, for A set of statements is executed repeatedly as long as a given condition is true. 5

6 Structured Programming Sequence Selection Repetition ? truefalse ? true false ? => conditional expression Or Comparison

Conditional Expressions A conditional expression is a Boolean expression that evaluates to true or false. –Example: if (salary <500) salary=salary+30; Relational operators and logical operators are used to build conditional expressions. Selection structures and repetition structures use conditional expressions. 7

8 Relational Operators ==equal to !=not equal to <less than >greater than <=less than or equal >=greater than or equal

9 Relational ExpressionValue 1 == 6false 5 != 9true 5<9true 5>10false 2<=0false 2>=0true Practice – evaluate

10 Logical Operators !not &&and ||or - can link relational expressions - create complex conditional expressions Example Algebra: 90≤ mark ≤ 100 C++ Statement: mark >= 90 && mark <=100

Logical Operators ABA&&BA||B!A!B Truth table for conditional expressions 0 = false 1= true

12 Operator Precedence 1.( ) 2.! 3. >= 4.== != 5.&& 6.||

13  (-6 =10)  (3.0 >= 2.0) || (3.0 >= 4.0)  (3.0 >= 2.0) && (3.0 >= 4.0)  ! (3.0 >= 4.0) true && true results in true Evaluate Expressions true || false results in true true && false results in false ! false results in true

14 Selection Statements C++ selection statements: –if statements if statement if-else statement if-else-if statement –switch statements –? the conditional operator

15 The if statement one-way branch of action if(expression) statement; /*single statement executed if expression is true */ // statement block is executed if expression is true. if(expression) { statement1; statement2; … statement n; }

16 The if statement - examples if (salary<500) salary=salary+30; if(x>0) { x=sqrt(x); ++k; }

17 Nested if statement If statement (s) within an if statement block if(x>0) { if (y<100) x=sqrt(y); x+=4; }

18 The if - else statement 2-way branch of action if(expression) statement; else statement; if(expression) { statement block } else { statement block }

The if-else Statement - Example //determine grade result #include using namespace std; int main(){ double grade; cout<<“Enter grade: “; cin>>grade; if ( grade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "take this course again.\n"; } return 0; }

20 The nested if-else int x=9, y=7, z=2, k=0, m=0, j=0; if(x > y) if(y < z) k++; else m++; else j++;

21 The if – else if statement multi-way branch of action Form of nested if-else if(expression1) {statement;} else if(expression2) {statement;} else if(expression3) {statement;} …… else {statement;} if(expression1) {statement;} else { if(expression2) { statement;} else { if(expression3) { statement;} else { …….. } Each branch may contain a single statement or a block of statements encloses within braces:{ statement; statement; statement;}

22 The if – else if statement (Example1) If(day==1) cout<<“Saturday\n”; else if(day==2) cout<<“Sunday\n”; else if(day==3) cout<<“Monday\n”; else if(day==4) cout<<“Tuesday\n”; else if(day==5) cout<<“Wednesday\n”; else if(day==6) cout<<“Thursday\n”; else if(day==7) cout<<“Friday\n”; else cout<<“Sorry! Invalid day number\n.”;

23 The if – else if statement (Example2) If(grade>=90) cout<<“A”; else if(grade>=80) cout<<“B”; else if(grade>=70) cout<<“C”; else if(grade>=60) cout<<“D”; else cout<<“F”;

24 The if – else if statement (Example3) if (rank==1 || rank==2) cout << "Lower division \n"; else if (rank==3 || rank==4) cout << "Upper division \n"; else if (rank==5) cout << "Graduate student \n"; else cout << "Invalid rank \n";

25 The switch statement switch(expression) { case constant: statement(s); break; case constant: statement(s); break; /* default is optional*/ default: statement(s); }

26 The switch statement Expression must be of type integer or character. The keyword case must be followed by a constant. break statement is required unless you want all subsequent statements to be executed.

27 switch statement example1 switch(grade)//grade of type char { case ‘A’: cout<<“Excellent Work!”<<endl; break; case ‘B’: cout<<“Good Work!”<<endl; break; case ‘C’: cout<<“Average Work!”<<endl; break; case ‘D’: case ‘F’: cout<<“Poor Work!”<<endl; break; }//end switch

28 switch statement example2 Switch (rank) { case 1: case 2: cout << "Lower division \n"; break; case 3: case 4: cout << “Upper division \n"; break; case 5: cout << “Graduate student \n"; break; default: cout << "Invalid rank \n"; }

29 The Conditional Operator The conditional operator replaces a simple if/else statement Syntax: expression ? statement1 : statement2; expression is a conditional expression. if expression is true statement1 is executed if expression is false statement 2 is executed Example: a>b? ++count : c=a+b; Equivalent if-else: if(a>b) ++count; else c=a+b;