Introduction to C Programming CE00312-1 Lecture 3 Control Structures in C.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Lecture 2 Introduction to C Programming
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
C++ for Engineers and Scientists Third Edition
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Computer Science Department Relational Operators And Decisions.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Flow of Control Part 1: Selection
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
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.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Branching statements.
CNG 140 C Programming (Lecture set 3)
Decision Making in C.
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.
Decisions Chapter 4.
Chapter 4 (Conditional Statements)
Flow of Control.
CS1100 Computational Engineering
Selection (if-then-else)
Control Structures: Selection Statement
Control Structures: Selection Statement
CprE 185: Intro to Problem Solving (using C)
ICS103: Programming in C 4: Selection Structures
Presentation transcript:

Introduction to C Programming CE Lecture 3 Control Structures in C

Program Structure Modularity Atomisation of complex problem into manageable units – possibly reusable in other complex problems Functions Control structures Basic mechanics of process deconstruction Most problems can be expressed as combinations of Sequence Selection Repetition

Modularity in a C Program The Function main In C, all code is packaged in functions main is the function that the system calls when you run your program Every program must have one and only one main All functions may return a value Main statement Call function1 statement Call function 2 return Function 1 Function 2

Function Components A function definition consists of two parts; A Specification ( or header or interface); Specifies the function name, the return value and argument list A Body ( or block or implementation); Code to be executed when the function is called. A sequence of statements enclosed in braces

Declaring and using a function #include int sum(int,int); /*Function prototype */ int main(void) { int num1=4, num2=7; int total; total=sum(num1,num2); printf(“The sum of the numbers is %d\n”, total); return 0; } int sum(int n1, int n2) { /* Function to find the sum of two numbers */ int sumresult; sumresult=n1+n2; return sumresult; }

Program Control - Sequence Default control structure Program execution occurs sequentially unless otherwise directed Each step should be the logical consequence of the preceding one Statements should be arranged such that they occur in the right order

Program control - Selection Selection Implemented using If..  Single branch If..else..  Multiple branching  Ordinal relationship in branch determination Switch..case...  Multiple branching  Category branch determination

If Statements if (condition) statement; if (condition) statement1; else statement 2; where a statement can be a compound statement.

Conditions and Operators Conditions No boolean type; integer 0 == false, 1 == true e.g. 5>3 has the value 1 2>3 has the value 0 Relational Operators =, > if (x=1) printf(“OK”); if (x==1) printf(“OK”); LogicalOperators !NOT &&AND ||OR

Some Examples If the conditional value is non-zero then statement1 is executed, otherwise execution passes to next statement after if construct. e.g. 1 if (age 75) printf(“Error”); e.g. 2 if (gender !=’m’ && gender !=’f’) printf(“Error”); e.g. 3 if (x<0) x=-x;

IF.. cf IF.. ELSE If the conditional value is non-zero then statement1 is executed, otherwise statement2 is executed. e.g. 4 if (x=3) printf(“Yes”); else printf(“No”);

Compound statements e.g. 5 if (a<b) { temp=a; /* compound */ a=b; /* statement */ b=temp; }

Compound Statements e.g. 7 if (a>b) { max=a; flag=1; } else { max=b; flag=2; } e.g. 6 if (a>b) max=a; else max=b;

Validation Example #include int main(void) { int valid,age; char gender; scanf(“%d%c”,&age, &gender); valid=1; if (age =65) { valid=0; printf(“\nAGE ERROR\n”); } if (gender !=’m’ && gender !=’f’) { valid=0; printf(“\nGENDER ERROR\n”); } if (valid) printf(“\nData OK\n”); else printf(“\nData Error\n”); return 0; }

If.. Else Chains e.g. if (month==1) printf(”January”); else if (month==2) printf(“February”); else if (month==3) printf(“March”); else printf(“Error”);

Dangling else problem 1. if (x>0) if (y==’A’) printf(“Positive A”); else printf(“Negative”); Which condition determines the outcome? 2. if (x>0) { if (y==’A’) printf(“Positive A”); } else printf(“Negative”); A is only printed if x > and y is A

Switch Statement Provides a multi-way decision by testing whether an expression matches one of a number of constant values. General Form: switch (expression) { case c1:statements case c2:statements …. default:statements }

switch Statement expression is any integer valued expression c1, c2 are integer valued constants (remember integer include chars!) statements are any C statements default is optional c1, c2 must be unique (i.e same constant cannot appear twice) each case can have only one value (not range)

Example Switch Statement Classify an input character c as digit, white space or other. switch (c) { case ‘ ‘: case ‘\n’: case ‘\t’: nwhite=nwhite+1; break; case ‘0’: case ‘1’: case ‘2’: case ‘3’: case ‘4’: case ‘5’: case ‘6’: case ‘7’: case ‘8’: case ‘9’: ndigit=ndigit+1; break; default:nother=nother+1; }

Integer example switch switch (gradePoint) { case 1: printf (“You have failed badly”); break; case 3: printf(“A narrow fail\n”); case 2: printf(“Better luck at the resit”); break; case 4: case 5: case 6: case 7: case 8: printf(“Well done, you passed”); break; default: printf(“Not a valid grade point”); }

Switch after switch is evaluated control falls to matching case if no match, go to default if present terminate with break or by reaching end of switch block can group cases together by omitting break - careful! No need for {... } within case