Introduction to Programming Lesson 1. Algorithms Algorithm refers to a method for solving problems. Common techniques for representing an algorithms:

Slides:



Advertisements
Similar presentations
Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within.
Advertisements

Introduction to Programming Lesson 1. Objectives Skills/ConceptsMTA Exam Objectives Understanding Computer Programming Understand computer storage and.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
By Sam Nasr Nasr Information Systems May 14, 2013.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Program Design and Development
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
C++ for Engineers and Scientists Third Edition
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Chapter 2: Algorithm Discovery and Design
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Understanding Events and Exceptions Lesson 3. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand.
CIS Computer Programming Logic
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
VB.Net - Exceptions Copyright © Martin Schray
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 2: General Problem Solving Concepts
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
2Object-Oriented Program Development Using C++ 3 Basic Loop Structures Loops repeat execution of an instruction set Three repetition structures: while,
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CPS120: Introduction to Computer Science Decision Making in Programs.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Exception Handling How to handle the runtime errors.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 Problem Analysis  Coding  Debugging  Testing.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4 – C Program Control
Programming Fundamentals
Arrays, For loop While loop Do while loop
Structured Program
Chapter 6 Control Statements: Part 2
ICT Programming Lesson 3:
2. Second Step for Learning C++ Programming • Data Type • Char • Float
ICT Gaming Lesson 2.
Chapter 4 - Program Control
Introduction to Programming
Presentation transcript:

Introduction to Programming Lesson 1

Algorithms Algorithm refers to a method for solving problems. Common techniques for representing an algorithms: –Flowchart –Decision Tables. Flowcharts and Decision Tables More precise than natural languages Less formal and easier to use than programming languages

Flowcharts A flowchart is a graphical representation of an algorithm. Common Flowchart Symbols Start or end of an algorithm Process or computational operation Input or out operation Decision making operation Direction of the flow of control

Flowchart Example A flowchart that compares two numbers: Start Input x Input y X > y? Output x Stop Output y No Yes

Decision Table Useful for large number of conditions Compact and readable format A decision table to calculating discount: Quantity < 10YNNN Quantity < 50YYNN Quantity < 100YYYN Discount5%10%15%20%

Introducing C# Microsoft.NET Framework –An Execution Environment –Reusable Class Libraries –Language Compilers The C# Programming Language –Part of the.NET Framework –High-level Language –Program needs to be compiled before they can be executed. –Case sensitive

Structure of a C# Program

Elements of a C# Program Select common elements of a C# program: Data TypesTypes of data in a program. Common data types are int (integers), char (single character value), float (floating point values). VariablesProvides temporary storage during program execution. int number = 10; ConstantsData fields whose value cannot be modified. const int i = 10; ArraysA collection of items in which each item can be accessed by a unique index. int[] numbers = { 1, 2, 3, 4, 5 }; OperatorsSymbols that specify which operation to perform on operands before returning a result. MethodsMethods are code blocks containing a series of statements. Methods can receive input via arguments and can return a value to the caller.

Decision Structures The if Statement The if-else Statement The switch Statement

The if Statement The if statement will execute a given sequence of statements only if the corresponding Boolean expression evaluates to true.

The if-else Statement The if-else statement allows your program to perform one action if the Boolean expression evaluates to true and a different action if the Boolean expression evaluates to false.

The switch Statement The switch statement allows multi-way branching. In many cases, using a switch statement can simplify a complex combination of if-else statements.

Repetition Structures The while LoopThe do-while LoopThe for LoopThe foreach LoopRecursion

The while Loop The while loop repeatedly executes a block of statements until a specified Boolean expression evaluates to false.

The do-while Loop The do-while loop repeatedly executes a block of statements until a specified Boolean expression evaluates to false. The do-while loop tests the condition at the bottom of the loop.

The for Loop The for loop combines the three elements of iteration—the initialization expression, the termination condition expression, and the counting expression—into a more readable code.

The foreach Loop The foreach loop is an enhanced version of the for loop for iterating through collections such as arrays and lists.

Recursion Recursion is a programming technique that causes a method to call itself in order to compute a result.

Exception Handling An exception is an unexpected error condition that occurs during program execution. When exception occurs, the runtime creates an exception object and “throws” it. Unless you “catch” the exception, the program execution will terminate. Exceptions are an object of the System.Exception class or one of its derived classes. –Example: DivideByZeroException exception object is thrown when the program attempts to divide by zero. –Example: FileNotFoundException exception object is throws when the program cannot find a given file.

Unhandled Exceptions What happens when the file c:\data.txt is not found in this code?

Handling Exceptions with try-catch Place the code that throws the exceptions inside a try block. Place the code that handles an exception inside a catch block. You can have more than one catch blocks for each try block. Each catch block handles a specific exception type. A try block must have at least a catch block or a finally block associated with it.

Exception Handling Sample

The finally Block The finally block is used in association with the try block. The finally block is always executed regardless of whether an exception is thrown. The finally block is often used to write clean-up code.

try-catch-finally Example