Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)



Similar documents
9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

Chapter 5. Selection 5-1

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Lecture 2 Notes: Flow of Control

While Loop. 6. Iteration

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Chapter 8 Selection 8-1

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

C++FA 5.1 PRACTICE MID-TERM EXAM

6. Control Structures

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Statements and Control Flow

AT&T Global Network Client for Windows Product Support Matrix January 29, 2015

Analysis One Code Desc. Transaction Amount. Fiscal Period

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Moving from C++ to VBA

C++FA 3.1 OPTIMIZING C++

Conditions & Boolean Expressions

13 Classes & Objects with Constructors/Destructors

Module 2 Stacks and Queues: Abstract Data Types

Chapter 5 Functions. Introducing Functions

COMPARISON OF FIXED & VARIABLE RATES (25 YEARS) CHARTERED BANK ADMINISTERED INTEREST RATES - PRIME BUSINESS*

COMPARISON OF FIXED & VARIABLE RATES (25 YEARS) CHARTERED BANK ADMINISTERED INTEREST RATES - PRIME BUSINESS*

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Common Data Structures

Sources: On the Web: Slides will be available on:

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Chapter One Introduction to Programming

J a v a Quiz (Unit 3, Test 0 Practice)

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Data Structure with C

Informatica e Sistemi in Tempo Reale

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

C++ INTERVIEW QUESTIONS

The While Loop. Objectives. Textbook. WHILE Loops

CS 2412 Data Structures. Chapter 2 Stacks and recursion

Stacks. Linear data structures

Object Oriented Software Design

The C Programming Language course syllabus associate level

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Moving from CS 61A Scheme to CS 61B Java

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

VB.NET Programming Fundamentals

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Keil C51 Cross Compiler

Case 2:08-cv ABC-E Document 1-4 Filed 04/15/2008 Page 1 of 138. Exhibit 8

Common Beginner C++ Programming Mistakes

CENTERPOINT ENERGY TEXARKANA SERVICE AREA GAS SUPPLY RATE (GSR) JULY Small Commercial Service (SCS-1) GSR

10CS35: Data Structures Using C

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Section IV.1: Recursive Algorithms and Recursion Trees

Example of a Java program

Introduction. Earlier programs structured as methods that call one another in a disciplined, hierarchical manner Recursive methods

Basics of I/O Streams and File I/O

CmpSci 187: Programming with Data Structures Spring 2015

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

Comp151. Definitions & Declarations

Passing 1D arrays to functions.

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Selection Statements

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Lecture Notes on Linear Search

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

6.087 Lecture 2 January 12, 2010

Enhanced Vessel Traffic Management System Booking Slots Available and Vessels Booked per Day From 12-JAN-2016 To 30-JUN-2017

Java Interview Questions and Answers

Member Functions of the istream Class

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Syllabus OBJECT ORIENTED PROGRAMMING C++

C++ program structure Variable Declarations

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Recursion vs. Iteration Eliminating Recursion

Introduction to Java

Legislative Brief: PAY OR PLAY PENALTIES LOOK BACK MEASUREMENT METHOD EXAMPLES. EmPowerHR

Tutorial on C Language Programming

6. Standard Algorithms

2015 Ohio MemberSource Newsletter Targeted Production Schedule (Laura Huff/Courtney Stewart)

1 Description of The Simpletron

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Lumousoft Visual Programming Language and its IDE

Transcription:

Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the right value <= // true if left value is less than OR EQUAL to the right value > // true if left value is greater than the right value >= // true if left value is greater than OR EQUAL to the right value Logical Operators! // inverts true to false or false to true && // boolean AND // boolean OR Precedence Rules (from highest to lowest priority) - Arithmetic operators (i.e. ++,--,+,-,*,/, ) - Boolean operators (i.e. <, >, <=, >=) - ==,!= - && - Note: Integer values can be used as boolean values - 0 is considered as false and non- zero is considered as true - For this class, we should not use numbers for boolean values. All boolean expressions must be bool types. If- else statements Ability to execute two alternative statements based on the value of a Boolean Expression. if (Boolean Expression) { // code1 else { // code2 - Notice the {. This allows many statements to be executed within a block. - Without {, only the following statement will be executed and other statements are considered outside the block statement. Multi- way If- else Statements Multiple if- else statements can allow the program to execute many branches.

int x = 3; if (x == 1) cout << x equals 1 << endl; else if (x == 2) cout << x equals 2 << endl; else if (x == 3) cout << x equals 3 <<endl; else cout << x does not equal 1, 2, or 3 << endl; Switch statements - Another way to write Multi- way if- else statements are switch statements. - A switch statement can use either an integer type, bool, char, or enum. - The break statement tells the compiler that it s the end of a case. - Without the break statement, the compiler will execute the statements until a break is reached. int a = 3; switch (a) { case 1: cout << x equals 1 << endl; case 2: cout << x equals 2 << endl; case 3: cout << x equals 3 << endl; default: cout << x does not equal 1, 2, or 3 << endl; It is possible to have many cases refer to the same statements. int a = 3; switch (a) { case 1: case 2: cout << x equals 1 or 2 << endl; case 3: cout << x equals 3 << endl; default: cout << x does not equal 1, 2, or 3 << endl;

Embedded if- else statements Within a block statement, another if statement can be written. int x = 7; if (x >= 5) { cout << x > 5 << endl; if (x == 5) { cout << x == 5 << endl; else { cout << x > 5 << end; While loop while (Boolean Expression) { // code The code block will be executed as long as the boolean expression evaluates to true. Do- while Loop do { //code while (Boolean Expression); Useful if you want to execute a block of code at least once. For Loop for (initialization; boolean expression; update) { //code - Legal to leave the init, boolean expression, and/or update statement empty. - Considered bad style to leave any of them blank. Nested Loops Within a loop s block, other loops can be defined. for (int i = 0; i < 10; i++) { cout << i = << i << endl; for (int j = 0; j < 10; j++) { cout << j = << j << endl; The above nested loop is an example of O(n 2 ), where n = 10 in this case.

Continue and break in loops - continue statement can be used to stop the current execution of statements within the block and jump to the next iteration of the current loop. - break statement can be used to break out of the current loop and continue execution after the end of the loop. Call Stack When functions are called, they are maintained in a call stack The call stack consists of one or more frames - An instance of the function. - Contains information such as parameters, return values, and local variables. Each frame also keeps track of the frame pointer for the function that called it. Therefore, when the function finishes execution, it gets popped from the stack and returns to the function that called it so it can continue execution. Recursion When a function contains a call to itself. Factorial N! = N * (N 1) * (N 2) * * 3 * 2 * 1 Note: 0! = 1, 1! = 1 int factorial (int n) { if (n <= 1) // base case return 1; return n * factorial(n 1); Fibonacci f(n) = f(n- 1) + f(n- 2)). f(0) = 0, f(1) = 1, f(2) = 1, f(3) = 2, f(4) = 3, int fibonacci(int n) { if (n == 1) { return 1; if (n == 0) { return 0; retrun fibonacci(n-2) + fibonacci(n-1);

Tail Recursion When there is no other computation to do once a value is returned. int factorialtail(int n, int product) { if (n == 1) return product; return factorialtail(n - 1, product * n); Stack Overflow - Happens when a program crashes due to running out of memory in its call stack. Enumerations - Enumeration is a type whose values are defined by a list of constants of type int. - You can define enumeration types to any int value. enum Month {JAN = 1, FEB = 2, MARCH = 3, APRIL = 4, MAY = 5, JUNE = 6, JULY = 7, AUG = 8, SEP = 9, OCT = 10, NOV = 11, DEC = 12; Note, without defining the values, it starts with 0 and always increments the previous enum by 1. enum Month { JAN, FEB, MARCH, APRIL, MAY, JUNE, JULY, AUG, SEP, OCT, NOV, DEC ; // JAN = 0 à DEC = 11 Note: It s good to define enums in an appropriate header file so other source files may use it. int month = 3; switch (month) case DEC: case JAN: case FEB: cout << It s winter time! << endl; case MARCH: case APRIL: case MAY: cout << It s spring time! << endl; case JUNE: case JULY: case AUG: cout << It s summer time! << endl; case SEP: case OCT:

case NOV: cout << It s fall! << endl; default: cout << Not a valid month << endl;