Fundamental of Programming (C)

Size: px
Start display at page:

Download "Fundamental of Programming (C)"

Transcription

1 Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 5 Structured Program Development Department of Computer Engineering

2 How to develop a program? Requirements Problem Analysis Refinement Design Designing algorithm Pseudo code Flow chart Implementation Implementing algorithm in c/c++ Validation Maintenance Test Software Development Life-Cycle Department of Computer Engineering 2/65

3 Department of Computer Engineering 3/65

4 Requirements Discovery Problem Analysis Problem Identification Abstraction Inputs and outputs determination Defining their relation Write a program to compute an employee's weekly pay? How many hours did you work? How much do you get paid per hour? employee's weekly pay equal to multiplication of Hours by Pay Rate Department of Computer Engineering 4/65

5 Problem vs. Solution A problem is something that causes trouble The solution is how to solve or fixe the problem Department of Computer Engineering 5/65

6 Algorithm A procedure for solving a problem in terms of actions and the order in which these actions are to be executed in a computer program (program control) Action Order Department of Computer Engineering 6/65

7 Execution order Sequential execution, normally, statements in a program are executed one after the other in the order in which they re written Various C statements enable you to specify that the next statement to be executed may be other than the next one in sequence. This is called transfer of control Department of Computer Engineering 7/65

8 Structured Programming goto statement that allows programmers to specify a transfer of control to one of many possible destinations in a program Research had demonstrated that programs could be written without any goto statements Programs produced with structured techniques were clearer, easier to debug and modify and more likely to be bug free in the first place. Research had demonstrated that all programs could be written in terms of only three control structures, namely the sequence structure, the selection structure and the repetition structure Department of Computer Engineering 8/65

9 Pseudo code An artificial and informal language that helps you develop algorithms similar to everyday English are not executed on computers consists only of action statements pay-calculation program: 1. Read Hours and Pay Rate 2. weekly pay = Read Hours * Pay Rate Department of Computer Engineering 9/65

10 Flowchart A flowchart is a diagram that depicts the flow of an algorithm pay-calculation program START Display message How many hours did you work? Read Hours Display message How much do you get paid per hour? Each symbol represents a different type of operation Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 10/65

11 Terminals represented by rounded rectangles indicate a starting or ending point START Terminal START Display message How many hours did you work? Read Hours Display message How much do you get paid per hour? Read Pay Rate END Multiply Hours by Pay Rate. Store result in Gross Pay. Flow line Display Gross Pay Terminal END Department of Computer Engineering 11/65

12 Input/Output Operations indicate an input or output operation START Display message How many hours did you work? Display message How many hours did you work? Input/Output Operation Read Hours Display message How much do you get paid per hour? Read Pay Rate Read Hours Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 12/65

13 Processes indicates a process such as a mathematical computation or variable assignment Multiply Hours by Pay Rate. Store result in Gross Pay. Process START Display message How many hours did you work? Read Hours Display message How much do you get paid per hour? Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 13/65

14 Execution START Display message How many hours did you work? Output Operation How many hours did you work? Read Hours Display message How much do you get paid per hour? Read Pay Rate Variable Contents: Hours:? Pay Rate:? Gross Pay:? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 14/65

15 Execution How many hours did you work? 40 Input Operation (User types 40) START Display message How many hours did you work? Read Hours Display message How much do you get paid per hour? Read Pay Rate Variable Contents: Hours: 40 Pay Rate:? Gross Pay:? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 15/65

16 Execution START Display message How many hours did you work? How much do you get paid per hour? Read Hours Display message How much do you get paid per hour? Output Operation Read Pay Rate Variable Contents: Hours: 40 Pay Rate:? Gross Pay:? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 16/65

17 Execution START Display message How many hours did you work? How many hours did you work? 20 Read Hours Display message How much do you get paid per hour? Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay:? Input Operation (User types 20) Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 17/65

18 Execution START Display message How many hours did you work? How many hours did you work? 20 Read Hours Display message How much do you get paid per hour? Read Pay Rate Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Process (Gross Pay = Hours * Pay Rate) Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 18/65

19 Execution START Display message How many hours did you work? Your gross pay is 800 Read Hours Display message How much do you get paid per hour? Read Pay Rate Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Output Operation Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 19/65

20 Connectors Sometimes a flowchart will not fit on one page A connector (represented by a small circle) allows you to connect two flowchart segments START A END A Department of Computer Engineering 20/65

21 Modules A program module (such as a function in C) is represented by a special symbol START The position of the module symbol indicates the point the module is executed module Read Input Call calc_pay function A separate flowchart can be constructed for the module Department of Computer Engineering 21/65 Display results END

22 Control Structures Sequence Decision selection statement The if statement is called a single-selection statement because it selects or ignores a single action. The if else statement is called a double-selection statement because it selects between two different actions. The switch statement is called a multiple-selection statement because it selects among many different actions Repetition while do while for Department of Computer Engineering 22/65

23 Sequence Structure a series of actions are performed in sequence The pay-calculating example Department of Computer Engineering 23/65

24 Compound Statements A statement is a specification of an action to be taken by the computer as the program executes Compound Statements is a list of statements enclosed in braces, { } Department of Computer Engineering 24/65

25 Decision Structure One of two possible actions is taken, depending on a condition Selection structures are used to choose among alternative courses of action NO YES NO x < y? YES Process A Process B Department of Computer Engineering 25/65

26 Decision Structure The flowchart segment below shows how a decision structure is expressed in C as an if/else statement Flowchart C programming language NO x < y? YES if (x < y) a = x * 2; Calculate a as x plus y. Calculate a as x times 2. else a = x + y; Department of Computer Engineering 26/65

27 Example Compound statement is way to group many statements together so they are treated as one if (grade >= 60) printf( "Passed.\n" ); // { printf( "Passed.\n" ); } else { printf( "Failed.\n" ); printf( "You must take this course again.\n" ); } Department of Computer Engineering 27/65

28 Decision Structure The flowchart segment below shows a decision structure with only one action to perform Flowchart C programming language NO x < y? YES if (x < y) a = x * 2; Calculate a as x times 2. Department of Computer Engineering 28/65

29 Combining Structures NO x > min? YES if (x > min) { if (x < max) printf("x is within the limits"); else printf("x is outside the limits"); } else printf("x is outside the limits"); Display x is outside the limits. NO Display x is outside the limits. x < max? YES Display x is within limits. Department of Computer Engineering 29/65

30 Example int k = 1, m = 4; if (k < 2 m == 3) { m = 2 + k; printf("%d", m); } else { k = 1; printf("%d", k); } int k = 1, m = 4; if (k < 2 m == 3) { m = 2 + k, printf("%d", m); } else { k = 1, printf("%d", k); } Which is more readable? Department of Computer Engineering 30/65

31 Example if(x) if(y) printf("yes"); else printf("no"); if (x < 0) sign = -1; else if (x == 0) sign = 0; else sign = 1; if(x) { if(y) printf("yes"); else printf("no"); } if (x < 0.25) count1++; else if (x >= 0.25 && x < 0.5) count2++; else if (x >= 0.5 && x < 0.75) count3++; else count4++; if(x) { if(y) printf("yes"); } else printf("no"); Department of Computer Engineering 31/65

32 Case Structure One of several possible actions is taken, depending on the contents of a variable Department of Computer Engineering 32/65

33 Case Structure indicates actions to perform depending on the value in years_employed If years_employed = 1, bonus is set to 100 If years_employed = 2, bonus is set to 200 CASE years_employed If years_employed = 3, bonus is set to 400 If years_employed is any other value, bonus is set to Other bonus = 100 bonus = 200 bonus = 400 bonus = 800 Department of Computer Engineering 33/65

34 switch A switch statement allows a single variable (integer or char) to be compared with several possible constants A constant can not appear more than once, and there can only be one default expression Department of Computer Engineering 34/65

