Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Air Force Institute of Technology Electrical and Computer Engineering
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
True or false A variable of type char can hold the value 301. ( F )
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Logical Operators and Conditional statements
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
IIT Kanpur C Course Lecture 3 Aug 31, Rishi Kumar, Final year BT-MT, CSE.
Chapter Making Decisions 4. Relational Operators 4.1.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Meet with your team later today in ELL321 at 6:30pm. Teams are listed on the Project page of the course.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Control statements Mostafa Abdallah
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
Data types Data types Basic types
Week 3 - Friday CS222.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
CISC/CMPE320 - Prof. McLeod
Boolean Expressions and If
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CMPE212 – Reminders The other four assignments are now posted.
Comp Org & Assembly Lang
Program Flow.
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom of the Projects page. Redmine sites are being built.

Today Bitwise Operators. Boolean Expressions. Conditionals and Loops. (If we have time – Demos of vector and string Classes.) Fall 2015CISC/CMPE320 - Prof. McLeod2

Fall 2015CISC/CMPE320 - Prof. McLeod3 Bit and Shift Operations These operate on all integer types and enums. Complement, ~, carries out a bitwise negation of each bit (1 becomes 0, 0 becomes 1). Binary &, | and ^ : ABA&BA|BA^B

Fall 2015CISC/CMPE320 - Prof. McLeod4 Bit and Shift Operations, Cont. The left shift operator, <<, moves all bits in val to the left by n positions: val << n. Zeros are added to the least significant bits. This is the same as multiplying a number by 2 n. Right shift, >>, moves all bits to the right. For unsigned integers zeros are added to the most significant bits – same as dividing by 2 n. For signed integers, the result is not predicable as the sign bit may be duplicated.

Fall 2015CISC/CMPE320 - Prof. McLeod5 Bitwise Operations, Examples Set the n th bit in a number and the rest are zeros: 1 << n To set the n th bit of any number, x, and not change the rest: x = x | 1 << n To check to see if the n th bit is set: if ((x & 1 << n) != 0)…

Fall 2015CISC/CMPE320 - Prof. McLeod6 Boolean Expressions We have seen the boolean operators already. Here are a few notes: Something like a < b < c will compile and run, but may not produce the desired result. Better to use: a < b && b < c Remember that & and | are bitwise operators, not logical ones.

Fall 2015CISC/CMPE320 - Prof. McLeod7 Boolean Expressions, Cont. The && and || logical operators use “short circuit evaluation”: For && if the LHS is false then the RHS is not evaluated. For || if the LHS is true then the RHS is not evaluated. (Same as in Java.)

Fall 2015CISC/CMPE320 - Prof. McLeod8 Boolean Expressions, Cont. Non zero integers are treated as being true, and zero is treated as being false. (Ouch!) So, you can use logical operators, && || and !, with integers. For example, the code: int x = 10; if (x) is the same as: int x = 10; if (x != 0)

Fall 2015CISC/CMPE320 - Prof. McLeod9 Boolean Expressions, Cont. See TestStuff.cpp. Applying ! to a non-zero integer returns false or zero. An if statement will treat a pointer by itself as an integer – it will be true unless it is nullptr. You can also test assignment statements since the assignment operator returns the value being assigned.

Fall 2015CISC/CMPE320 - Prof. McLeod10 Conditional Expressions C++ has if, if/else and switch. Syntax of if/else: if (boolean_expression) true_part else false_part Use { } to enclose more than one statement.

Fall 2015CISC/CMPE320 - Prof. McLeod11 switch Statement Same syntax as in Java: switch (expression) { case val1: // statements if expression produces val1 break; case val2: // statements if expression produces val2 break; case val3: … default: // statements if none of the above is true } // end switch

Fall 2015CISC/CMPE320 - Prof. McLeod12 switch Statement, Cont. expression must be an integer. The break statements are needed to prevent the next case statement from taking place if the one above it is true. The default is only executed if none of the other case blocks have executed.

Fall 2015CISC/CMPE320 - Prof. McLeod13 The Conditional Operator int max = (n1 > n2) ? n1 : n2; Is the same as: int max; if (n1 > n2) max = n1 else max = n2 Which is easier to read?

Fall 2015CISC/CMPE320 - Prof. McLeod14 Loops Three types: while (condition) statement_or_block do statement_or_block while(condition); for(init; condition; expr) statement_or_block

Fall 2015CISC/CMPE320 - Prof. McLeod15 Goto C++ also has the infamous “goto” statement: goto identifier : …. identifier : statement Don’t use it! (Maybe use it to kick you completely out of a nested loop. break only kicks you out one level…)

Fall 2015CISC/CMPE320 - Prof. McLeod16 break and continue Work as they do in Java: break; spits you right out of the loop. continue; sends you back to the loop condition.

As Promised Demos of the string and vector (a template container class) classes from the STL: StringDemo.cpp VectorDemo.cpp Much better than C-strings and arrays! See CPlusPlus.com for more info and member functions. Fall 2015CISC/CMPE320 - Prof. McLeod17