1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

Slides:



Advertisements
Similar presentations
1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
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 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 3. Selection Constructs.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
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.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Selection Structures (if & switch statements) (CS1123)
Object Oriented Programming Spring COMSATS Institute of Information Technology Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Overview Go over parts of quiz? Another iteration structure for loop.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 C++ Classes and Data Structures Course link…..
Chapter 3 Selection Statements
LESSON 06.
Dr. Shady Yehia Elmashad
MT262A Review.
Dr. Shady Yehia Elmashad
Dr. Shady Yehia Elmashad
FUNCTIONS& FUNCTIONS OVERLOADING
Additional Control Structures
Conditional Construct
Review for Final Exam.
CS150 Introduction to Computer Science 1
Counting Loops.
Structure As Function Arguments
CPS120: Introduction to Computer Science
Chapter 4: Control Structures I (Selection)
Review for Final Exam.
Fundamental Programming
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Functions Chapter No. 5.
Structures Chapter 4.
Presentation transcript:

1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements iv) Functions and passing parameter v) Structures

2/22 Structural Programming in C and C++ Even though object-oriented programming is central to C++, you still need to know basic structural constructs to do the job. The implementation of the member functions of a class (i.e. methods in OOP term) is largely structural programming. Almost all the structural programming constructs in C are also valid in C++.

3/22 Relational Operators... int n; cout << "Enter a number: "; cin >> n; cout << "n<10 is " << (n < 10) << endl; cout 10 is " 10) << endl; cout << "n==10 is " << (n == 10) << endl;... A Sample Run: Enter a number: 20 n<10 is 0 n>10 is 1 n==10 is 0

4/22 Relational Operators (cont.) Displaying the results of relational operations, or even the values of type bool variables, with cout << yields 0 or 1, not false and true. The relational operators in C++ include: >, =, <=. Any value other than 0 is considered true, only 0 is false.

5/22 Logical Operators  Logical AND Operator: && if ( x == 7 && y == 11 ) statement ; Logical OR Operator: || if ( x < 5 || x 15 ) statement ;  Logical NOT Operator: ! if !(x == 7) statement ;

6/22 Operator Precedence SUM = SUM + 5 OR SUM =+ 5

7/22 Conditional Statement: if Syntax

8/22 Conditional Statement: if…else Syntax If (x > 100) statement ; else statement ; A statement can be a single statement or a compound statement using { }.

9/22 Conditional Statement: switch Syntax switch(speed) { case 33: statement ; break; case 45: statement ; break; case 78: statement ; break; } Int a,b,c; char op; cin >> a >>op >>b; switch(op) case ‘+’: c= a+b;break; case ‘-’: c= a-b;break; default: cout <<“unknown operator”; }

10/22 Conditional Operator Syntax

11/22 Conditional Operator Syntax result = (alpha < 77) ? beta : gamma; is equivalent to if (alpha < 77) result = beta; else result = gamma;

12/22 Result = (num > 0)?‘positive’: (num<0) ?’negative’: ’zero’ is equivalent to if num>0 result = ‘positive’; else if num <0 result = ‘negative’; else result = ‘zero’; Example

13/22 The for Loop Syntax

14/22 The for Loop Control Flow

15/22 Example For (I=1;I<=10;I++) cout << I;cout << “*” cin>>n; For (I=1;I<=10;I++) For (I=1;I<=n;I++) cout << “*” <<endl; sum = sum +I; cout << sum; For (I=1;I<=3;I++) For (j=1;j<=10;j++) cout << I << “*” <<j<<“=“<<I*j;

16/22 The while Loop Syntax

17/22 The while Loop Control Flow

18/22 Example i = 1; While (i<=10) cout << i; Cin >> n; I = 1; While (I<=n) { sum = sum + I; cout << sum } While ( I<=10 ) cout << “*”;

19/22 The do Loop Syntax

20/22 The do Loop Control Flow

21/22 Example Do {cout <<I; I = I +1; } while (I<=10); Cin >> n; Do { cout <<I; I ++; } while (I<=n)

22/22 Functions Def : Set of statements used for a specific task Syntax: returntype fName( arguments) {… statements return variblename} Types of functions: 1. Function with no arguments and no return 2. Functions with argument and no return 3. Functions with argument and a return

23/22 Using Functions To Aid Modularity... void starline(); // function prototype int main() {... starline();... starline(); return 0; } void starline() // function definition { for(int j=0; j<45; j++) cout << '*'; cout << endl; }

24/22 Passing Arguments To Functions void repchar(char, int); int main() { char chin; int nin; cin >> chin; cin >> nin; repchar(chin, nin); return 0; } void repchar(char ch, int n) { for(int j=0; j < n; j++) cout << ch; cout << endl; }

25/22 Returning Values From Functions float lbstokg(float); int main() { float lbs; cout << "Enter your weight in pounds: "; cin >> lbs; cout << "Your weight in kg is " << lbstokg(lbs) << endl; return 0; } float lbstokg(float pounds) { return * pounds; }

26/22 Using Structures To Group Data struct part { // declare a structure int modelnumber; // ID# of widget int partnumber; // ID# of widget part float cost; // cost of part }; int main() { part part1; part1.modelnumber = 6244; part1.partnumber = 373; part1.cost = ; cout << "Model " << part1.modelnumber << ", part " << part1.partnumber << ", cost $" << part1.cost << endl; return 0; }

27/22 Structures Within Structures struct Distance { int feet; float inches; }; struct Room { Distance length; Distance width; }; int main() { Room dining={ {13, 6.5},{10, 0.0} }; float l = dining.length.feet + dining.length.inches/12; float w = dining.width.feet + dining.width.inches/12; cout << "Dining room area is " << l * w << " square feet" << endl; return 0; }