35 switch switch (variable) { case const: statements...; default: statements...; } switch (c = toupper(getch())) { case R : printf("red"); break; case G : printf("green"); break; default: printf("other"); } Department of Computer Engineering 35/65

36 Example switch(betty) { case 1: printf("betty = 1\n"); case 2: printf("betty=2\n"); break; case 3: printf("betty=3\n"); break; default: printf("not sure\n"); } CASE betty? Other betty = 1 betty = 2 betty = 3 Not sure Department of Computer Engineering 36/65

37 Repetition Structure A loop tests a condition, and if the condition exists, it performs an action. Then it tests the condition again. If the condition still exists, the action is repeated. This continues until the condition no longer exists x < y? YES Process A Department of Computer Engineering 37/65

38 Repetition Structure The flowchart segment below shows a repetition structure expressed in C as a while loop Flowchart C programming language while (x < y) x < y? YES Add 1 to x x++; Department of Computer Engineering 38/65

39 While while (loop_repetition_condition) statement; OR //Compound statement while (loop_repetition_condition) { statement1; statement2; // } Department of Computer Engineering 39/65

40 Controlling a Repetition Structure The action performed by a repetition structure must eventually cause the loop to terminate. Otherwise, an infinite loop is created In this flowchart segment, x is never changed. Once the loop starts, it will never end How can this flowchart be modified so it is no longer an infinite loop? x < y? YES Display x Department of Computer Engineering 40/65

41 Controlling a Repetition Structure Adding an action within the repetition that changes the value of x YES x < y? Display x Add 1 to x Department of Computer Engineering 41/65

42 A Pre-Test Repetition Structure This type of structure is known as a pre-test repetition structure. The condition is tested BEFORE any actions are performed if the condition does not exist, the loop will never begin YES x < y? Display x Add 1 to x Department of Computer Engineering 42/65

43 Example while (1); int counter = 0; while (counter < 1000) ; int counter = 0; while (counter < 9) printf("%d\n", counter ++); int counter = 9; while (counter > 0) printf("%d\n", counter --); int counter = 0; while (counter < 9) { printf("%d\n", counter); counter++; } int counter = 0; while (counter < 9) { printf("%d\n", counter ++); } Department of Computer Engineering 43/65

44 A Post-Test Repetition Structure The condition is tested AFTER the actions are performed A post-test repetition structure always performs its actions at least once C programming language Display x do { printf( ); x++; } while (x < y); Add 1 to x x < y? YES Department of Computer Engineering 44/65

45 do-while do statement; while (loop_repetition_condition) OR do //Compound statement { statement1; statement2; // } while (loop_repetition_condition) Department of Computer Engineering 45/65

46 Example Draw a flowchart for the following problem: Read 5 integer and display the value of their summation. Can you identify the input and output? Input : 5 integer n1, n2, n3, n4, n5 Output: The summation of n1, n2,.., n5 Input example: Output example: 20 Department of Computer Engineering 46/65

47 Assume input example: start Input n1 n1 2 This flowchart does not use loop, hence we need to use 6 different variables Input n2 Input n3 input n4 n2 n3 3 4 input n5 n4 5 sum n1+n2+n3+n4+n5 n5 6 output sum sum 20 end Department of Computer Engineering 47/65

48 Assume input example: counter 1, sum 0 counter sum counter < 6 true input n sum sum + n counter++ output sum false < < 6 6 false true n The This Uses counter loop only is counter-controlled Increases 3 variables by 1 Department of Computer Engineering 48/65

49 Decreasing Counter-Controlled Loop counter 5, sum 0 false counter > 0 true input x sum sum+ x counter-- output sum Department of Computer Engineering 49/65

50 For for (initial_value ; condition; update_counter) statement; OR // Compound statement for (initial_value ; condition; update_counter) { statement; statement; // } Department of Computer Engineering 50/65

51 counter 1, sum 0 counter < 6 true input n sum sum + n counter++ output sum false int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf( %d,&x); sum = sum + x; } printf( %d,sum); Department of Computer Engineering 51/65

