Control structures in C. Going beyond sequential
|
|
- Margery Dean
- 6 years ago
- Views:
Transcription
1 Control structures in C Going beyond sequential
2 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 Each executes exactly once If a function call occurs, the sequence is interrupted; when the function returns, the sequence resumes Step 1 Step 2 Call F Step 3 End (main) Start (F) F step 1 F step 2 End (F)
3 Altering flow of control Programming constructs can be employed to alter the flow of logic Two basic categories: Selection (aka branching) Iteration (aka looping) Both constructs rely on the evaluation of logical or relational expressions to determine which instruction(s) to execute
4 Quick review of logical expressions Logical, or boolean expressions, are expressions that evaluate to true or false In C, there is no boolean data type Int values used instead 0 means false Any other int value means true
5 Relational & Logical Operators in C Relational operators: >, <, >=, <= ==,!= Logical operators: &&!
6 Selection structures in C Simplest structure is plain if: if(expression evaluates true) { } Example: /* perform instruction(s) */ if ((x % 10) == 0) { } printf( %d is a multiple of 10\n, x);
7 Selection structures in C Important syntax notes: Always put parentheses around the expression to be tested in an if clause (it's a syntax error if you don't) Never put a semicolon at the end of the if clause Semicolon, all by itself, is a null (non-operational) statement if (expression); says if expression is true, do nothing Begin/end brackets ({}) are required if more than one statement is to be executed because of an if clause; if there is only one statement, they are optional
8 Something to watch out for what is output from this program? #include <stdio.h> int main() { int x = 0; if (x = 0) { printf("x is zero"); } return 0; }
9 Selection structures in C Perhaps the most versatile selection structure is the if/else structure In this structure, the if clause is followed by statement(s) to perform if the expression is true, and an else clause is followed by statement(s) to perform if the expression is false
10 The if/else structure: syntax if (expression) { } else { } /* statement(s) to perform if expression is true */ /* statement(s) to perform if expression is false */
11 #include <stdio.h> #include <stdlib.h> #include <time.h> Example int main() { srand(time(null)); int r = rand() % 20-10; if(r >= 0) printf("%d is positive\n", r); else printf("%d is negative\n", r); }
12 Compound selection (multiple branching) Each if and else clause is followed by one or more statements to execute We can place another if or if/else combination within the set of statements followed by either clause The result is a nested if or nested if/else structure
13 #include <stdio.h> #include <stdlib.h> Example int main() { srand(time(null)); int r = rand() % 3-1; if(r > 0) printf("%d is positive\n", r); else if (r < 0) printf("%d is negative\n", r); else printf("%d is zero\n", r); }
14 Notes on nested if/else An else clause never has its own condition; it is always the alternative to the nearest previous if clause If you want to test another condition, therefore, you need another if clause this was the case with the previous program: if(r > 0) printf("%d is positive\n", r); else if (r < 0) /* new condition, new if clause */
15 Matching ifs and elses An else clause always pairs with the closest if clause that doesn't already have a paired else Example if (A)... if (B)... if (C)... else /* pairs with condition C */... else /* pairs with condition B */... else /* pairs with condition A */...
16 Alternate branching structures: switch/case & the ternary operator Nested if/else structures are sufficient to cover any program you might want to write that involves logical selection There are a couple of alternative structures you should at least be aware of These are briefly discussed on the next 2 slides
17 The switch/case structure May be used instead of if/else when the condition being tested is in the form if x == y Instead of using the relational expression, we use the variable (x) as the switch, and the various possible values (y) as cases Instead of a final else, we can use a default case
18 The ternary operator Is so named because it takes 3 operands Syntax: Logic: expression? statement1 : statement2 If the expression is true, perform statement1 Otherwise, perform statement 2
19 Loops in C Selection structures, as the name implies, select certain statements to execute while skipping other statements Loops are structures that repeat a set of statements as long as a condition remains true
20 Loop control We can generally divide all loops into two categories: Count controlled loops are those that execute a set number of iterations Event controlled loops execute an indeterminate number of times; something external to the program (e.g. the user) decides how many times the loop will execute
21 Loop constructs in C The basic, most general type of loop is the while loop A loop specifically designed for count control is the for loop A loop that is guaranteed to give us at least one iteration regardless of the condition is a do/while loop We will spend most of our time with the first two
22 While loops Syntax: while (expression) { } /* statements to perform if expression is true */ /* statement or statements that update the condition */
23 While Statement while ( Expression ) { statement(s); } Expression is test for terminating condition Loop exit occurs when test succeeds (tests false) Loop entry is moment flow of control reaches 1st statement in loop body One iteration means one pass through the loop Even though actual value being tested changes inside loop body, exit does not occur until next time value is tested
24 Example int x = 0; // control variable initialization while (x < 100) // control variable test { printf ( x=%d\n, x); x ++; // control variable updated }
25 Another example int main() { int x = 10; while (x > 0) { printf( %2d\n, x); x--; } printf( Lift off!\n ); return 0; } Loop trace: Value of x Output (loop ends) Lift off!
26 int sum = 0, number = 1; while ( sum <= ) { } sum = sum + number; number = number + 1; Example When will the loop end? A.When number reaches 100 B.When sum equals C.When number exceeds 100 D.When sum exceeds
27 Example int product = 1, number = 1, count = 20, lastnumber; lastnumber = 2 * count - 1; When the loop concludes, what is the value of number? while (number <= lastnumber) { product = product * number; number = number + 2; }
28 Example: finding sum and average 100 numeric values need to be added together and averaged Using a while loop: read the 100 values find their total find their average
29 int main() { int thisnum; int total = 0; // initialize sum int count = 0; // initialize loop control while ( count < 100 ) // test expression { printf ( Enter value: ); scanf( %d, &thisnum); // read 1 value total = total + thisnum ; // add value to sum count++ ; // update loop control } } printf ( The sum of the values is: %d\n, total); printf ( The average is: %lf\n, (double)total/count); return 0;
30 Event controlled loop In the previous example, a loop was used to find the sum and average of 100 numbers Although it should work, it's pretty rigid User has to hang in there and type 100 values Only works for data sets of size 100 Using an event-controlled loop we can modify the program to be both more user-friendly and more versatile
31 Infinite loop Loop that never stops iterating Happens when terminating condition can t be reached Need to ensure that the control variable changes during body of loop 31
32 Examples of infinite loops /* loop body is null statement: */ int x=0; while (x < 10); /* loop body is single statement that doesn t update control variable */ int x=0; while (x < 10) printf( %d\n, x); x++; /* control variable update is incorrectly written */ int x=0; while (x < 10) { printf( %d\n, x); x--; } 32
33 For loops Count-controlled loops are so common in programming that most languages have a special set of syntax designed specifically for this construct In C, the special syntax for a countcontrolled loop is called a for loop 33
34 Count-controlled loops Count-controlled while loop: initialize counter before loop starts test counter in loop control expression update counter inside loop body A for loop is a stylized version of a countcontrolled while loop; all of the elements above appear at the top of the loop 34
35 For Loop Syntax for ( initialization ; test expression ; update ) { 0 or more statements to repeat } 35
36 The for loop contains an initialization an expression to test for continuing an update to execute after each iteration of the body 36
37 Count-controlled while loop example int x=0; /* step 1: initialize counter */ while (x < 100) /* step 2: test counter value */ { printf( I will be a good student\n ); x++; /* step 3: increment counter */ } 37
38 Same logic using for loop for (int x=0; x<100; x++) printf( I will be a good student\n ); Step 1: initialize loop counter (performed once) Step 2: test counter value (performed once for each iteration) Step 3: increment loop counter (performed once per iteration) Body of loop is performed between steps 2 and 3, just as in the while loop version 38
39 Example using decrement for (int count = 4 ; count > 0 ; count-- ) { printf( %d\n, count); } printf( Done\n ); OUTPUT: Done 39
40 Notes on for loop Just stylized version of while loop; condition test occurs before each iteration Can contain single statement (making brackets unnecessary) because increment occurs in loop heading Each section of loop heading can contain multiple parts, separated by commas; each section can also be omitted 40
41 What is output? int count=0; for (; count < 10 ; count++ ) { printf( ) ; } 41
42 What is output? int count=0; for (; count < 10 ; count++ ); { printf( ) ; } 42
43 1Potato 2Potato 3Potato 4 5Potato 6Potato 7Potato More Write a loop to produce the following output: 43
44 Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop body is performed first, then condition is tested A do-while loop is guaranteed to perform one iteration, because the validity of the condition is not known until after the iteration is complete 44
45 Syntax for do-while loop do { /* loop body statements */ }while (expression); Notes: do-while loops are almost always event-controlled Often not necessary to initialize loop control Good idea to keep end bracket and while(expression) on same line 45
46 Applications for do-while loops Often used to display an initial menu of choices, one of which is quit program you want user to see this at least once, even if the option they pick is to quit Can work well for file input Useful for checking validity of input value eliminates having to prompt twice for same data (see examples, next two slides) 46
47 Do-While Loop vs. While Loop POST-TEST loop (exitcondition) The looping condition is tested after executing the loop body. Loop body is always executed at least once. PRE-TEST loop (entrycondition) The looping condition is tested before executing the loop body. Loop body may not be executed at all. 47
48 Nested loops Just as a selection structure can be nested within another selection structure (or within a loop), a loop can also be nested When one loop is nested within another, each iteration of the outer loop contains several iterations of the inner loop 48
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 informationLoops. Repeat after me
Loops Repeat after me Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements repeat is determined by the value of a control variable,
More informationLoops. Repeat after me
Loops Repeat after me 1 Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements repeat is determined by the value of a control variable,
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 informationThere 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 informationFlow 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 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 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 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 informationLogic & program control part 3: Compound selection structures
Logic & program control part 3: Compound selection structures Multi-way selection Many algorithms involve several possible pathways to a solution A simple if/else structure provides two alternate paths;
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 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 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 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 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 informationRepetition and Loop Statements Chapter 5
Repetition and Loop Statements Chapter 5 1 Chapter Objectives To understand why repetition is an important control structure in programming To learn about loop control variables and the three steps needed
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 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 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 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 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 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 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 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 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 informationWhy Is Repetition Needed?
Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable
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: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures
More informationCSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York
CSc 10200 Introduc/on to Compu/ng Lecture 8 Edgardo Molina Fall 2011 City College of New York 18 The Null Statement Null statement Semicolon with nothing preceding it ; Do-nothing statement required for
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 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 informationOER on Loops in C Programming
OER on Loops in C Programming Prepared by GROUP ID 537 B. Bala Nagendra Prasad (bbalanagendraprasad@gmail.com C. Naga Swaroopa (swarupabaalu@gmail.com) L. Hari Krishna (lhkmaths@gmail.com) 1 Table of Contents
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 information204111: Computer and Programming
204111: Computer and Programming Week 4: Control Structures t Monchai Sopitkamon, Ph.D. Overview Types of control structures Using selection structure Using repetition structure Types of control ol structures
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.
More informationChapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?
Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct and use countercontrolled, sentinel-controlled,
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 informationControl Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.
EEE-117 COMPUTER PROGRAMMING Control Structures Conditional Statements Today s s Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical
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 informationUnit 7. 'while' Loops
1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment
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 information*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN
More informationL o o p s. for(initializing expression; control expression; step expression) { one or more statements }
L o o p s Objective #1: Explain the importance of loops in programs. In order to write a non trivial computer program, you almost always need to use one or more loops. Loops allow your program to repeat
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 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 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 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 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 informationChapter 4: Control structures. Repetition
Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition
More informationOperators And Expressions
Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication
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 informationCopyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1 Chapter 6 : (Control Structure- Repetition) Using Decrement or Increment While Loop Do-While Loop FOR Loop Nested Loop
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 informationChapter 2: Functions and Control Structures
Chapter 2: Functions and Control Structures TRUE/FALSE 1. A function definition contains the lines of code that make up a function. T PTS: 1 REF: 75 2. Functions are placed within parentheses that follow
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 informationExample. 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 informationChapter 4: Control structures
Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition
More informationCrude Video Game Simulator Algorithm
Crude Video Game Simulator Algorithm The following program will simulate free games at an arcade. The player will enter his/her score for 5 levels of play. The sum of these is their game score. If this
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 informationChapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition) 1 Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and
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 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 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 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 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 informationC: How to Program. Week /Apr/23
C: How to Program Week 9 2007/Apr/23 1 Review of Chapters 1~5 Chapter 1: Basic Concepts on Computer and Programming Chapter 2: printf and scanf (Relational Operators) keywords Chapter 3: if (if else )
More informationFlow 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 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 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 informationControl Statements Review CSC 123 Fall 2018 Howard Rosenthal
Control Statements Review CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Review some of Java s control structures Review how to control the flow of a program via selection mechanisms Understand if, if-else,
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 informationFunctions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.
Functions In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create
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 informationLoops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. The Need For Repetition (Loops) Writing out a simple counting program (1
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 informationChapter Goals. Contents LOOPS
CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common
More informationChapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.
Chapter 2 Flow of Control Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Boolean Expressions Building, Evaluating & Precedence Rules Branching Mechanisms if-else switch Nesting if-else
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 informationIntro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming
Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals
More informationIntroduction to the Java Basics: Control Flow Statements
Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared
More informationDecision 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 informationWriting to and reading from files
Writing to and reading from files printf() and scanf() are actually short-hand versions of more comprehensive functions, fprintf() and fscanf(). The difference is that fprintf() includes a file pointer
More informationFlow Control. Key Notion. Statement Categories. 28-Oct-10
Boaz Kantor Introduction to Computer Science, Fall semester 2010-2011 IDC Herzliya Flow Control Raistlin: This alters time. Astinus: This alters nothing...time flows on, undisturbed. Raistlin: And carries
More informationA Look Back at Arithmetic Operators: the Increment and Decrement
A Look Back at Arithmetic Operators: the Increment and Decrement Spring Semester 2016 Programming and Data Structure 27 Increment (++) and Decrement (--) Both of these are unary operators; they operate
More informationSchool of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15
Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL
More informationLoops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Application Of Loops In Actual Software Play again? Re-running specific
More informationAdvanced Computer Programming
Hazırlayan Yard. Doç. Dr. Mehmet Fidan WHILE, DO-WHILE and FOR LOOPS Loops are used for executing code blocks repeatedly. Decision of continuing loop is given by boolean expression. If boolean expression
More informationUNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING
( Word to PDF Converter - Unregistered ) http://www.word-to-pdf-converter.net FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING INTRODUCTION TO C UNIT IV Overview of C Constants, Variables and Data Types
More informationIntroduction to C/C++ Lecture 3 - Program Flow Control
Introduction to C/C++ Lecture 3 - Program Flow Control Rohit Sehgal Nishit Majithia Association of Computing Activities, Indian Institute of Technology,Kanpur rsehgal@cse.iitk.ac.in nishitm@cse.iitk.ac.in
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 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 informationReview of the C Programming Language for Principles of Operating Systems
Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights
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 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 informationFlow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya
Boaz Kantor Introduction to Computer Science, Fall semester 2010-2011 IDC Herzliya Flow Control Raistlin: This alters time. Astinus: This alters nothing...time flows on, undisturbed. Raistlin: And carries
More informationGeneral Information About Iteration (Loops)
General Information About Iteration (Loops) There are many ways of setting up a program to iterate/loop (re execute the same statements many times): Define a fixed number of iterations. Have the number
More informationB.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University
Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are
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 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 information