CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.

Slides:



Advertisements
Similar presentations
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.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 5: Loops and Files.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Chapter 4: Control Structures II
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 5: Structured Programming
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 5: Structured Programming
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 4 – C Program Control
Chapter 5: Structured Programming
Chapter 4 - Program Control
JavaScript: Control Statements.
Structured Program
Chapter 4 - Program Control
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Lecture 4: Selection Control Structure
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming

Objectives 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 prepared by NI, edited by MAF

Sequential Structure  Statements are executed one by one until the end of the program is reached.  A group of statements that executed sequentially which is usually grouped (bracketed) by { } is known as Compound Statement prepared by NI, edited by MAF

Sequential Structure - example int main(void) { int count = 0; printf(“Count = %d\n”, count); count++; printf(“Count = %d\n”, count); count++; printf(“Count = %d\n”, count); count++; printf(“Count = %d\n”, count); count++; printf(“Count = %d\n”, count); return 0; } prepared by NI, edited by MAF

Selection Structure  In selection structure, the program is executed based upon the given condition.  Only instructions that satisfy the given condition are executed.  There are 3 types of selection structure:  if One alternative  if…else Two alternatives  nested if..else Multiple alternatives  switch Multiple alternatives prepared by NI, edited by MAF

Selection structure: if  Syntax : if (condition) Statement;  The statement is only executed if the condition is satisfied.  Example: if (score >= 60) printf(“Pass!!\n”);  In the example above, the word “Pass!!” will only be printed out if score is larger than or equal to 60. If not, the word “Pass!!” will not be printed out and the program will continue with the next statement. prepared by NI, edited by MAF A condition is an expression that can return true or false (usually involving the use of an operator). Note that there is no semicolon (;) after the if statement. If there is one, that means the if statement and the printf() statement are 2 different statements and they will both get executed sequentially.

Selection structure: if… else  Syntax : if (condition) statement1; else statement2;  If the condition is satisfied, statement1 will be executed. Otherwise, statement2 will get executed.  Example : if (score >= 60) printf(“Pass!!\n”); else printf(“Fail!!\n”)  In the above example, the word “Pass!!” will be printed if the value of score is bigger than 60 or equal to 60. Otherwise the string ‘Fail!!” will be printed out prepared by NI, edited by MAF

Nested if… else statements  A nested if…else statement is an if…else statement with another if…else statements inside it.  Example : if (score >= 90) printf(“A\n”); else if (score >= 80) printf(“B\n”); else if (score >= 70) printf(“C\n”); else if (score >= 60) printf(“D\n”) else printf(“F\n”);  The else if statement means that if the above condition is not satisfied, then try checking this condition.If any one of the condition is already satisfied, then ignore the rest of the available conditions prepared by NI, edited by MAF

Plurality of Statements  In the examples that we have seen so far, there is only one statement to be executed after the if statement.  If we want to execute more than one statement after the condition is satisfied, we have to put curly braces { } around those statements to tell the compiler that they are a part of the if statement, making it a Compound Statement  Example if (score >= 90) { printf(“You have done very well\n”); printf(“I’ll give you a present\n”); } else if (score >= 60) { printf(“You have passed the course\n”); printf(“Sorry No present from for you\n”); printf(“Go and celebrate on your own\n”); } prepared by NI, edited by MAF

Selection structure: switch  A switch statement is used to choose one choice from multiple cases and one default case.  Syntax: switch (variable) { case case1: statement1; break; case case2: statement2; break; … default; statement; break; } prepared by NI, edited by MAF The break statement is needed so that once a case has been executed, it will skip all the other cases and go outside the switch statement. If the break statement is omitted, the execution will be carried out to the next alternatives until the next break statement is found.

switch - example int number; printf(“Enter a positive integer number: “); scanf(“%d”,&number); switch (number) { case 1: printf(“One!!\n”); break; case 2: printf(“Two!!\n”); break; case 3: printf(“Three!!\n”); break; default: printf(“Others\n”); break; } prepared by NI, edited by MAF This program reads a number from the user and print out the string equivalent for 1, 2 or 3. If the value being keyed in is other than 1,2 or 3, the default statement will be executed where the statement “Others” will be printed out.