52 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n );??? num _ Department of Computer Engineering 52/65

53 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 1 num _ Department of Computer Engineering 53/65

54 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 1 num _ Department of Computer Engineering 54/65

55 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 1 num 1 _ Department of Computer Engineering 55/65

56 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 2 num 1 _ Department of Computer Engineering 56/65

57 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 2 num 1 _ Department of Computer Engineering 57/65

58 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 2 num 1 2 _ Department of Computer Engineering 58/65

59 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 3 num 1 2 _ Department of Computer Engineering 59/65

60 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 3 num 1 2 _ Department of Computer Engineering 60/65

61 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 3 num _ Department of Computer Engineering 61/65

62 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 4 num _ Department of Computer Engineering 62/65

63 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 4 num _ Department of Computer Engineering 63/65

64 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 4 num have come to exit_ Department of Computer Engineering 64/65

65 Example * ** *** **** ***** ****** #include <stdio.h> int main() { int icounter = 0; int jcounter = 0; while (icounter < 7) { jcounter = 0; while (jcounter < icounter) { printf("*"); jcounter++; } printf("\n"); icounter++; } getch(); return 0; } Department of Computer Engineering 65/65

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 5 - Structured Program Development Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad How to develop a program? Requirements Problem Analysis

More information

Introduction to Flowcharting

Introduction to Flowcharting Introduction to Flowcharting 1 Acknowledgment This tutorial is based upon Appendix C from Starting Out with C++: From Control Structures to Objects (5th Edition) Copyright Tony Gaddis 2007 Published by

More information

Control Structure: Loop

Control Structure: Loop Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1 Loop Structure Condition is tested first

More information

depicts pictorially schematic representation of an algorithm document algorithms. used in designing or documenting

depicts pictorially schematic representation of an algorithm document algorithms. used in designing or documenting Definition A flowchart depicts pictorially the sequence in which instructions are carried out in an algorithm. A flowchart is a schematic representation of an algorithm or a stepwise process, showing the

More information

Structured Program Development

Structured Program Development Structured Program Development Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Introduction The selection statement if if.else switch The

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving

CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving CS 199 Computer Programming Spring 2018 Lecture 2 Problem Solving ALGORITHMS AND FLOWCHARTS A typical programming task can be divided into two phases: Problem solving phase produce an ordered sequence

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

Fundamentals of Programming. Lecture 6: Structured Development (part one)

Fundamentals of Programming. Lecture 6: Structured Development (part one) Fundamentals of Programming Lecture 6: Structured Development (part one) Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu edu Sharif University of Technology Computer Engineering Department Outline Algorithms

More information

Chapter 3 Structured Program Development

Chapter 3 Structured Program Development 1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

More information

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e Before writing a program to solve a particular problem, it s essential to have a thorough understanding of the problem and a carefully planned approach to solving the problem. The

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 08: Control Statements Readings: Chapter 6 Control Statements and Their Types A control

More information

Theory of control structures

Theory of control structures Theory of control structures Paper written by Bohm and Jacopini in 1966 proposed that all programs can be written using 3 types of control structures. Theory of control structures sequential structures

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure

More information

Introduction to C Programming

Introduction to C Programming 1 2 Introduction to C Programming 2.6 Decision Making: Equality and Relational Operators 2 Executable statements Perform actions (calculations, input/output of data) Perform decisions - May want to print

More information

BIL101E: Introduction to Computers and Information systems Lecture 8

BIL101E: Introduction to Computers and Information systems Lecture 8 BIL101E: Introduction to Computers and Information systems Lecture 8 8.1 Algorithms 8.2 Pseudocode 8.3 Control Structures 8.4 Decision Making: Equality and Relational Operators 8.5 The if Selection Structure

More information

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Topics

More information

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs Morteza Noferesti Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs We want to solve a real problem by computers Take average, Sort, Painting,

More information

BRANCHING if-else statements

