Principles of Computer Science

Similar documents
Principles of Computer Science

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Java Control Statements

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object-Oriented Programming

Prepared by: Shraddha Modi

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Chapter 3: Operators, Expressions and Type Conversion

Lecture 3 Operators MIT AITI

Control Structures in Java if-else and switch

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

COMP Primitive and Class Types. Yi Hong May 14, 2015

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Flow Control. CSC215 Lecture

More on control structures

Review for Test 1 (Chapter 1-5)

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Decision Making and Loops

DATA TYPES AND EXPRESSIONS

CSCI 1061U Programming Workshop 2. C++ Basics

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Quick Reference Guide

Repetition Structures

Introduction. C provides two styles of flow control:

LECTURE 04 MAKING DECISIONS

ECE 122. Engineering Problem Solving with Java

CSCE 145 Exam 1 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!

Advanced if/else & Cumulative Sum

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

CompSci 125 Lecture 11

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Introduction to C/C++ Lecture 3 - Program Flow Control

APCS Semester #1 Final Exam Practice Problems

Accelerating Information Technology Innovation

Chapter 4 Introduction to Control Statements

Condi(onals and Loops

CS111: PROGRAMMING LANGUAGE II

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

SECTION II: LANGUAGE BASICS

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Chapter 4: Control structures. Repetition

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Introduction to the Java Basics: Control Flow Statements

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Operators & Expressions

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CS110D: PROGRAMMING LANGUAGE I

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Building Java Programs

Lecture 7: General Loops (Chapter 7)

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Conditions, logical expressions, and selection. Introduction to control structures

Chapter 4: Making Decisions

Building Java Programs

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Chapter 4: Control structures

Unit 3. Operators. School of Science and Technology INTRODUCTION

Chapter 4: Making Decisions

The following expression causes a divide by zero error:

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

AP Computer Science Homework Set 1 Fundamentals

Programming with Java

REPETITION CONTROL STRUCTURE LOGO

Basic Java Syntax, cont d. COMP 401, Fall 2015 Lecture 3 1/15/2015

Building Java Programs

Building Java Programs

Building Java Programs Chapter 2

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Building Java Programs

Java Basic Programming Constructs

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Operators. Java operators are classified into three categories:

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

BASIC ELEMENTS OF A COMPUTER PROGRAM

Simple Java Programming Constructs 4

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

Recap: Assignment as an Operator CS 112 Introduction to Programming

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Programming Lecture 3

Programming for Engineers Iteration

Unit 1 Lesson 4. Introduction to Control Statements

Basics of Java Programming variables, assignment, and input

Computer Science is...

16.317: Microprocessor Systems Design I Fall 2015

The for Loop. Lesson 11

Operators in java Operator operands.

Transcription:

Principles of Computer Science Lecture 2 Dr. Horia V. Corcalciuc Horia Hulubei National Institute for R&D in Physics and Nuclear Engineering (IFIN-HH) January 27, 2016

Loops: do-while do-while loops do { while(<expression>); Contrary to while loops, do-while loops, on the first run, the statements in the do-while block are executed first, after which the expression is evaluated and, if it evaluated to true then the statements are executed again. The loop terminates once the expression at the end evaluates to false.

Control Flow break and continue break do { ; while(<expression>); continue do { continue; ; while(<expression>); The break statement can be used to break out of the inner-most loop such that execution continues after the loop block. The continue statement can be used to skip any statements following the continue, re-evaluate the loop expression and then loop again on the next iteration.

Control Flow do-while vs. while do-while Example int i = 10; do { i = i - 1; while(i > 0); while Example int i = 10; while(i > 0) { i = i - 1; It is possible to convert a while loop to a do-while loop and vice versa.

Control Flow Conditionals: if-else if-else if-else if-else branching if(<truth of expression>) { else if(<truth of expression>)) { else if(<truth of expression>)) {... else { if-else if-else branching Example int i = 10; if(i > 5) { System.out.println("greater"); else if(i < 5) { System.out.println("smaller"); else { System.out.println(5); if branches can have multiple else if conditions but only one if and only one else.

Control Flow Conditionals: switch switch branching switch(<value of expression>) { case <constant>: case <constant>: ; default: ; switch branching Example int i = 10; switch(i) { case 1: System.out.println(1); default: System.out.println("Error!"); Upon entering the switch statement, the expression is evaluated to a value. The block of the first case constant that matches what the expression was evaluated to is executed. In case there is a break statement, then the switch block is exited. Otherwise, execution continues to the next case until a break statement is found (case fallthrough). If no case constants match, and if there is a default case, then the statements of the default case are executed. Exercise

Control Flow if vs. switch if if(<truth of expression>) { else if(<truth of expression>)) {... else { switch switch(<value of expression>) { case <constant>: case <constant>: default: if branches on the truth of the expression whilst switch branches on the value of the expression. if-else if-else cannot fall though the blocks switch case-labels take a constant value, a value that can be determined at compile time, not an expression. The truth of the expression for if-else if-else blocks is evaluated at run-time whilst the constants of the switch cases are determined at compile-time.

Control Flow switch differences between C# and Java switch fallthrough in Java switch(<value of expression>) { case <constant>: case <constant>: default: switch fallthrough in C# switch(<value of expression>) { case <constant>: case <constant>: goto default; default: In C# case fallthrough is possible by using the goto statement.

Control Flow Conditional Evaluation Examples if(3 > 5) switch(10) if(2-1) - does not work in C#, Java if(-1) - does not work in C#, Java switch(2 > 1)

L- and R-Values L- and R-Value Assignment L-Value := R-Value Expressions that refer to containers such as memory locations are called L-Value expressions. L-Values can only appear on the left-hand side of an assignment. On the other hand, an R-Value refers to the value of an expression. All L-Values are R-Values but not all R-Values are L-Values. Illegal Examples (Java and C#) i + 3 = 5; // i + 3 is an r-value not an l-value 3 = i + 2; // 3 is NOT an l-value

Assignment and Testing for Equality Programming languages distinguish between assignment and testing for equality by using the following syntax Exercise : Assignment is performed by using a single equal sign (=) and the assignment operation itself evaluates to the contents of the left-most member in the assignment. Evaluation of Assignment int i = 0 int j = (i = 1) + i; Testing for equality is performed with a double-equal sign (==) and the test evaluates to either true or false. Evaluating an Expression if(false == 5 > 3)

Increment and Decrement Pre-Increment Increment and Decrement int i = 5; int j = ++i + 1; Declare a new integer variable i and assign the value 5 to i Declare a new integer variable j, increment i, add 1 to the result and assign to j Post-Increment Increment and Decrement int i = 5; int j = i++ + 1; Declare a new integer variable i and assign the value 5 to i Declare a new integer variable j, take the current value of i, add 1 to the current value of i, assign to j and increment the value of i

Increment and Decrement : Comparison Increment and Decrement i = i + 1; i = i - 1; Pre ++i; --i; Post i++; i--; ASM Pre-Increment ; j = ++i + 42 mov dword [ss:rbp+var_4], 0x1 mov eax, dword [ss:rbp+var_4] add eax, 0x1 ASM Post-Increment ; j = i++ + 42 mov dword [ss:rbp+var_4], 0x1 mov eax, dword [ss:rbp+var_4] mov ecx, eax add ecx, 0x1

Increment and Decrement : In Conditionals Pre-Increment int i = 0; if(++i) { //... Post-increment int i = 0; if(i++) { //... Java and C# cannot directly convert an integer to a boolean but in C and C++ the value 0 represents false and any other value represents true (it is language specific). Pre-incrementing or pre-decrement takes place before the expression is evaluated and post-increment or post-decrement takes place after the expression has been evaluated. Exercise Avoid micro-optimisations and let the compiler optimise for you but keep in mind that a pre-increment is an operation that may generate shorter code.

Operator Precedence Precedence Increment and Decrement e++, e--, ++e, --e Mathematical *, /, %, +, - Relational <, >, <=, >=, ==,!= Logical &&, Examples int a = 5 * 2-1; a = 5 * (2-1); a = 10 % 2 // In C#: boolean -> bool boolean m = 2 > 3; m = (2 > 3) && (1 < 5); m = (2 > 3) (1 < 5); boolean a = (10 % 2) + 1 == 1;

Calculating Factorial Iteratively Calculating Factorial Numbers int j = 1; int n = 0; while(n < 10) { switch(n) { case 0: ++n; System.out.println(1); default: System.out.println(j); j = ++n * j; Factorial Relation n! = Recurrence { 1 if n 0 n (n 1)! if n > 0

The Modulo Operator: Listing Even Numbers Displaying Even Numbers int i = 0; do { if(i % 2 == 0) { System.out.println(i); while(++i < 10); Even Number Set Even = {2k k Z The modulo operator (%) returns the remainder of the division of a number. The values that the modulo operator operates on can be of an integer type or a floating point type. Can you think of a reason why we used a do-while loop instead of a while loop? What would happen if the do-while loop would be converted directly to a while loop. How would we print out the odd numbers? Exercise

Shorthand Shorthand operators can be used whenever the operation involves the left-most variable to which the result is assigned to. List of Shorthand Operation Example Operation Shorthand Notation Addition i = i + 2 i += 2 Multiplication i = i * 2 i *= 2 Division i = i / 2 i /= 2 Modulo i = i % 2 i %= 2

Lecture Material Mirrors Materials, such as the slides, the programs used, the exercises and the solutions to the exercises will be posted at: Personal Website