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

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

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Microsoft Access 3: Understanding and Creating Queries

Computer Programming C++ Classes and Objects 15 th Lecture

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

Appendix K Introduction to Microsoft Visual C++ 6.0

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

6. Control Structures

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

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

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

Chapter One Introduction to Programming

Lecture 2 Notes: Flow of Control

Beginning to Program Python

if and if-else: Part 1

Microsoft Access 2000

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Microsoft Access XP Session 1 Week 8

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:

Creating a Database in Access

Passing 1D arrays to functions.

The if Statement and Practice Problems

The While Loop. Objectives. Textbook. WHILE Loops

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

MS Access Lab 2. Topic: Tables

First Bytes Programming Lab 2

Lecture 2 Mathcad Basics

Geometer s Sketchpad. Discovering the incenter of a triangle

Statements and Control Flow

13 Classes & Objects with Constructors/Destructors

CISC 181 Project 3 Designing Classes for Bank Accounts

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

Informatica e Sistemi in Tempo Reale

How to Perform Data Backup for No Boot Issues? With Screenshots

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

C++ Input/Output: Streams

Advanced Bash Scripting. Joshua Malone

F ahrenheit = 9 Celsius + 32

Basics of I/O Streams and File I/O

EXCEL 2007 VLOOKUP FOR BUDGET EXAMPLE

Visual Logic Instructions and Assignments

How To Use Spss

Time Clock Import Setup & Use

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

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Moving from C++ to VBA

Ohio University Computer Services Center August, 2002 Crystal Reports Introduction Quick Reference Guide

Introduction to Python

How to set up a database in Microsoft Access

What is a database? The parts of an Access database

A. Grouping to Obtain Counts

Scanned image. If multiple scanner installed in the computer then click here to select desired scanner. Select Resolution, Color, and Scan Type.

GUIDE. To access reports in the Skyward Gradebook open the Gradebook Main Screen.

Object Oriented Software Design

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

Common Beginner C++ Programming Mistakes

MICROSOFT ACCESS A. CREATING A DATABASE B. CREATING TABLES IN A DATABASE

While Loop. 6. Iteration

Introduction to Microsoft PowerPoint

Title: SharePoint Advanced Training

1001ICT Introduction To Programming Lecture Notes

University of Southern California Marshall Information Services

Introduction to Java

Microsoft Access 2010: Basics & Database Fundamentals

Visual Basic Programming. An Introduction

Repetition Using the End of File Condition

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Grandstream XML Application Guide Three XML Applications

Conditions & Boolean Expressions

Deitel Dive-Into Series: Dive Into Microsoft Visual C++ 6

The MAC address, short for Media Access Control address, is a number in hexadecimal format that uniquely identifies every machine on a network.

MICROSOFT ACCESS 2003 TUTORIAL

DiskPulse DISK CHANGE MONITOR

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Introduction to Microsoft Access

Lab 2: MS ACCESS Tables

How to Concatenate Cells in Microsoft Access

Upload Center Forms. Contents. Defining Forms 2. Form Options 5. Applying Forms 6. Processing The Data 6. Maxum Development Corp.

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Computer Programming In QBasic

Quosal Form Designer Training Documentation

Computer Science for San Francisco Youth

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Some of the Choices. If you want to work on your own PC with a C++ compiler, rather than being logged in remotely to the PSU systems

Creating a Simple Macro

POLARIS INSTALLATION

Tactile and Advanced Computer Graphics Module 5. Graphic Design Fundamentals

Chapter 2 Introduction to Java programming

Step by step guides. Deploying your first web app to your FREE Azure Subscription with Visual Studio 2015

Introduction. Chapter 1

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

MOBILE DEVICE CONFIGURATION GUIDE ActiveSync

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

Part I. Multiple Choice Questions (2 points each):

Transcription:

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

Setting up a coordinate system By default the viewing window has a coordinate system already set up for you 10-10 10-10 The origin is in the middle of the screen -10 < x < 10-10 < y < 10

Setting up coordinates To set up your own coordinates you can write: cwin.coord(x1,y1,x2,y2); Where x1 and y1 are coordinates of the top left point and x2 and y2 are coordinates of bottom right point. (x1,y1) cwin.coord(-20,20,20,-20); (x2,y2)

The Input Window & Prompts All input and prompts are displayed at the top of the graphics window. The prompt appears in a gray box. Plan your graphics to leave some blank space at the top, or else they ll be covered up when a prompt appears. Like the Message class, the prompt could be any string, but escape characters (like \n \t) won t work.