BRANCHING if-else statements BRANCHING if-else statements Conditional Statements A conditional statement lets us choose which statement t t will be executed next Therefore they are sometimes called selection statements Conditional

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

LECTURE 5 Control Structures Part 2

LECTURE 5 Control Structures Part 2 LECTURE 5 Control Structures Part 2 REPETITION STATEMENTS Repetition statements are called loops, and are used to repeat the same code multiple times in succession. The number of repetitions is based on

More information

Decision Making and Loops

Decision Making and Loops Decision Making and Loops Goals of this section Continue looking at decision structures - switch control structures -if-else-if control structures Introduce looping -while loop -do-while loop -simple for

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure 2.8 Formulating

More information

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14 Day05 A Young W. Lim 2017-10-07 Sat Young W. Lim Day05 A 2017-10-07 Sat 1 / 14 Outline 1 Based on 2 Structured Programming (2) Conditions and Loops Conditional Statements Loop Statements Type Cast Young

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

More information

Module 4: Decision-making and forming loops

Module 4: Decision-making and forming loops 1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The

More information

Structured Program Development in C

Structured Program Development in C 1 3 Structured Program Development in C 3.2 Algorithms 2 Computing problems All can be solved by executing a series of actions in a specific order Algorithm: procedure in terms of Actions to be executed

More information

Outline. Program development cycle. Algorithms development and representation. Examples.

Outline. Program development cycle. Algorithms development and representation. Examples. Outline Program development cycle. Algorithms development and representation. Examples. 1 Program Development Cycle Program development cycle steps: Problem definition. Problem analysis (understanding).

More information

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching. Class Incharge: S. Sasirekha Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

More information

Computer System and programming in C

Computer System and programming in C Approaches to Problem Solving Concept of algorithm and flow charts ALGORITHMS AND FLOWCHARTS A typical programming task can be divided into two phases: Problem solving phase produce an ordered sequence

More information

FLOW CHART AND PSEUDO CODE

FLOW CHART AND PSEUDO CODE FLOW CHART AND PSEUDO CODE Flowchart A Flowchart is a pictorial representation of an algorithm. The First flowchart is made by John Von Newman in 1945. It is a symbolic diagram of operation sequence, dataflow,

More information

Lecture 6. Statements

Lecture 6. Statements Lecture 6 Statements 1 Statements This chapter introduces the various forms of C++ statements for composing programs You will learn about Expressions Composed instructions Decision instructions Loop instructions

More information

Computers Programming Course 7. Iulian Năstac

Computers Programming Course 7. Iulian Năstac Computers Programming Course 7 Iulian Năstac Recap from previous course Operators in C Programming languages typically support a set of operators, which differ in the calling of syntax and/or the argument

More information

Chapter 5: Control Structures

Chapter 5: Control Structures Chapter 5: Control Structures In this chapter you will learn about: Sequential structure Selection structure if if else switch Repetition Structure while do while for Continue and break statements S1 2017/18

More information

Chapter Two: Program Design Process and Logic

Chapter Two: Program Design Process and Logic Chapter Two: Program Design Process and Logic 2.1 Chapter objectives Describe the steps involved in the programming process Understand how to use flowchart symbols and pseudocode statements Use a sentinel,

More information

Chapter 13 Control Structures

Chapter 13 Control Structures Chapter 13 Control Structures Control Structures Conditional making a decision about which code to execute, based on evaluated expression if if-else switch Iteration executing code multiple times, ending

More information

C++ PROGRAMMING SKILLS Part 2 Programming Structures

C++ PROGRAMMING SKILLS Part 2 Programming Structures C++ PROGRAMMING SKILLS Part 2 Programming Structures If structure While structure Do While structure Comments, Increment & Decrement operators For statement Break & Continue statements Switch structure

More information

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

Dr. R. Z. Khan, Associate Professor, Department of Computer Science ALIGARH MUSLIM UNIVERSITY Department of Computer Science Course: CSM-102: Programming & Problem Solving Using C Academic Session 2015-2016 UNIT-2: Handout-3 Topic: Control Structures (Selection & Repetition)

