Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Chapter 3 - Structured Program Development
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
© 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 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Structured Program Development in C
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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. 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.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional 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.
C Lecture Notes 1 Structured Program Development.
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.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
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.
Decision making If.. else statement.
Branching statements.
Chapter 4 – C Program Control
JavaScript: Control Statements I
Chapter 4 - Program Control
Chapter 2.1 Control Structures (Selection)
Chapter 8 - JavaScript: Control Statements I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Ch 7: JavaScript Control Statements I.
Lecturer CS & IT Department UOS MBDIN
Programming Fundamentals
JavaScript: Control Statements I
Lecture 2: Logical Problems with Choices
- Additional C Statements
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Program Control Topics While loop For loop Switch statement
Chapter 3 - Structured Program Development
2.6 The if/else Selection Structure
Dale Roberts, Lecturer IUPUI
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
Branching statements Kingdom of Saudi Arabia
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

Lecture 2 Control Structure

Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater than <= is less than or equal to >= is greater than or equal to != is not equal to

Logical Operators (&&, ||, !) Used for more complex conditions than the other operators Helps combine multiple conditions –Logical AND – Condition1 && Condition2 –Logical OR – Condition1 || Condition2 Logical Negation – to reverse condition –if (!(grade == “A”)) Can be replaced with relational op (!=) –if (grade != “A”) 1

Control Structures  Control Structures Selection Structures Looping Structures

Control Structures Algorithms –Procedure for solving a problem in terms of The actions to be executed The order in which actions are to be executed Pseudocode –Artificial and informal language that helps programmers develop algorithms Flowchart –Graphical representation of an algorithm

Control Structures Sequential Execution –Statements executed one after the other in order in which they are written Add grade to total Add 1 to counter total = total + grade; counter = counter + 1; 3

Control Structures Selection Structures –Used to choose among alternative courses of action –if selection structure Single selection structure –if /else selection structure Double selection structure –switch selection structure Multiple selection structure 4

Control Structures Repetition Structures –Used to repeat an action while a condition remains true –for repetition structure –while repetition structure –do/while repetition structure 5

Selection Structures if selection structure –Performs an indicated action when the condition is true. “Passed” grade >= 60 true false if (grade >= 60) cout<<“Passed”; 6

Statement 1 expression Statement 2 Statement 3 true false

Selection Structures if/else selection structure –Performs an action when the condition is true and another when the condition is false. if (grade >= 60) printf (“Passed”); else printf (“Failed”); print “Passed” grade >= 60 truefalse print “Failed” 7

Form and Syntax If ( ) else

int main() { int num, neg,pos; cout<<“Please enter the no.”; cin>> num; if (num < 0) neg= neg +1; else pos= pos +1; }

Conditional Operator Closely related to if/else structure ?: (Conditional Operator) Only ternary operator in C –Takes three operands Operands with conditional operator form conditional expression –Operand 1– condition –Operand 2 – action if condition true –Operand 3 – action if condition false 8

Conditional Operator print “Passed” grade >= 60 truefalse print “Failed” grade >= 60 ? “Passed”: “Failed” 9

if (Expression1 ) Statement1 else if (Expression2 ) Statement2... else if (ExpressionN ) StatementN else Statement N+1 EXACTLY 1 of these statements will be executed. Nested if statements

Nested if/else //nested if/else //printing grades of students 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<<“Failed”; 10

Nested if/else //nested if/else //printing grades of students 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<<“Failed”; If grade of a student is 84, the conditions for “B”, “C” and “D” are all true. What will be printed? 11

Selection Structures switch selection structure –Multiple selection structure is useful when an algorithm contains a series of decisions –Variable or expression is tested separately for one of several possible values –Each value represents a different action 12

Switch Selection Structure // counting grades assigned int aCount, bCount, cCount, dCount, fCount; switch (grade) { case ‘A’: case ‘a’: ++aCount; case ‘B’: case ‘b’: ++bCount; case ‘C’: case ‘c’: ++cCount; case ‘D’: case ‘d’: ++dCount; default: ++fCount; } If grade has value ‘A’, which variable is incremented? 13

switch Selection Structure // counting grades assigned int aCount, bCount, cCount, dCount, fCount; switch (grade) { case ‘A’: case ‘a’: ++aCount; break; case ‘B’: case ‘b’: ++bCount; break; case ‘C’: case ‘c’: ++cCount; break; case ‘D’: case ‘d’: ++dCount; break; default: ++fCount; } 14

break Statement break statement causes program control to move to the first statement after the selection/ repetition structure Switch cases would otherwise run together 15

switch Selection Structure case a action case a true false break case b action case b true break default action false 16

Summary of precedence order (revised) OperatorAssociativity () left to right ! right to left * / % left to right + - left to right >= left to right == != left to right && left to right || left to right ?: left to right = += -= *= /= %= right to left, left to right 25

Control Structures C++ keywords –Cannot be used as identifiers or variable names