The Input Window & Prompts To get text input from the user we use cwin's member function get_string. Example: string text; text = cwin.get_string("type some text"); get_string does three things: 1) It displays the text in quotes on the graphics window. 2) It waits for user to enter text 3) It grabs the text and returns it. Because get_string does not automatically put the input text into some variable like cin does, it just returns it, so we need to assign the returned value to some variable. Since ccc_win uses the <string> library, we don t need to include it to use strings. The prompt and user input appears at the top of the graphics window. We can t move it. If you want text inside the window, use Message class.

Example #include "ccc_win.h" int ccc_win_main ( ) { string first_name; string last_name; string message_string; first_name = cwin.get_string ("What is your first name?"); last_name = cwin.get_string ("What is your last name?"); message_string = "Your full name is: " + first_name + " " + last_name; cwin << Message (Point(0,1), message_string); } return 0;

cwin cont. Similarly we can get numbers from the user: int int age = cwin.get_int("how old are you?"); double double x_coord = cwin.get_double("enter the x-coordinate.");

Getting mouse click location We can also get as an input a location where the user clicks. Point P = cwin.get_mouse("click somewhere!"); get_mouse returns the location as a Point object. The prompt will appear at the top of the screen. The coordinates are displayed on top of the screen as mouse moves.

Example: User created triangle #include "ccc_win.h" int ccc_win_main ( ) { Point P1; Point P2; Point P3; P1 = cwin.get_mouse("click point 1!"); P2 = cwin.get_mouse("click point 2!"); P3 = cwin.get_mouse("click point 3!"); Line L1(P1,P2); Line L2(P2,P3); Line L3(P3,P1); cwin << L1 << L2 << L3; } return 0;

cwin member functions cwin.coord (double x1, double y1, double x2, double y2) cwin << x cwin.clear ( ) string cwin.get_string (string text) int cwin.get_int (string text) double cwin.get_double (string text) Point cwin.get_mouse (string text) Sets the viewing window with top left corner (x1,y1) and bottom right corner (x2,y2). Outputs graphics object x (Point, Line, Circle, or Message). Erases the screen. Displays prompt text and returns the entered string. Displays prompt text and returns the entered integer. Displays prompt text and returns the entered double. Displays prompt text and returns the mouse click point.

Hitting the target Consider a following hypothetical program. We want to draw concentric circles and then label each circle with a number as shown in the figure below. 1 5 10 Then we want to prompt the user to click on the target and depending on which circle they clicked, we want to tell them what their score is.

Hit the target pseudo code 1. Make three concentric circles using the circle class. 2. Draw the circles. 3. Create three messages for the score labels. 4. Write the messages to the screen. 5. Prompt the user to click on the screen and capture the point where they click into a point variable. 6. Figure out the users score. 7. Create a message with the users score. 8. Write the score message to the window.

If statement How do we figure out how many points to report? It depends on which circle we are inside. We want our code to function something like the following: score = 0 if (inside biggest circle) score += 1 if (inside middle circle) score += 4 if (inside smallest circle) score += 5

How does an if statement work? if (expression) { // Execute the code between braces } If the expression is true the code between the braces is executed. If the expression is false, all the code in the braces is skipped.

Why are if statements useful? Here is a flow diagram for all our programs before if statement Command Command Command Pretty boring... Command

Flow chart for a typical program with if statements Command if Command if Command Command if if

Boolean expressions Boolean expression is something that the computer can assign a true or false value to. Example: 6 > 2 3 > 17 Boolean variable is a variable that can only store one of two values: true (1) or false (0). if statements use boolean expressions! if (boolean expression) { //Do stuff }

Table of basic boolean operators Operator Description True example False example < Less than 2 < 5 5 < 2 > Greater than 5 > 2 2 > 5 >= Greater than or 5 >= 5 2 >= 5 equal <= Less than or equal 2 <= 5 5 <= 2 == Equal (comparison) 2 == 2 2 == 5!= Not equal 2!=5 2!=2

if statement When the computer encounters an if statement it checks first if the boolean expression in the parenthesis is true. If it is true then the computer executes the code inside the braces. if (boolean expression) { //Do stuff }

Example int x; cout << " Please give me a number"; cin >> x; if (x > 5) { cout << "Your number was greater than 5"; }

Importance of { } Actually ommitting the braces is legal. We could write: if ( x > 7 ) cout << "Your number is bigger than 7!"; The program works as expected. If you omit braces the next line of code is assumed to be under the if statement.

Importance of { } what about: if ( x > 7) cout << "Your number is bigger than 7!"; cout << "Congratulations you win!"; Here regardless of value of x the computer will output the line: Congratulations you win! When you want multiple statements under an if statement, you need braces. if ( x > 7) { cout << "Your number is bigger than 7!"; cout << "Congratulations you win!"; } Good idea to always use the braces because it doesn't hurt.