More information

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Program Development Cycle Program development

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information

A control structure refers to the way in which the Programmer specifies the order of executing the statements

A control structure refers to the way in which the Programmer specifies the order of executing the statements Control Structures A control structure refers to the way in which the Programmer specifies the order of executing the statements The following approaches can be chosen depending on the problem statement:

More information

CS1100 Introduction to Programming

CS1100 Introduction to Programming Decisions with Variables CS1100 Introduction to Programming Selection Statements Madhu Mutyam Department of Computer Science and Engineering Indian Institute of Technology Madras Course Material SD, SB,

More information

UIC. C Programming Primer. Bharathidasan University

UIC. C Programming Primer. Bharathidasan University C Programming Primer UIC C Programming Primer Bharathidasan University Contents Getting Started 02 Basic Concepts. 02 Variables, Data types and Constants...03 Control Statements and Loops 05 Expressions

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Flow Chart & Algorithms

Flow Chart & Algorithms Flow Chart Algorithms Planning Your Code Page 1 Page 4 See Appendix A, for Licensing Attribution information by-nc-sa-3.0 https://creativecommons.org/licenses/by-nc-sa/3.0/ https://creativecommons.org/faq/#what-does-some-rights-reserved-mean

More information

Chapter 13. Control Structures

Chapter 13. Control Structures Chapter 13 Control Structures Control Structures Conditional making a decision about which code to execute, based on evaluated expression if if-else switch Iteration executing code multiple times, ending

More information

Week 1 Introduction to Computer and Algorithm (Part1) UniMAP Sem II 11/12 DKT121: Basic Computer Programming 1

Week 1 Introduction to Computer and Algorithm (Part1) UniMAP Sem II 11/12 DKT121: Basic Computer Programming 1 Week 1 Introduction to Computer and Algorithm (Part1) UniMAP Sem II 11/12 DKT121: Basic Computer Programming 1 General Information Contributes 3 units: 2 hours lectures 2 hours labs and tutorials Main

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017 Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The

More information

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable CS 201 Selection Structures (2) and Repetition Debzani Deb Multiple-Alternative Decision Form of Nested if Nested if statements can become quite complex. If there are more than three alternatives and indentation

More information

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function

More information

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow Informatics Ingeniería en Electrónica y Automática Industrial Control flow V1.1 Autores Control Flow Statements in C language Introduction if-else switch while for do-while break continue return goto 2

More information

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu Physics 2660: Fundamentals of Scientific Computing Lecture 5 Instructor: Prof. Chris Neu (chris.neu@virginia.edu) Reminder I am back! HW04 due Thursday 22 Feb electronically by noon HW grades are coming.

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps 1 True and False in C False is represented by any zero value The int expression having the

More information

Name Roll No. Section

Name Roll No. Section Indian Institute of Technology, Kharagpur Computer Science and Engineering Department Class Test I, Autumn 2012-13 Programming & Data Structure (CS 11002) Full marks: 30 Feb 7, 2013 Time: 60 mins. Name

More information

Comments. Comments: /* This is a comment */

Comments. Comments: /* This is a comment */ Flow Control Comments Comments: /* This is a comment */ Use them! Comments should explain: special cases the use of functions (parameters, return values, purpose) special tricks or things that are not

