CH Programming An introduction to programming concepts.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Introduction to C++ Programming
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 01: Introduction to Computer Programming
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
COMPUTER SCIENCE I C++ INTRODUCTION
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 CS161 Introduction to Computer Science Topic #3.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Control Structures (B) Topics to cover here: Sequencing in C++ language.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Lecture2.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 1.2 Introduction to C++ Programming
Completing the Problem-Solving Process
Computing Fundamentals
Basic Elements of C++.
Revision Lecture
Variables A piece of memory set aside to store data
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Programming Funamental slides
Programming Funamental slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Summary Two basic concepts: variables and assignments Basic types:
Lecture3.
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Chapter 3: Selection Structures: Making Decisions
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Capitolo 1 – Introduction C++ Programming
Chapter 3: Selection Structures: Making Decisions
CS 101 First Exam Review.
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

CH Programming An introduction to programming concepts

Outline  Programming Languages  Variables  Assignment Statements  Arithmetic Expressions  Input Statements  Output Statements  Conditional Statements  Loops  Command vs Program mode

Programming Languages  A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer.  Programming languages, like human languages, are defined through the use of syntactic, grammatical and semantic rules, to determine structure and meaning respectively.  Function A programming language is a language used to write computer programs, which involve a computer performing some kind of computation or algorithm and possibly control external devices such as printers, robots, and so on.  Compiled vs. Interpreted

Outline  Programming Languages  Variables  Assignment Statements  Arithmetic Expressions  Input Statements  Output Statements  Conditional Statements  Loops  Command vs Program mode

Variables The Tupperware of Programming  Variables are like saved documents on your computer, each one storing information.  When you name a variable you are actually declaring or defining it. type varName1; -> int age; type varName1, varName2; -> int age, weight; type varName1 = value; -> float ratio = 0.87;  In CH, for example, variables can hold letters (string_t), and numbers (int, double, and float) values.  If you want to access the information that a variable has stored in it, all you have to do is call the variable's name. cout << ratio; 0.87  Make sure the names are descriptive.

Outline  Programming Languages  Variables  Assignment Statements  Arithmetic Expressions  Input Statements  Output Statements  Conditional Statements  Loops  Command vs Program mode

Assignment Statements  Assignment statements are used to assign a value to a variable. variable-name = expression;  Imagine we have defined the following variables: string_t name, lastName, letterGrade; int average, test1, test2, finalExam;  Lets assign them some values: name = "Mary"; lastName = "Smith"; test1 = 98; test2 = 78; finalExam = 90; average = (test1 + test2 + finalExam)/3;  /* ( )/3 = */ letterGrade = "B+";

Outline  Programming Languages  Variables  Assignment Statements  Arithmetic Expressions  Input Statements  Output Statements  Conditional Statements  Loops  Command vs Program mode

Arithmetic Expressions  Just as in algebra, arithmetic expressions are evaluated from left to right with multiplication (*) and division (/) being evaluated before addition (+) and subtraction (-). The operator modulo (%) returns the reminder of a division, e.i. 5 % 2 = 1  Use parentheses to overrule this order or to clarify the expression. For instance, grade = (98 + test2) *.50 + finalExam *.50; Practice arithmetic expressions and other CH topics on problets.orgproblets.org

Outline  Programming Languages  Variables  Assignment Statements  Arithmetic Expressions  Input Statements  Output Statements  Conditional Statements  Loops  Command vs Program mode

Input Statements  Input statements are used to input values and assign the values to variables. cin >> variable-name;  If you declared a variable called test1, int test1;  the following statement: cin >> test1; average = (test1 + test2 + finalExam)/3;  If you want to enter two or more values using one input statement, use >> before each variable name. For example the statement cin >> name >> test1; would allow you to enter your name, press return and then enter a value for test1, and press return again.

Input Statements (Strings)  If you want to input a value into a string variable, the actual text entered would not contain quotation marks as we have seen in the assignment statement. For the statement below that reads in the nickname, the user would enter Bobby, NOT “Bobby”. cin >> name;  If your program is to be used by others, you should output a line to tell the users what they are to enter. cout << "Input your name: "; cin >> name;  Note: In CH, when cin is used to read in a string and an int or float variable was the last value read in the execution, the statement getchar(); Is necessary to remove the rest of the input line that was entered when the number value was read.

Outline  Programming Languages  Variables  Assignment Statements  Arithmetic Expressions  Input Statements  Output Statements  Conditional Statements  Loops  Command vs Program mode

Output Statements  To write to the screen.  To output the value of any type of variable use the form: cout << variable-name; cout << test1  You can also use the the forms cout << “literal string”; or cout << expression; cout<< “text to be written to the screen”;  text to be written to the screen cout <<“Congratulations! You got an “ << letterGrade <<“ !!!” << endl;  Congratulations! You got an B+ !!! cout << “Your average or numeric grade is “ << average;  Your average or numeric grade is  Another example: cout << “Please enter your age”; cin>> yourAge;  When cin or cout statements are used in program mode, the following include statement must be inserted after the comment statements at the beginning of the program to allow the program to accept input and output using the cin and cout instructions. #include

Outline  Programming Languages  Variables  Assignment Statements  Arithmetic Expressions  Input Statements  Output Statements  Conditional Statements  Loops  Command vs Program mode

Conditional Statements  The if-then statement allows the program to choose which statements to execute depending on a condition.  The if statement has the form if (condition) { statement list }  When the if statement is executed, the condition is evaluated. If it is true, the CH code in the statement list (the then portion) is executed. If the condition is false, the statements in the list are skipped.  The brackets can be omitted if there is only one statement in the list.

Conditional Statements  The condition for floating point numbers and integers is an expression that uses at least one of the relational operators below. The condition must be in parentheses. Equal ==  Note: Two equal signs are used for the relational operator, equal. Not equal != Less than < Greater than > Less than or equal <= Greater than or equal >= And && Or ||  Note: logical ANDs and ORs are used to create composite conditions, i.e. conditions with multiple parts. In the case of AND, all sub-conditions of the composite condition must be true in order for the composite condition to evaluate to true. In the case of OR, it suffices that one sub-condition is true for the composite condition to be true.

Conditional Statements if (average >= 90){ letterGrade = “A”; cout << “Congratulations! You got an A!”<<endl; } if (average = 85) cout << “Congratulations! You got a B+!”<<endl; if (average >= 70){ cout << “Congratulations! You passed!”<<endl; } else { cout << “I’m sorry. You failed!”<<endl; }

Outline  Programming Languages  Variables  Assignment Statements  Arithmetic Expressions  Input Statements  Output Statements  Conditional Statements  Loops  Command vs Program mode