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

Size: px
Start display at page:

Transcription

1 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read , 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical Operations Objective: Write a program that plays Rock-Paper-Scissors. Pseudo-Code: This is a game for 2 players. Ask player 1 for their choice - rock, paper or scissors Ask player 2 for their choice rock, paper or scissors Implement logic: Tie occurs if If player 1 and player 2 choose the same thing Player1 wins if player 1 selects rock AND player 2 selects scissors Player 1 selects paper AND player 2 selects rock Player 1 selects scissors AND player 2 selects paper Player 2 wins if player 2 selects rock AND player 1 selects scissors Player 2 selects paper AND player 1selects rock Player 2 selects scissors AND player 1 selects paper What do we do if the player enters something other than rock, paper or scissors? How do we write Java code that implements our pseudo-code? player 1 selects rock AND player 2 selects scissors Both of these problems can be solved using logical operations Two logical operations: AND, OR 1

2 Input Validation Ask player 1 for their choice - rock, paper or scissors Verify the choice is good, if not, exit the program. More detailed pseudo code Print a message asking the user to enter their choice Read the choice Exit if choice is invalid. Print a message asking the user to enter their choice Read the choice Scanner keyboard = new Scanner(System.in); System.out.print( Player 1: enter choice 1-rock, 2-paper, 3-scissors: ); String input = keyboard.nextline(); int player1 = Integer.parseInt(input); The choice is valid if the value is 1, 2 or 3. Any other value is not valid. Rewrite this: Choice is valid if player1 == 1 OR player1 == 2 OR player1 == 3 The choice is valid if the value is 1, 2 or 3. Any other value is not valid. Rewrite this: Choice is valid if player1 == 1 OR player1 == 2 OR player1 == 3 boolean isvalidchoice = (player1 == 1) (player1 == 2) (player1 == 3); // same Behavior. Logical OR double pipe symbol Result is true if any of the individual operands is true Stop evaluation if encounter a true Behavior. Logical OR double pipe symbol Result is true if any of the individual operands is true, Stop evaluation if encounter a true Assume player1 has value 2. boolean isvalidchoice = false player1 == 2 player1 == 3; boolean isvalidchoice = false true player1 == 3; boolean isvalidchoice = true; Assume player1 has value 3. Assume player1 has value 1. boolean isvalidchoice = true player1 == 2 player1 == 3; boolean isvalidchoice = true; boolean isvalidchoice = false player1 == 2 player1 == 3; boolean isvalidchoice = false false player1 == 3; boolean isvalidchoice = false false true; boolean isvalidchoice = true; 2

3 Behavior. Logical OR double pipe symbol Result is true if any of the individual operands is true, Stop evaluation if encounter a true Assume player1 has value 15. boolean isvalidchoice = false player1 == 2 player1 == 3; boolean isvalidchoice = false false player1 == 3; boolean isvalidchoice = false false false; Exit if choice is invalid. Use a decision structure. If the value of isvalidchoice is false, we want to exit the program. if (isvalidchoice == false) System.out.println( Entered invalid value. Exiting. ); boolean isvalidchoice = false; The choice is valid if the value is 1, 2 or 3. Any other value is not valid. Rewrite this: Choice is valid if player1 == 1 OR player1 == 2 OR player1 == 3 Use the logical OR symbol to join boolean expressions An alternative. Rewrite this as: Choice is at least 1 AND at most 3 boolean isvalidchoice = player1 >= 1 && player1 <= 3; Behavior. Logical AND double ampersand symbol && Result is true if all of the individual operands are true. Result is false if any operand is false. Stop evaluation if encounter a false The choice is valid if the value is 1, 2 or 3. Any other value is not valid. An alternative. Rewrite this as: Choice is at least 1 AND at most 3 boolean isvalidchoice = player1 >= 1 && player1 <= 3; Behavior. Logical AND double ampersand symbol && Result is true if all of the individual operands are true. Result is false if any operand is false. Stop evaluation if encounter a false Assume player1 has value 1. boolean isvalidchoice = player1 >=1 && player1 <= 3; boolean isvalidchoice = true && player1 <= 3; // evaluate left operand boolean isvalidchoice = true && true; boolean isvalidchoice = true; // evaluate right operand // evaluate && operator Is the behavior different if player1 has a value of 2 or 3?. 3