More information

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution 1. (20 points, 5 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi CE 43 - Fall 97 Lecture 4 Input and Output Department of Computer Engineering Outline printf

More information

Lab Session # 3 Conditional Statements. ALQUDS University Department of Computer Engineering

Lab Session # 3 Conditional Statements. ALQUDS University Department of Computer Engineering 2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 3 Conditional Statements ALQUDS University Department of Computer Engineering Objective: Our objective for today s lab session is to introduce

More information

SELECTION STATEMENTS:

SELECTION STATEMENTS: UNIT-2 STATEMENTS A statement is a part of your program that can be executed. That is, a statement specifies an action. Statements generally contain expressions and end with a semicolon. Statements that

More information

Score score < score < score < 65 Score < 50

Score score < score < score < 65 Score < 50 What if we need to write a code segment to assign letter grades based on exam scores according to the following rules. Write this using if-only. How to use if-else correctly in this example? score Score

More information

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level

More information

IT 1033: Fundamentals of Programming Loops

IT 1033: Fundamentals of Programming Loops IT 1033: Fundamentals of Programming Loops Budditha Hettige Department of Computer Science Repetitions: Loops A loop is a sequence of instruction s that is continually repeated until a certain condition

More information

Course Outline Introduction to C-Programming

Course Outline Introduction to C-Programming ECE3411 Fall 2015 Lecture 1a. Course Outline Introduction to C-Programming Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut Email: {vandijk,

More information

Unit 7. Functions. Need of User Defined Functions

Unit 7. Functions. Need of User Defined Functions Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have

More information

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs PDS Lab Section 16 Autumn-2017 Tutorial 3 C Programming Constructs This flowchart shows how to find the roots of a Quadratic equation Ax 2 +Bx+C = 0 Start Input A,B,C x B 2 4AC False x If 0 True B x 2A

More information

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section : CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart ANS. Flowchart:- A diagrametic reperesentation of program is known as flowchart Symbols Q-2) Explain basic structure of c language

More information

Chapter 13 Control Structures

Chapter 13 Control Structures Chapter 13 Control Structures Highlights Sequence action_1 action_2 Conditional Iteration 13-2 Control Structures Conditional making a decision about which code to execute, based on evaluated expression

More information

Question Bank (SPA SEM II)

Question Bank (SPA SEM II) Question Bank (SPA SEM II) 1. Storage classes in C (Refer notes Page No 52) 2. Difference between function declaration and function definition (This question is solved in the note book). But solution is

More information

Lecture 10. Daily Puzzle

Lecture 10. Daily Puzzle Lecture 10 Daily Puzzle Imagine there is a ditch, 10 feet wide, which is far too wide to jump. Using only eight narrow planks, each no more than 9 feet long, construct a bridge across the ditch. Daily

More information

Lecture 5 Tao Wang 1

Lecture 5 Tao Wang 1 Lecture 5 Tao Wang 1 Objectives In this chapter, you will learn about: Selection criteria Relational operators Logical operators The if-else statement Nested if statements C++ for Engineers and Scientists,

More information

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Unit-II Programming and Problem Solving (BE1/4 CSE-2) Unit-II Programming and Problem Solving (BE1/4 CSE-2) Problem Solving: Algorithm: It is a part of the plan for the computer program. An algorithm is an effective procedure for solving a problem in a finite

More information

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

C++ Programming Lecture 7 Control Structure I (Repetition) Part I C++ Programming Lecture 7 Control Structure I (Repetition) Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department while Repetition Structure I Repetition structure Programmer

More information

Repetition Algorithms

Repetition Algorithms Repetition Algorithms Repetition Allows a program to execute a set of instructions over and over. The term loop is a synonym for a repetition statement. A Repetition Example Suppose that you have been

More information

DECISION CONTROL AND LOOPING STATEMENTS

DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of

More information

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A. Chapter 5 While For 1 / 54 Computer Science & Engineering 150A Problem Solving Using Computers Lecture 05 - s Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 While For 2 / 54 5.1 Repetition

More information

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops Computer Science & Engineering 150A Problem Solving Using Computers Lecture 05 - Loops Stephen Scott (Adapted from Christopher M. Bourke) 1 / 1 Fall 2009 cbourke@cse.unl.edu Chapter 5 5.1 Repetition in

More information

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26 Day06 A Young W. Lim 2017-09-20 Wed Young W. Lim Day06 A 2017-09-20 Wed 1 / 26 Outline 1 Based on 2 C Program Control Overview for, while, do... while break and continue Relational and Logical Operators

More information

Repetition Structures II

Repetition Structures II Lecture 9 Repetition Structures II For and do-while loops CptS 121 Summer 2016 Armen Abnousi Types of Control Structures Sequential All programs that we have written so far The statements inside a pair

More information

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso 1 Topics Multiple Selection vs Binary Selection switch Statement char data type and getchar( ) function Reading newline characters 2 So far, we have

More information

Programming for Engineers Iteration

Programming for Engineers Iteration Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

More information

Control Statements. If Statement if statement tests a particular condition

Control Statements. If Statement if statement tests a particular condition Control Statements Control Statements Define the way of flow in which the program statements should take place. Implement decisions and repetitions. There are four types of controls in C: Bi-directional

More information

EECE.2160: ECE Application Programming Spring 2018

EECE.2160: ECE Application Programming Spring 2018 EECE.2160: ECE Application Programming Spring 2018 1. (46 points) C input/output; operators Exam 1 Solution a. (13 points) Show the output of the short program below exactly as it will appear on the screen.

More information

The four laws of programs

The four laws of programs Statements 1 2 The four laws of programs 3 These are like Isaac Asimov's 4 laws of robotics: 0: programs must work properly 1: programs must be readable, provided this does not conflict with the previous

More information

PSEUDOCODE AND FLOWCHARTS. Introduction to Programming

PSEUDOCODE AND FLOWCHARTS. Introduction to Programming PSEUDOCODE AND FLOWCHARTS Introduction to Programming What s Pseudocode? Artificial and Informal language Helps programmers to plan an algorithm Similar to everyday English Not an actual programming language

More information

1 EEE1008 C Programming

1 EEE1008 C Programming 1 1 EEE1008 C Programming Dr. A.M. Koelmans 2 Administrative details Dr. A.M. Koelmans Location: Merz Court, room E4.14 Telephone: 8155 Web: www.staff.ncl.ac.uk/albert.koelmans/ E-mail: albert.koelmans@newcastle.ac.uk

More information

Lecture 5: C programming

Lecture 5: C programming CSCI-GA.1144-001 PAC II Lecture 5: C programming Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Brian Kernighan Dennis Ritchie In 1972 Dennis Ritchie at Bell Labs writes C and in 1978

More information

4*4*4 2. What is the output of the following flowchart for the values given below: (25 p)

4*4*4 2. What is the output of the following flowchart for the values given below: (25 p) Samples 1. Design a pseudocode that computes x n. Prompt the user to enter the value of x and n from keyboard. (25 p) Ex: Sample input for 4 and 3 your design should calculate 4 3 4*4*4 2. What is the

More information

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018 Computer Programing Class 02: 25 Jan 2018 [SCPY204] for Physicists Content: Data, Data type, program control, condition and loop, function and recursion, variable and scope Instructor: Puwis Amatyakul

More information

CpSc 1111 Lab 4 Formatting and Flow Control

CpSc 1111 Lab 4 Formatting and Flow Control CpSc 1111 Lab 4 Formatting and Flow Control Overview By the end of the lab, you will be able to: use fscanf() to accept a character input from the user and print out the ASCII decimal, octal, and hexadecimal

More information

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock) C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 By the end of this lecture, you will be able to identify the

More information

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 1 By the end of this lecture, you will be able to identify

More information

ALGORITHMS AND FLOWCHARTS

ALGORITHMS AND FLOWCHARTS ALGORITHMS AND FLOWCHARTS ALGORITHMS AND FLOWCHARTS A typical programming task can be divided into two phases: Problem solving phase produce an ordered sequence of steps that describe solution of problem

More information

Chapter 1: Problem Solving Skills Introduction to Programming GENG 200

Chapter 1: Problem Solving Skills Introduction to Programming GENG 200 Chapter 1: Problem Solving Skills Introduction to Programming GENG 200 Spring 2014, Prepared by Ali Abu Odeh 1 Table of Contents Fundamentals of Flowcharts 2 3 Flowchart with Conditions Flowchart with

More information