Switch cont…  The value for ‘case’ must be integer or character constant.  Eg.1 switch (number) { case 1 : statement; break; ….  Eg.2 switch (color) { case ‘R’ : statement; break;  The order of the ‘case’ statement is unimportant prepared by NI, edited by MAF

Repetition Structure (Loop)  Used to execute a number of statements from the program more than one time without having to write the statements multiple times.  Two designs of loop :  To execute a number of instructions from the program for a finite, pre-determined number of time (Counter-controlled loop)  To execute a number of instructions from the program indifinitely until the user tells it to stop or a special condition is met (Sentinel-controlled loop)  There are 3 types of loops in C:  while  do…while  for prepared by NI, edited by MAF

Repetition : while loop  Syntax : while (condition) statement;  As long as the condition is met (the condition expression returns true), the statement inside the while loop will always get executed.  When the condition is no longer met (the condition expression returns false), the program will continue on with the next instruction (the one after the while loop).  Example: prepared by NI, edited by MAF int total = 0; while (total < 5) { printf(“Total = %d\n”, total); total++; } Similar as in the if statement, the condition is an expression that can return true or false.

Repetition : while loop cont…  In this example :  (total < 5) is known as loop repetition condition (counter-controlled)  total is the loop counter variable  In this case, this loop will keep on looping until the counter variable is = 4. Once total = 5, the loop will terminate prepared by NI, edited by MAF

Repetition : while loop cont…  The printf() statement will get executed as long as the variable total is less than 5. Since the variable total is incremented each time the loop is executed, the loop will stop after the 5th output. Output: Total = 0 Total = 1 Total = 2 Total = 3 Total = 4 prepared by NI, edited by MAF

Infinite loop  If somehow the program never goes out of the loop, the program is said to be stuck in an infinite loop.  The infinite loop error happens because the condition expression of the while loop always return a true.  If an infinite loop occurs, the program would never terminate and the user would have to terminate the program by force. prepared by NI, edited by MAF

Repetition : do… while loop  Syntax do { statement; } while(condition);  A do…while loop is pretty much the same as the while loop except that the condition is checked after the first execution of the statement has been made.  When there is a do…while loop, the statement(s) inside it will be executed once no matter what. Only after that the condition will be checked to decide whether the loop should be executed again or just continue with the rest of the program. prepared by NI, edited by MAF

do… while loop cont…  Let us consider the following program: int total = 10; while (total < 10) { printf(“Total = %d\n”, total); total++; } printf(“Bye..”);  What does this program do? The program will only print the word “Bye..”. The statements inside the while loop will never be executed since the condition is already not satisfied when it is time for the while loop to get executed. prepared by NI, edited by MAF

do… while loop cont…  Now consider the following program: int total = 10; do { printf(“Total = %d\n, total); total++; } while (total < 10) printf(“Bye..”);  Compared to the previous one, what will the output be? The program will get an output: Total = 10 Bye.. because the condition is not checked at the beginning of the loop. Therefore the statements inside the loop get executed once. prepared by NI, edited by MAF

Repetition : for loop  Syntax : for (expression1; expression2; expression3) statement;  Expression1: initialize the controlling variable  Expression2: the loop condition  Expression3: changes that would be done to the controlling variable at the end of each loop.  Note that each expression is separated by a semicolon (;) prepared by NI, edited by MAF

for loop - example  Example: int total; for (total = 0; total < 5; total++) printf(“Total = %d\n”, total);  Output: Total = 0 Total = 1 Total = 2 Total = 3 Total = 4 prepared by NI, edited by MAF

for loop cont…  Notice that the output is the same as the one for the while loop example. In fact, the two examples are exactly equivalent. Using a for loop is just another way of writing a while loop that uses a controlling variable.  It is also possible to omit one or more of the for loop expressions. In such a case, we just put the semicolon without the expression. int total= 0; for (; total < 5; total++) printf(“Total = %d\n”, total); prepared by NI, edited by MAF

continue and break statement  Both of these statements are used to modify the program flow when a selection structure or a repetition structure is used.  The break statement is used to break out of selection or repetition structure. For example: for (a = 0; a < 5; a++) { if (a == 2) break; printf(“a = %d\n”, a); }  The output of this example would be: a = 0 a = 1 prepared by NI, edited by MAF

continue and break statement  When a = 2, the program will break out of the for loop due to the break statement. This will effectively terminate the loop. It will not wait till the value of a reaches 5 before terminating the loop.  The continue statement is used to ignore the rest of the statements in the loop and continue with the next loop. prepared by NI, edited by MAF

continue and break statement  Example: for (a = 0; a < 5; a++) { if (a == 2) continue; printf(“a = %d\n”, a); }  Output: a = 0 a = 1 a = 3 a = 4 prepared by NI, edited by MAF

continue and break statement  a = 2 is not printed out because the loop skips the printf() function when the continue statement is encountered.  In a while and do…while structure, the loop condition will be checked as soon as the continue statement is encountered to determine whether the loop will be continued.  In a for loop, any modification to the controlling variable will be done before the condition is checked. prepared by NI, edited by MAF

SUMMARY  In this chapter, you’ve learnt about 3 control structures in C programming :  Sequential  Selection if..else nested if..else switch  Repetition while do…while for  Two designs of repetition :  Counter-controlled  Sentinel-controlled  The use of continue and break statement prepared by NI, edited by MAF