4 boolean isvalidchoice = player1 >= 1 && player1 <= 3; Behavior. Logical AND double ampersand symbol && Result is true if all of the individual operands are true. Result is false if any operand is false., stop evaluation if encounter a false Assume player1 has value 4. boolean isvalidchoice = player1 >=1 && player1 <= 3; boolean isvalidchoice = true && player1 <= 3; // evaluate left operand boolean isvalidchoice = true && false; // evaluate right operand boolean isvalidchoice = false; // evaluate && operator Assume player1 has value 0. boolean isvalidchoice = player1 >=1 && player1 <= 3; boolean isvalidchoice = false && player1 <= 3; // evaluate left operand boolean isvalidchoice = false; // evaluate && operator Not (!) Operator In our program we wanted to exit if the choice was not valid. Declared a boolean variable isvalidchoice if block was as follows if (isvalidchoice == false) System.out.println( Entered invalid value. Exiting. ); An alternative is to use the NOT (!) operator if (! isvalidchoice ) System.out.println( Entered invalid value. Exiting. ); Logical Operators Practice Precedence int x = 2; int y = 3; boolean r1 = x > y; // r1 has value? boolean r2 =!(x > y); // r2 has value? boolean r3 =!x > y; // compile error b/c! can only be applied to boolean vars boolean r4 = x >= 1 && y < 5 x + y > 6; // what is the value of r4? When an expression contains a mixture of algebraic, logical and relational operations, how is the expression evaluated? Precedence rules (see page 144) These are things you just learn or lookup when you need to boolean r5 = x >= 1 && y < 5 && x + y > 6; // what is the value of r4? 4

5 General Rules (see pg 144) Summary Logical Operators Evaluate expressions in parentheses () Best way to learn is to practice. Do the checkpoint exercises 3.16 and 3.17 on page 145 For each expression: Evaluate NOT first Perform algebraic operations * / % first, left to right + - second, left to right Perform relational operations <, >, <=, >= first == and!= next Logical Operations && first second Logical OR boolean result = expr1 expr2; Variable result as value true if expr1 is true OR if expr2 is true. Stop evaluation if an expression is true Logical AND && boolean result = expr1 && expr2; Variable result as value true if expr1 is true and if expr2 is true. Stop evaluation if an expression is false Not! Invert the value of a boolean variable Formatted Print Formatted Print - printf In Lab3, we defined a variable grossincome as a double. When we output the value, double grossincome = ; System.out.println( The gross income is \$ + grossincome); Results in: The gross income is \$ We can use a formatted print for the output to have 2 places to the right of the decimal. The printf Java statement has 2 parts: Part 1 is the format string Enclosed in double quotes Special qualifiers to designate String data, integer data, real data, and to specify the formatting details Part 2 is a comma separated list of variables Example: Outputting whole numbers (e.g. int). int x = 3; int y = 5; System.out.printf( The sum of %d and %d is %d\n, x, y, x + y); Qualifier: %d means the variable type is a whole number (byte, short, int or long) 1 Qualifier for each variable: x, y, x+y Outputs: The sum of 3 and 5 is 8 5

6 Formatted Print - printf Formatted Print - printf For real numbers, we use the qualifier %f, double x = 1; double y = 3; System.out.printf( %f / %f is %f\n, x, y, x / y); Qualifier: %f means the variable type is a real number (float or double) Output is now: / is Suppose we only want to output to a precision of 2 places. We can do that with the printf by specifying the precision. double x = 1; double y = 3; System.out.printf( The %.2f / %.2f is %.2f\n, x, y, x / y); Qualifier: %f means the variable type is a real number (float or double) Using.2 between the % and f says use 2 spaces to the right of the decimal Output is now: 1.00/3.00 is 0.33 Formatted Print To output Strings, the qualifier is %s See section 3.10 of your book for details, but you can also specify: Minimum field widths Useful if you want to output data in a tabular format Print numbers with commas! int income = 10000; System.out.printf( %,d\n, income); à 10,000 Force left or right justification Summary: printf Using formatted print allows you to have detailed control of how the output will be displayed. The printf Java statement has 2 parts: Part 1 is the format string Enclosed in double quotes Special qualifiers to designate String data, integer data, real data, and to specify the formatting details Part 2 is a comma separated list of variables You don t need to memorize how to print a number with commas etc. You do need to know it is simple to do and how to get the details Project 1: use the comma format to print the incomes. 6

