C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
CMPUT 101 Lab # 5 October 22, :00 – 17:00.
True or false A variable of type char can hold the value 301. ( F )
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
CS107 Introduction to Computer Science Java Basics.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Programming is instructing a computer to perform a task for you with the help of a programming language.
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.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Hello World 2 What does all that mean?.
CS107 Introduction to Computer Science Java Basics.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
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.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
CONTROLLING PROGRAM FLOW
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Fundamental Programming: Fundamental Programming Introduction to C++
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
Programming in java Lecture-2 Java Introduction. Platform Computing platform include hardware and software framework, this combination allows software.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
Fundamental Programming Fundamental Programming Introduction to Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Programming what is C++
CMPT 201.
REPETITION CONTROL STRUCTURE
Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
CS161 Introduction to Computer Science
Introduction to C++ October 2, 2017.
Chapter 2 – Getting Started
Compound Assignment Operators in C++
Introduction to C++ Programming
Programming Funamental slides
Selection Control Structure
Computing Fundamentals
Programs written in C and C++ can run on many different computers
Fundamental Programming
Repetition Statements (Loops) - 2
CS 101 First Exam Review.
CSC 1051 – Data Structures and Algorithms I
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Programming Fundamental
Presentation transcript:

C++ Basics CSci 107

A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always need to include the header that // defines cin and cout; the header is called iostream.h #include int main() { //variable declaration //read values input from user //computation and print output to user return 0; } After you write a C++ program you compile it; that is, you run a program called compiler that checks whether the program follows the C++ syntax –if it finds errors, it lists them –If there are no errors, it translates the C++ program into a program in machine language which you can execute

Notes what follows after // on the same line is considered comment indentation is for the convenience of the reader; compiler ignores all spaces and new line ; the delimiter for the compiler is the semicolon all statements ended by semicolon Lower vs. upper case matters!! –Void is different than void –Main is different that main

The infamous Hello world program When learning a new language, the first program people usually write is one that salutes the world :) Here is the Hello world program in C++. #include int main() { cout << “Hello world!”; return 0; }

Variable declaration type variable-name; Meaning: variable will be a variable of type Where type can be: –int//integer –double//real number –char//character Example: int a, b, c; double x; int sum; char my-character;

Input statements cin >> variable-name; Meaning: read the value of the variable called from the user Example: cin >> a; cin >> b >> c; cin >> x; cin >> my-character;

Output statements cout << variable-name; Meaning: print the value of variable to the user cout << “any message “; Meaning: print the message within quotes to the user cout << endl; Meaning: print a new line Example: cout << a; cout << b << c; cout << “This is my character: “ << my-character << “ he he he” << endl;

If statements if (condition) { S1; } else { S2; } S3; condition S1 S2 S3 TrueFalse

Boolean conditions..are built using Comparison operators == equal != not equal < less than > greater than <= less than or equal >= greater than or equal Boolean operators && and || or ! not

Examples Assume we declared the following variables: int a = 2, b=5, c=10; Here are some examples of boolean conditions we can use: if (a == b) … if (a != b) … if (a <= b+c) … if(a <= b) && (b <= c) … if !((a < b) && (b<c)) …

If example #include void main() { int a,b,c; cin >> a >> b >> c; if (a <=b) { cout << “min is “ << a << endl; } else { cout << “ min is “ << b << endl; } cout << “happy now?” << endl; }

While statements while (condition) { S1; } S2; condition S1 S2 TrueFalse

While example //read 100 numbers from the user and output their sum #include void main() { int i, sum, x; sum=0; i=1; while (i <= 100) { cin >> x; sum = sum + x; i = i+1; } cout << “sum is “ << sum << endl; }

Exercise Write a program that asks the user –Do you want to use this program? (y/n) If the user says ‘y’ then the program terminates If the user says ‘n’ then the program asks –Are you really sure you do not want to use this program? (y/n) –If the user says ‘n’ it terminates, otherwise it prints again the message –Are you really really sure you do not want to use this program? (y/n) –And so on, every time adding one more “really”.