There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.
|
|
- Shannon Boyd
- 5 years ago
- Views:
Transcription
1 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 for very simple programs. There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. For example, we might want to execute a group of statements only if a particular condition is true, and skip them otherwise. Or, we might want to execute a group of statements again and again. The C language provides statements, which we call control-flow statements, that specify the order of execution of statements in the program.
2 Objectives: at the end of this module, the student should be able to: 1. explain the purposes of the sequence, selection and iteration control-flow structures; 2. use the if and switch statements; 3. use the while, do-while, and for statements; 4. differentiate the various selection and iteration statements from each other.
3 - the order in which statements are executed in a program. 3 Types of Control Flow Structures: 1. Sequence 2. Selection 3. Iteration
4 Sequence control refers mainly to the sequential execution of statements. This type of control corresponds to the following flowchart: Statement 1 Statement 2 Statement 3 Figure 4.1 Sequence Control Flow
5 Translated into a C program, the statements are ordered successively, statement1; statement2; statement3; As you can see, we have used this type of control flow in all the sample programs that we discussed in the previous module.
6 Selection control allows the program to choose which statement to execute next based on some condition. C provides two such statements to achieve this: 1. the if-else statement and 2. the switch statement.
7 The if-else statement functions the same way as the diamond symbol does in flowcharts. It evaluates an expression (or condition) and chooses the next statement to perform based on the truth value of the expression. The format of the if-else statement is if (expression) statement1; else statement2; where the else part is optional.
8 The if-else statement is equivalent to the following flowchart: true expression false Statement 1 Statement 2 Fig 4.2 Flowchart for If-else statement
9 The if-else statement first evaluates expression. If it is true, statement1 is executed. Otherwise, statement2 is executed. Notice that statement2 is not executed when expression is true, and statement1 is not executed when expression is false.
10 #include <stdio.h> int x; main() { printf( Input an integer : ); /*prompt*/ scanf( %d, &x); /*read input, store in x*/ if (x < 0) { /*evaluate expression*/ printf( negative number\n ); } else { printf( nonnegative number\n ); } } we used the if-else statement to control which printf statement to execute.
11 Recall: we said the else part of the If-else statement is optional. So, we can write the statement as: if (expression) statement1; In this case, expression is evaluated and if it s true, statement1 is executed. Otherwise, nothing is executed and the program merely proceeds to the next statement in the program.
12 The If statement is equivalent to the following flowchart: true expression false Statement 1 Fig 4.3 Flowchart of If statement So, if we omit the else part in our previous example, no message is outputted when the input value is greater than or equal to zero (that is, x >= 0).
13 if (expression) { statement1a; statement2a; statementna; } else { statement1b; statement2b;. statementmb; }
14 true expression false Statement 1a Statement 1b Statement 2a Statement 2b Statement Na Statement Mb Fig. 4.4 If-else with compound statements
15 The group of statements enclosed in braces is called a compound statement. In C, a compound statement can always take the place of a single statement. So in the if-else statement if (expression) statement1; else statement2; we can put a compound statement in place of statement1. The same is true for statement2.
16 This results to if-else statements that have the following structures: if (expression) statement1; else { statement; statement; statement; } if (expression) { statement; statement; statement; } else statement2;
17 below is a program code which has a compound statement: if (b == 0) printf( c is undefined\n ); else { c = a/b; printf( c = %d\n, c); }
18 The braces and the statements may be placed differently as long as their sequence is not altered. It can be equally written as if (b == 0) printf( c is undefined\n ); else { c = a/b; printf( c = %d\n, c); }
19 In the program, a compound statement is placed in the else part. If expression (b==0) is true, the statement printf( c is undefined\n ); is executed. Otherwise, statements c = a/b; and printf( c = %d\n, c); are executed instead.
20 Let s take a look again at the format of the ifelse statement: if (expression) statement1; else statement2; Actually, it s possible for statement1 (or statement2) to be another if-else statement. In such a case, an if-else statement is specified within another if-else statement.
21 if (x!= 0) if (y!= 0) { a = x/y; z = a + b; } else z = LONG_MAX; else z = b;
22 In the program above, the statements a = x/y; and z = a + b; are executed only if expressions (x!= 0) and (y!= 0) are both true. If (x!= 0) is true but (y!= 0) is false, the statement z = LONG_MAX; is executed instead. As we mentioned earlier, the else part is optional. Omitting the else part of the inner if-else statement, however, can result to ambiguity because there will be more if s than else s, and the else part can possibly be meant to belong to any one of the if s. The C compiler resolves this ambiguity by associating the else with the closest previous else-less if.
23 The nesting of the if-else statements can be extended to several levels. For example, if (expression1) if (expression2) { if (expression3) if (expression4) statement1; else statement2; } else statement3; else statement4; has the if-else statements nested up to four levels. Note how the braces are placed. In this program, statement1 is executed only if all four expressions are true.
24 The if-else statement provides a two-way decision the next statement is chosen between two alternatives. The switch statement, on the other hand, provides a multi-way decision. The next statement is chosen among several alternatives.
25 format of the switch statement is switch (expr) { case const1: statement1a; statement1b; case const2: statement2a; case const3: statement3a; default: statement4a; }
26 expr is an expression of type integer, and const1, const2 and const3 are constant integer values (that is, not variables). The expression is evaluated and compared to several constant integer values. When a match is found, the program starts execution at the corresponding statement. By the way, the default part is optional and may be omitted. The switch statement is best understood by looking at its equivalent flowchart shown in Figure 4.5.
27 expr=const 1? T F Statement 1a; Fig. 4.5 Flowchart for Switch Statement expr=const 2? T Statement 1b; F Statement 2a; expr=const 3? T F Statement 3a; Statement 4a;
28 char ch; float grade; switch (ch) { case A : grade = 4.0; break; case B : grade = 3.0; break; case C : grade = 2.0; break; case D : grade = 1.0; break; default: grade = 0.0; } grade is assigned the value 4.0 when ch is A, 3.0 when ch is B, 2.0 when ch is C, 1.0 when ch is D, and 0.0 otherwise. Now, what do you think would happen if we remove all the break statements? grade will be 0.0 no matter what ch is, because the last statement always gets executed.
29 Iteration - refers to the repeated execution of program statement. - often referred to as loops. C provides three statements to perform iteration: 1) the while statement, 2) the do-while statement, and 3) the for statement.
30 The while statement has the format: while (expression) statement; It repeatedly executes statement while expression is true. It is equivalent to the flowchart in Figure 4.6.
31 expression F T Statement 1a; Fig. 4.6 Flowchart for While Statement
32 The expression is first evaluated. If true, statement is executed and expression is reevaluated. statement is repeatedly executed until expression becomes false, which causes the loop to exit. In the while loop, statement can be a compound statement. Thus we have while (expression) { statement; statement; } which executes several statements repeatedly.
33 int n; n = 0; while (n < 5) { printf( %d\n, n); n++; } gives the output A loop that does not terminate is called an infinite loop.
34 Another example: char ch; printf( continue? (y/n) ); scanf( %c, &ch); while (ch!= n ) { printf( continue? (y/n) ); scanf( %c, &ch); } Can you tell what it does? It keeps on prompting for a character input and it terminates when character y is inputted. When run, the output might look like continue? (y/n) y continue? (y/n) y continue? (y/n) n
35 The do-while statement is quite similar to the while statement. Both continue to iterate as long as an expression is true. The format of do-while is do statement; while (expression); where statement is repeatedly executed while expression is true.
36 The difference between the do-while statement and the while statement is that in do-while, expression is evaluated after executing statement. In while, expression is evaluated first before executing statement. This means that in do-while, statement is executed at least once, while in while, statement may not be executed at all.
37 n = 0; do { printf( %d\n, n); n++; } while (N < 5); The do-while version of the prompting program previously given is char ch; do { printf( continue? (y/n) ); scanf( %c, &ch); } while (ch!= n )
38 Another loop C provides is the for loop. But it has a much different structure than the while and do-while statements. Its format is: for (init; cond; incr) statement; init, cond and incr stands for initialization, condition, and increment, respectively.
39 The for statement above is equivalent to init; while (cond) { statement; incr; } init is first executed and then cond is evaluated. If it is true, statement is executed, followed by incr. Then, cond is again evaluated. The loop exits when cond becomes false.
40 Thus, given a while statement, the equivalent for statement can be easily obtained by identifying which is init, cond, statement and incr. While statement: n = 0; /* init */ while (n < 5) { /* cond */ printf( %d\n, n); /* statement */ n++; /* incr */ }
41 This can be directly transformed into a for loop that does the same thing output numbers 0 to 4. The for loop is obtained by simply plugging in the components that we identified above. The equivalent for loop: for (n = 0; n < 5; n++) printf( %d\n, n); Init and incr may consist of several statements separated by commas. For example, for (n = 0, m = 1; n < 5; n++, m++) printf( %d\n, m+n); has two statements (n = 0; and m = 1;) in the initialization part and two statements (n++; and m++;) in the increment part.
42 The initialization, condition and increment parts may be omitted, although the semicolons separating these parts must remain. If init and incr are omitted, no initialization and increment is done. If cond is omitted, it is taken as permanently true. Thus, for (n = 0; ;n++ ) printf( %d\n, n); outputs the numbers 0, 1, 2, 3, 4, until infinity
43 C provides two statements that are sometimes used to control the execution of loops. These are: 1) break statements. 2) continue statements. The break statement causes an immediate exit from the loop.
44 For example, consider again the while loop that outputs integers 0 to 4: int n; n = 0; while (n < 5) { printf( %d\n, n); n++; } For illustration purposes, let s insert an if statement that executes break, such that:
45 int n; n = 0; while (n < 5) { if (n == 3) break; printf( %d\n, n); n++; } This causes the loop to exit (or terminate) when n is 3. As a result, the loop only outputs integers 0 to 2.
46 The continue statement, is used to skip an iteration (that is, to ignore executing the rest of the loop). For example, suppose that we replace the break statement above with a compound statement that executes continue: int n; n = 0; while (n < 5) { if (n == 3) { n++; continue; } printf( %d\n, n); n++; }
47 When n is 3, it is incremented and then the continue statement causes the program control to ignore the rest of the loop (the printf and n++) and jump back to test the loop condition (n < 5). This means that 3 is not outputted since the printf statement is ignored when n is 3. The output of the program is thus:
48 Let s have a more useful example. Suppose that we would like to prompt the user to enter a sequence of positive integers. We can design the program such that it ignores negative values and it stops reading input when a zero is entered.
49 int x, sum = 0; printf( input a sequence of positive integers: ); while (sum == sum) { scanf( %d, &x); if (x == 0) break; if (x < 0) continue; sum = sum + x; } printf( sum of positive integers = %d\n, sum);
50 The loop condition (sum == sum) is always true since sum is always equal to itself. Inside the loop, scanf reads an integer and stores it in x. If x is 0, the loop immediately exits. If it s a negative integer, the rest of the loop is ignored (the negative value is not added to sum) and the next number is read. The loop continues until a zero value causes it to exit. Note that even though the loop condition is always true, it s not an infinite loop because of the break statement inside.
51 Control-flow structures are statements that determine the order of execution of statements in a program. In C, control-flow structures are of three types: sequence, selection, and iteration. Sequence control is what is normally followed when statements are written sequentially. Selection control provides a way to select the next statement among a number of alternatives. The if-else and switch statements are used for this type of control-flow. Iteration control refers to the repetitive execution of statements. There are three C statements that provide iteration: the while statement, the do-while statement, and the for statement.
Flow Control. CSC215 Lecture
Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements
More informationn Group of statements that are executed repeatedly while some condition remains true
Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,
More informationV2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed
Programming in C 1 Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control
More informationFlow of Control. Flow of control The order in which statements are executed. Transfer of control
1 Programming in C Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control
More informationIntroduction. 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 informationREPETITION 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 informationChapter 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 informationLecture 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 informationDept. of CSE, IIT KGP
Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of
More informationPDS 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 informationINTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from
INTRODUCTION TO C++ PROGRAM CONTROL Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Repetition Statement for while do.. while break and continue
More informationModule 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 informationStructured 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 informationChapter 4. Flow of Control
Chapter 4. Flow of Control Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr Sequential flow of control
More informationStructured Programming. Dr. Mohamed Khedr Lecture 9
Structured Programming Dr. Mohamed Khedr http://webmail.aast.edu/~khedr 1 Two Types of Loops count controlled loops repeat a specified number of times event-controlled loops some condition within the loop
More informationProgramming for Electrical and Computer Engineers. Loops
Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly
More informationUnit 3 Decision making, Looping and Arrays
Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.
More informationIntroduction 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 informationMODULE 2: Branching and Looping
MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written
More informationCOMP 208 Computers in Engineering
COMP 208 Computers in Engineering Lecture 14 Jun Wang School of Computer Science McGill University Fall 2007 COMP 208 - Lecture 14 1 Review: basics of C C is case sensitive 2 types of comments: /* */,
More information3 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 informationITC213: 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 informationComputers 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 informationChapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.
Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop
More informationChapter 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 informationLecture 7 Tao Wang 1
Lecture 7 Tao Wang 1 Objectives In this chapter, you will learn about: Interactive loop break and continue do-while for loop Common programming errors Scientists, Third Edition 2 while Loops while statement
More informationDecision 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 informationSudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur
Programming and Data Structure Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur Shortcuts in Assignment Statements A+=C A=A+C A-=B A=A-B A*=D A=A*D A/=E
More informationChapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationC++ 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 informationAll copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva
All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..
More informationInformation Science 1
Information Science 1 Fundamental Programming Constructs (1) Week 11 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 10 l Flow of control
More informationProblem Solving and 'C' Programming
Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 05: Selection and Control Structures 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained herein
More informationLecture 05 I/O statements Printf, Scanf Simple statements, Compound statements
Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple
More informationLecture 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 informationC 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 information15 FUNCTIONS IN C 15.1 INTRODUCTION
15 FUNCTIONS IN C 15.1 INTRODUCTION In the earlier lessons we have already seen that C supports the use of library functions, which are used to carry out a number of commonly used operations or calculations.
More informationCpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting
CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting Your factors.c and multtable.c files are due by Wednesday, 11:59 pm, to be submitted on the SoC handin page at http://handin.cs.clemson.edu.
More informationECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control
ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 4: Flow of Control Outline of this Lecture Examples of Statements in C Conditional Statements The if-else Conditional Statement
More informationLecture 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 informationProgramming 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 informationDECISION 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 informationControl structures in C. Going beyond sequential
Control structures in C Going beyond sequential Flow of control in a program Start (main) Program starts with the first statement in main Instructions proceed sequentially: One at a time Top to bottom
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #17 Loops: Break Statement (Refer Slide Time: 00:07) In this session we will see one more feature that is present
More information공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE
공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE LOOPS WHILE AND FOR while syntax while (expression) statement The expression is evaluated. If it is
More informationChapter 4 C Program Control
1 Chapter 4 C Program Control Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 4 C Program Control Outline 4.1 Introduction 4.2 The Essentials of Repetition
More informationInformation Science 1
Topics covered Information Science 1 Fundamental Programming Constructs (1) Week 11 Terms and concepts from Week 10 Flow of control and conditional statements Selection structures if statement switch statement
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 5: Control Structures II (Repetition) Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input,
More informationTheory 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 informationC++ 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 informationOverview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University
Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the
More informationControl Structures. Lecture 4 COP 3014 Fall September 18, 2017
Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls
More informationControl 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 informationChapter 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 informationFundamentals of Computer Programming Using C
CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar Faculty Name: Ami D. Trivedi Class: FYBCA Subject: US01CBCA01 (Fundamentals of Computer Programming Using C) *UNIT 3 (Structured Programming, Library Functions
More informationCOP 2000 Introduction to Computer Programming Mid-Term Exam Review
he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can
More informationSELECTION 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 informationFundamental 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 informationStatements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14
Statements Control Flow Statements Based on slides from K. N. King Bryn Mawr College CS246 Programming Paradigm So far, we ve used return statements and expression statements. Most of C s remaining statements
More informationRepetition 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 informationC: 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بسم اهلل الرمحن الرحيم
بسم اهلل الرمحن الرحيم Fundamentals of Programming C Session # 10 By: Saeed Haratian Fall 2015 Outlines Examples Using the for Statement switch Multiple-Selection Statement do while Repetition Statement
More informationFundamentals 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 informationRepetition Structures
Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements
More informationCondition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.
Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes
More informationCSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1
CSE123 LECTURE 3-1 Program Design and Control Structures Repetitions (Loops) 1-1 The Essentials of Repetition Loop Group of instructions computer executes repeatedly while some condition remains true Counter-controlled
More informationProblem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur
Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 18 Switch Statement (Contd.) And Introduction to
More informationComputer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons
Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite
More informationLoops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input
Loops / Repetition Statements Repetition s allow us to execute a multiple times Often they are referred to as loops C has three kinds of repetition s: the while loop the for loop the do loop The programmer
More informationStructured 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 informationLECTURE 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 informationChapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++
Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure
More informationOther Loop Options EXAMPLE
C++ 14 By EXAMPLE Other Loop Options Now that you have mastered the looping constructs, you should learn some loop-related statements. This chapter teaches the concepts of timing loops, which enable you
More informationChapter 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 informationProgramming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029
Programming Basics and Practice GEDB029 Decision Making, Branching and Looping Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Decision Making and Branching C language possesses such decision-making capabilities
More informationLoops / Repetition Statements
Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for
More informationSTUDENT LESSON A12 Iterations
STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of
More informationCP 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 informationChapter 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 informationDay06 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 informationProblem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur
Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 15 Branching : IF ELSE Statement We are looking
More informationThe following expression causes a divide by zero error:
Chapter 2 - Test Questions These test questions are true-false, fill in the blank, multiple choice, and free form questions that may require code. The multiple choice questions may have more than one correct
More informationIslamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.
Islamic University of Gaza Computer Engineering Dept. C++ Programming For Industrial And Electrical Engineering By Instructor: Ruba A. Salamh Chapter Four: Loops 2 Chapter Goals To implement while, for
More informationDecision making with if Statement : - Control Statements. Introduction: -
Control Statements Introduction: - Any C program if you consider, the set of statements are normally executed sequentially in the order in which they are written, and such programs have sequential structure
More informationSequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;
Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The
More informationWeek 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.
Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than
More informationChapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Copyright 2011 Pearson Addison-Wesley. All rights reserved.
More informationInformatics 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 information5. Selection: If and Switch Controls
Computer Science I CS 135 5. Selection: If and Switch Controls René Doursat Department of Computer Science & Engineering University of Nevada, Reno Fall 2005 Computer Science I CS 135 0. Course Presentation
More informationECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C
ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C School of Electrical and Computer Engineering Cornell University revision: 2018-09-03-15-59 1 Statements, Syntax, Semantics, State
More information5. Control Statements
5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements
More informationSTUDENT 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 informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 5 Structured Program Development Department of Computer Engineering How to develop
More informationLoops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while
More informationLoops! 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 informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationObjectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators
Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)
More informationControl Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003
Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an
More informationComments. 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 informationFundamentals of Programming Session 9
Fundamentals of Programming Session 9 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines
More information