7 More Decision Logic Increment Operators Decision Logics allows us to modify the flow of a program if, if-else, if-else if-else, switch allow for conditional execution of statement. Loop logic allows us to repeat statement(s) We can control how many times to repeat We can control the conditions when to repeat We are going to start with a tool then get into loops! We have already learned how to add or subtract a value from a variable int number = 10; number = number + 1; // number has value 11 number += 1; // number now has value 12 Java supports a short hand notation for adding 1 to a variable number++; // number now has value 13 ++number; // number now has value 14 The first form is post fix and the second is pre fix Same if they are the only statement on the line Pre fix: increment first, then do anything else Post fix: do everything else, then increment int number = 10; Consider: System.out.println( The value of number is + number++); Behavior: After the output is formatted the value of number is incremented by 1 System.out.println( The value of number is + number); number++; Now consider: System.out.println( The value of number is + ++number); Behavior: Before the output is formatted the value of number is incremented by 1 number++; System.out.println( The value of number is + number); Decrement Operators We have already learned how to add or subtract a value from a variable int number = 10; number = number - 1; // number has value 9 number -= 1; // number now has value 8 Java supports a short hand notation for adding 1 to a variable number--; // number now has value 7 --number; // number now has value 6 The first form is post fix and the second is pre fix Same if they are the only statement on the line Pre fix: increment first, then do anything else Post fix: do everything else, then increment 7

8 Recommendation: Never combine increment and decrement operations with other activities. Reduces errors Makes code easier to understand A programmer is writing a menu and the user is supposed to select a menu option by entering a number between 1 and 10. The program needs to verify the selection is valid before continuing. They need to complete the following code: System.out.print( Please enter a number between 1 and 10: ); int choice= keyboard.nextint(); // keyboard is a ref to a Scanner boolean isvalid = YYY; We want the variable isvalid to be true when if (! isvalid ) // if (isvalid == false) the user enters a number between 1 and 10 and false otherwise. System.out.println( Invalid input. Exiting ); What should the programmer replace YYY with? a) (choice > 0 ) && (choice < 11); b) (choice >= 1) && (choice <= 10); c) (choice > 0 ) (choice < 11); d) (choice >= 1) (choice <= 10); e) Either a) or b) f) Either c) or d) 2/5/2018 a) and b) will both work. Recall A && B is true only when both A and B are true. c) and d) will not work. Consider, if choice is 20, then c) results in true because (choice > 0) (choice < 11) becomes true (choice < 11) which becomes true. A programmer is writing a menu and the user is supposed to select a menu option by entering a number between 1 and 10. The program needs to verify the selection is valid before continuing. They need to complete the following code: System.out.print( Please enter a number between 1 and 10: ); int choice= keyboard.nextint(); // keyboard is a ref to a Scanner boolean isnotvalid = YYY; if ( isnotvalid ) System.out.println( Invalid input. Exiting ); What should the programmer replace YYY with? a) (choice <= 0 ) && (choice >= 11); b) (choice < 1) && (choice > 10); c) (choice <= 0 ) (choice >= 11); d) (choice < 1) (choice > 10); e) Either a) or b) f) Either c) or d) 2/7/2018 We want the variable isnotvalid to be true when the user does not enter a number between 1 and 10 and false otherwise. c) and d) will both work. Recall A B is true when either A or B (or both) are true. If the user enters 20, then the right most expressions in c) and d) are both true. a) and b) will not work. There is no way that these expressions can ever produce a true value. 8

### Programming with Java

Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

### Problem Solving With Loops

To appreciate the value of loops, take a look at the following example. This program will calculate the average of 10 numbers input by the user. Without a loop, the three lines of code that prompt the

### Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

### Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Assignments Lecture 6 Complete for Project 1 Reading: 3.1, 3.2, 3.3, 3.4 Summary Program Parts Summary - Variables Class Header (class name matches the file name prefix) Class Body Because this is a program,

### CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING INPUT AND OUTPUT Input devices Keyboard Mouse Hard drive Network Digital camera Microphone Output devices.

### 5) (4 points) What is the value of the boolean variable equals after the following statement?

For problems 1-5, give a short answer to the question. (15 points, ~8 minutes) 1) (4 points) Write four Java statements that declare and initialize the following variables: A) a long integer with the value

### Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

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

COMP 110-001 Primitive and Class Types Yi Hong May 14, 2015 Review What are the two major parts of an object? What is the relationship between class and object? Design a simple class for Student How to

### BASIC INPUT/OUTPUT. Fundamentals of Computer Science

BASIC INPUT/OUTPUT Fundamentals of Computer Science Outline: Basic Input/Output Screen Output Keyboard Input Simple Screen Output System.out.println("The count is " + count); Outputs the sting literal

### Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

### Simple Java Reference

Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...

### Repetition, Looping. While Loop

