In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to C Programming
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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 )
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
C++ for Engineers and Scientists Third Edition
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 4—Statement Forms The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Statement Forms C H A P T E R 4 The statements.
Chapter 4 Statement Forms. Statement types 1.Simple statements expression; println(“The total is “ + total + “.”); (method call) 2. Compound statements.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
Expressions Eric Roberts CS 106A January 13, 2016.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 4 Statement Forms. Statement types 1.Simple statements expression; println(“The total is “ + total + “.”); (method call) 2. Compound statements.
Five-Minute Review 1.What are classes and objects? What is a class hierarchy? 2.What is an expression? A term? 3.What is a variable declaration? 4.What.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
CS 106A, Lecture 4 Introduction to Java
Chapter 3 Control Statements
Selections Java.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Expressions and Control Flow in JavaScript
Chapter 3: Understanding C# Language Fundamentals
CS106A, Stanford University
Flow of Control.
CS 106A, Lecture 6 Control Flow and Parameters
CS 106A, Lecture 5 Booleans, Control Flow and Scope
Flow of Control.
Computers & Programming Languages
Module 2: Understanding C# Language Fundamentals
Flow of Control.
LOOPS BY: LAUREN & ROMEO.
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Lecture Notes – Week 2 Lecture-2
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
SSEA Computer Science: Track A
Chapter 2 Programming Basics.
Presentation transcript:

In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program

We will learn about: Boolean Shorthand's for adding, subtracting Constants If, for, while and scope

Just like readInt is used for reading integers readDouble is used to reading doubles

Lets have a look at an example of area of a circle. double PI =3.14; PI is a variable and it can be changed which means the whole equation will change. In order to define a constant we must use: private static final double PI=3.14; You could come up with a constant called SEVENTY_TWO=72

import acm.program.*; public class CalculateAreaOfCircle extends ConsoleProgram { public void run() { double pi = 3.14; println(“This program will calculate the area of a circle”); double r = readDouble(“Enter the radius”); double area = pi * pi * r; println(“The area of the circle is:” + area + “.”); }

Seems like x = x +1 is very commonly used in programming languages There are short hands for doing this: x+=1; x++; x-=1; x--; x*=2; x/=2;

Boolean is a data type which contains true or false P=(3>5) P=False George Boole was the person who came up with the data type Boolean. He was an English Mathematician. He died of pneumonia.

Boolean expression is just a test for a condition Essentially, evaluates to a true or false Value Comparisons == equals != not equals > greater than < less than >= greater than equal to <= less than equal to

Boolean expressions in order of precedence: ! “not” !p if p is true than p is false and vice versa && “and” P && q (only true if both p and q are true) || “OR” P || Q “either p or q or both are true”

Stop evaluation Boolean expressions as soon as we know the answer Consider: P = (5 > 3) || (4<=2); The test (4 <= 2 ) is not performed!

A compound statement (or block) is a set of statements enclosed in braces { int x = 5; double y=readDouble(“y:”); println(“y=“ + y); } Variables scope is a block in which it is declared Scope is the lifetime of a variable When the variable is available to be used.

General form: if (condition) { statements } With else: if (condition) { statements } else if (conditon) { } Its always a good idea to use braces even if there is only one statement

If ( (num % 2) ==0 ) { println(“num is even”); } else { println(“number is odd”); println(“and so are you!”); } Its always a good idea to use braces even if there is only one statement

If (score>=90) { Println(“A”); } else if (score>=80) { Println(“B”); } else if )score>=70) { Println(“C”); } else { Println (“Bad times!”); }

Int day = readInt(“Day of the week as int:”); Switch (day) { Case 0: Println(“Sunday”); Break; Case 6: Println(“Saturday”); Break; Default: Println(“Weekday”); break

General form: For (init; condition; step) { Statements; } Init: done once at the start of the loop (initialization) Condition: checked before every iteration through loop Statements get executed if conditions is true

Example: for (int i=0; i<5; i++) { println(i); } As computer scientists we count from 0 The variable I dies after the brace

Example: For (int i=6; i>0; i=-2) { Println(i); } is not displayed

General form: While (condition) { statements;} Example: int x=15; while (x>1) { x/=2; println(x); } is not displayed

Before beginning to use ACM library you need to download it from: