C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.

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

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
True or false A variable of type char can hold the value 301. ( F )
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
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++ (4) 10 September.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Lecture 1 The Basics (Review of Familiar Topics).
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
Basic Elements of C++ Chapter 2.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
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.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
CSC 107 – Programming For Science. The Week’s Goal.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
LESSON 3 Expressions, Statements, & Operators. STATEMENTS A statement Controls the sequence of execution Evaluates an expression Does nothing (the null.
C++ Lesson 1.
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
Computing and Statistical Data Analysis Lecture 2
Computing and Statistical Data Analysis Lecture 2
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Java Primer 1: Types, Classes and Operators
Basic Elements of C++.
C Short Overview Lembit Jürimägi.
C Basics.
Basic Elements of C++ Chapter 2.
11/10/2018.
Starting JavaProgramming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
CS139 October 11, 2004.
Summary Two basic concepts: variables and assignments Basic types:
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
COP 3330 Object-oriented Programming in C++
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Fundamental Programming
In this class, we will cover:
Presentation transcript:

C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Types Quick reminder from the previous session: –int, bool, char, double, float, string,... Some types can also be long or short and some can be unsigned: –unsigned long int x; Classes are new types that we can get from elsewhere (e.g. Standard Template Library or G4MICE) or which we can make ourselves. string is a class that the STL provides for us. 2

Control and Looping We will look at the following: –If –Else –While –For C/C++ also provides: –Switch –Case Which we will not cover in this course. 3

Operators Mathematical: + (addition) - (subtraction) * ( multiplication) / (division) % (modulus) Logical: & (bitwise AND) | ( bitwise OR) ! (NOT) 4

Logical Operators == equal to, this is different from =, which is the “assignment operator”!) != not equal to < less than > greater than <= less than or equal to >= greater than or equal to && logical AND || logical OR 5

Statement Blocks In all of the control and looping commands you may wish to have more than one command executed. To do this, we put the commands inside a pair of { } Any block of code can be collected in this manner (we will come back to this with scope later on). 6

if Statement if( condition ) expression; if( condition ) { expression1; expression2;... expressionN; } Note that the if() does not have a semicolon after it, that would be a NULL command: –If( condition ); This would do nothing. 7

else else can be added as many times as required: if( condition 1 ) expression 1; else if( condition 2) expression 2;... else expression N; As before, { } can be used as required. 8

while while will loop over the code so long as the condition is true: while( condition ) expression; while( condition ) { expression1; expression2;... expressionN; } 9

for A loop that allows you to set some initial values, specify the condition that must be true for looping to continue and specify code to be executed at the end of each loop: –for( x = 0; x < 10; x = x + 1 ) {... } This will start with x = 0 and execute the code in the {} and then repeat with x = 1, 2, 3,... up to 9. 10

= -= Several operators in addition to those we’ve already seen: ++x; increment x --y; decrement y; x += 5; add 5 to x; z -= 10; subtract 10 from z; These can be useful in loops, e.g.: –for( x = 0; x < 10; ++x ) 11

Control and Looping Example Download loop.cc, compile it and run it. Read through the code and let me know if anything is unclear. 12

Functions Functions in C are declared by specifying: –The return type (can be void if nothing is returned) –The function name –Whether it is const (we will get to this later) –Arguments to the function Examples: –void dump(); –double absoluteValue( double x ); 13

Absolute Value The file absolute.cc shows an example of a function that returns the absolute value of the parameter that is passed to it. Compile and run this and ensure that you understand how it works. 14

Parameter Passing Parameters to functions are only ever passed “by value”. That is, a copy of the value of the variable is sent to the function. There is a way to pass by reference, we will cover that in the next session. For now, you can’t change the value of a variable that is passed to a function. 15

Signatures The complete set of information about a function that uniquely identifies it is known as its signature. The signature consists of the function name, return type, constness and arguments. You can have more than one function with the same name, so long as they have different signatures. 16

signature.cc The file signature.cc shows an example of different functions with the same name, but different signatures. Compile and run this and ensure that you understand how it works. 17

scope Functions, variables and types may not be visible from all code. We have seen an example of the std namespace, within which variables such as cout and types such as string are defined. This is why we refer to them with std:: preceding the variable or type name. 18

variables inside { } If a variable is declared inside a { } block, it is only visible to the code inside that block. This means that you can temporarily declare a variable that will only exist while the code in that block is being executed. It also means that you can run the risk of having two different variables of the same name at the same time! 19

scope example The file scope.cc shows a range of examples of variables being visible to different fractions of the code. Compile and run this and ensure that you understand how it works. Note that it will NOT compile at first, you will need to understand why... 20

Final Exercise If you have time, add a function to the now working scope example. Experiment to understand whether the variables defined inside main() are visible inside the new function and vice versa. 21

After Lunch Classes Pointers and References Makefiles Standard Template Library Unit testing Documentation Homework assignment for tomorrow! 22