Repetition, Looping Last time we looked at how to use if-then statements to control the flow of a program. In this section we will look at different ways to repeat blocks of statements. Such repetitions

### AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

AL GHURAIR UNIVERSITY College of Computing CSC 209 JAVA I week 2- Arithmetic and Decision Making: Equality and Relational Operators Objectives: To use arithmetic operators. The precedence of arithmetic

### Repe\$\$on CSC 121 Fall 2015 Howard Rosenthal

Repe\$\$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

### Repe\$\$on CSC 121 Spring 2017 Howard Rosenthal

Repe\$\$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

### Operators & Expressions

Operators & Expressions Operator An operator is a symbol used to indicate a specific operation on variables in a program. Example : symbol + is an add operator that adds two data items called operands.

### Object Oriented Programming. Java-Lecture 1

Object Oriented Programming Java-Lecture 1 Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen

### Repetition CSC 121 Fall 2014 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

### CS111: PROGRAMMING LANGUAGE II

1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables

### These are reserved words of the C language. For example int, float, if, else, for, while etc.

Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

### Introduction to Java & Fundamental Data Types

Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains

### Section 2.2 Your First Program in Java: Printing a Line of Text

Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

### STUDENT LESSON A7 Simple I/O

STUDENT LESSON A7 Simple I/O Java Curriculum for AP Computer Science, Student Lesson A7 1 STUDENT LESSON A7 Simple I/O INTRODUCTION: The input and output of a program s data is usually referred to as I/O.

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

Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

### Fundamental of Programming (C)

Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

### CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

### CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

These are sample pages from Kari Laitinen s book "A Natural Introduction to Computer Programming with Java". For more information, please visit http://www.naturalprogramming.com/javabook.html CHAPTER 5

### Operators in java Operator operands.

Operators in java Operator in java is a symbol that is used to perform operations and the objects of operation are referred as operands. There are many types of operators in java such as unary operator,

### Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Dec. 9 Loops Please answer the following questions. Do not re-code the enclosed codes if you have already completed them. What is a loop? What are the three loops in Java? What do control structures do?

### Loops. CSE 114, Computer Science 1 Stony Brook University

Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

### 2.5 Another Application: Adding Integers

2.5 Another Application: Adding Integers 47 Lines 9 10 represent only one statement. Java allows large statements to be split over many lines. We indent line 10 to indicate that it s a continuation of

### Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M tutor Isam M. Al Jawarneh, PhD student isam.aljawarneh3@unibo.it Mobile Middleware

### Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Motivations If you assigned a negative value for radius

### Chapter 3: Operators, Expressions and Type Conversion

101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

### Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

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

Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

### Computer System and programming in C

1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

### Java Coding 3. Over & over again!

Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java

### Section 2.2 Your First Program in Java: Printing a Line of Text

Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

### Unit 3. Operators. School of Science and Technology INTRODUCTION

INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

### Lesson 10..The switch Statement and char

Lesson 10..The switch Statement and char 10-1 The if statement is the most powerful and often used decision-type command. The switch statement is useful when we have an integer variable that can be one

### CS110: PROGRAMMING LANGUAGE I

CS110: PROGRAMMING LANGUAGE I Computer Science Department Lecture 4: Java Basics (II) A java Program 1-2 Class in file.java class keyword braces {, } delimit a class body main Method // indicates a comment.

### CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS Computers process information and usually they need to process masses of information. In previous chapters we have studied programs that contain a few variables

### 2.8. Decision Making: Equality and Relational Operators

Page 1 of 6 [Page 56] 2.8. Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. This section introduces a simple version of Java's if statement

Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

### Operators and Expressions:

Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence

### 12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

### Program Fundamentals

Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

### Le L c e t c ur u e e 2 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 Variables Operators

Course Name: Advanced Java Lecture 2 Topics to be covered Variables Operators Variables -Introduction A variables can be considered as a name given to the location in memory where values are stored. One

### CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I 05 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions / Comments? recap and some more details about variables, and if / else statements do lab work

### Fundamentals of Programming

Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

### UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

### Course Outline. Introduction to java

Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

### Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Chapter 4 Loops Sections Pages Review Questions Programming Exercises 4.1 4.7, 4.9 4.10 104 117, 122 128 2 9, 11 13,15 16,18 19,21 2,4,6,8,10,12,14,18,20,24,26,28,30,38 Loops Loops are used to make a program

### Repetition, Looping CS101

Repetition, Looping CS101 Last time we looked at how to use if-then statements to control the flow of a program. In this section we will look at different ways to repeat blocks of statements. Such repetitions

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

Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

### Full file at

Chapter 2 Introduction to Java Applications Section 2.1 Introduction ( none ) Section 2.2 First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler

### Basic Operations jgrasp debugger Writing Programs & Checkstyle

Basic Operations jgrasp debugger Writing Programs & Checkstyle Suppose you wanted to write a computer game to play "Rock, Paper, Scissors". How many combinations are there? Is there a tricky way to represent

### ECE 122 Engineering Problem Solving with Java

ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction Outline Problem: How do I input data and use it in complicated expressions Creating complicated expressions

### Arithmetic and IO. 25 August 2017

Arithmetic and IO 25 August 2017 Submissions you can submit multiple times to the homework dropbox file name: uppercase first letter, Yourlastname0829.java the system will use the last submission before

### AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU Week 21/02/2007-23/02/2007 Lecture Notes: ASCII 7 bits = 128 characters 8 bits = 256characters Unicode = 16 bits Char Boolean boolean frag; flag = true; flag

### Chapter 4: Control structures. Repetition

Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

### Loops and Expression Types

Software and Programming I Loops and Expression Types Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline The while, for and do Loops Sections 4.1, 4.3 and 4.4 Variable Scope Section

### CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

### SECTION II: LANGUAGE BASICS

Chapter 5 SECTION II: LANGUAGE BASICS Operators Chapter 04: Basic Fundamentals demonstrated declaring and initializing variables. This chapter depicts how to do something with them, using operators. Operators

### Programming for Engineers Introduction to C

Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

### Midterm Examination (MTA)

M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and

### UNIT 3 OPERATORS. [Marks- 12]

1 UNIT 3 OPERATORS [Marks- 12] SYLLABUS 2 INTRODUCTION C supports a rich set of operators such as +, -, *,,

Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

### Chapter 3. Selections

Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

### GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

GridLang: Grid Based Game Development Language Language Reference Manual Programming Language and Translators - Spring 2017 Prof. Stephen Edwards Akshay Nagpal Dhruv Shekhawat Parth Panchmatia Sagar Damani

### 6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

6.S189 Homework 1 http://web.mit.edu/6.189/www/materials.html What to turn in Do the warm-up problems for Days 1 & 2 on the online tutor. Complete the problems below on your computer and get a checkoff

### SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

FACULTY OF SCIENCE AND TECHNOLOGY SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1 ACADEMIC SESSION 2014; SEMESTER 3 PRG102D: BASIC PROGRAMMING CONCEPTS Section A Compulsory section Question

### Chapter 4: Control structures

Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

### Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Tester vs. Controller Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG For effective illustrations, code examples will mostly be written in the form of a tester

### Operators in C. Staff Incharge: S.Sasirekha

Operators in C Staff Incharge: S.Sasirekha Operators An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C

### More on control structures

Lecture slides for: Chapter 5 More on control structures Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

### JAVA Programming Fundamentals

Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

### Getting started with Java

Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

### Chapter 2: Basic Elements of Java

Chapter 2: Basic Elements of Java TRUE/FALSE 1. The pair of characters // is used for single line comments. ANS: T PTS: 1 REF: 29 2. The == characters are a special symbol in Java. ANS: T PTS: 1 REF: 30

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

### Elementary Programming

Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal

### Accelerating Information Technology Innovation

Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lesson 02 Variables and Operators Agenda Variables Types Naming Assignment Data Types Type casting Operators

### Full file at

Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

### CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)

### Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 5, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction

### Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Examples of Software Programming and Data Structure Lecture 2 Sudeshna Sarkar Read an integer and determine if it is a prime number. A Palindrome recognizer Read in airline route information as a matrix

### Simple Java Programming Constructs 4

Simple Java Programming Constructs 4 Course Map In this module you will learn the basic Java programming constructs, the if and while statements. Introduction Computer Principles and Components Software

### Logic & program control part 2: Simple selection structures

Logic & program control part 2: Simple selection structures Summary of logical expressions in Java boolean expression means an expression whose value is true or false An expression is any valid combination

Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

### Fundamentals of Programming

Fundamentals of Programming Introduction to the C language Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) The C language

### Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will

### AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

### C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

C Programming Language 1 C C is better to use than assembly for embedded systems programming. You can program at a higher level of logic than in assembly, so programs are shorter and easier to understand.

### Computer Programming : C++

The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

### Example: Monte Carlo Simulation 1

Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149

### Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Entry Point of Execution: the main Method Elementary Programming EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG For now, all your programming exercises will be defined within the