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

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

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

Decision Making -Branching. Class Incharge: S. Sasirekha

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

mith College Computer Science CSC231 C Tutorials Fall 2017 Introduction to C Dominique Thiébaut

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

Flow Control. CSC215 Lecture

DECISION CONTROL AND LOOPING STATEMENTS

DECISION MAKING STATEMENTS

Crude Video Game Simulator Algorithm

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

F.E. Sem. II. Structured Programming Approach

Module 4: Decision-making and forming loops

& Technology. Expression? Statement-x. void main() int no; scanf("%d", &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements.

EE 121. Office suite. Lecture 3: Introduction to programming and C programming language

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

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

Introduction. C provides two styles of flow control:

Fundamental of Programming (C)

Fundamentals of Programming

Informática Ingeniería en Electrónica y Automática Industrial

Introduction to C Programming

F.E. Sem. II. Structured Programming Approach

Advanced Computer Programming

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

Following is the general form of a typical decision making structure found in most of the programming languages:

CSCE 531, Spring 2015 Final Exam Answer Key

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Programming and Data Structures

Chapter 5: Control Structures

Chapter 6. Section 6.4 Altering the Flow of Control. CS 50 Hathairat Rattanasook

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Decision Making and Loops

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

M1-R4: Programing and Problem Solving using C (JAN 2019)

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

n Group of statements that are executed repeatedly while some condition remains true

CHAPTER : 9 FLOW OF CONTROL

Review of the C Programming Language for Principles of Operating Systems

8. Control statements

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

mith College Computer Science CSC231 C Tutorials Fall 2017 Introduction to C Dominique Thiébaut

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

INTRODUCTION TO COMPUTER SCIENCE - LAB

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

Chapter 8 Statement-Level Control Structure

Low level programming (37-023)

MODULE 2: Branching and Looping

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Repetition Structures II

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

C - Basics, Bitwise Operator. Zhaoguo Wang

Programming for Engineers Iteration

C++ Program Flow Control: Selection

Introduction to Programming

CSCE 206: Structured Programming in C++

C Language, Token, Keywords, Constant, variable

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Chapter 3 Structured Program Development

Loops / Repetition Statements

Computers Programming Course 7. Iulian Năstac

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

CS1100 Introduction to Programming

Review of the C Programming Language

C: How to Program. Week /Mar/05

LESSON 6 FLOW OF CONTROL

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Structured Program Development

UIC. C Programming Primer. Bharathidasan University

Dept. of CSE, IIT KGP

Control Statements. If Statement if statement tests a particular condition

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

6. Control Statements II

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

CSC Java Programming, Fall Java Data Types and Control Constructs

The Design of C: A Rational Reconstruction: Part 2

CSE 307: Principles of Programming Languages

CSE 130 Introduction to Programming in C Control Flow Revisited

Continued from previous lecture

Lecture 5 Tao Wang 1

Chapter 2 - Introduction to C Programming

Fundamental of Programming (C)

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Problem Solving and 'C' Programming

Programming for Electrical and Computer Engineers. Loops

LECTURE 18. Control Flow

Chapter 4. Flow of Control

Introduction to C/C++ Lecture 3 - Program Flow Control

Chapter 8. Statement-Level Control Structures

BRANCHING if-else statements

Conditional Statement

Transcription:

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

Introduction Control flow statements specify the order in which computations are performed Different types Conditionals: Take a decision among two or more options depending on the evaluation of a condition. if else and switch Loops: Iterations of operations (with condition evaluation) for, while and do-while Jump: They change unconditionally the order of execution. continue, break, return and goto Labels: Used to identify lines in a program. case, default and «label:» 3

if-else statement (I) if (expr) stat1; else stat2; If expr is true then stat1 is processed If expr is false, stat2 is processed expr is true if its value is different than cero else stat2; is optional 4

if-else statement (II) stat1 and stat2 can be blocks of sentences between brackets if (expr) /* Block of sentences 1 */ } else /* Block of sentences 2 */ } Different if-else blocks can be grouped with brackets 5

if-else statement (III) if (expr1) if (expr2) if (expr3) stat31; else stat32; } else stat2; stat31 is processed if expr1, expr2 and expr3 are true stat32 is processed if expr1, expr2 are true and expr3 is false stat2 is processed if expr1 is false (without considering expr2 and expr3) 6

if-else statement (IV) Nested if-else statements Brackets determine priority among if and else Without brackets Each else is associated with the closest if Each block of statements is processed independently if (expr1) stat1; else if (expr2) stat2; else if (expr3) stat3;... else if (exprn) statn; else statn+1; statn is processed just if exprn is true statn+1 is processed just if none of the previous statements have been precessed 7

switch statement (I) switch (expr) case const-expr1: /* Statement block 1 */ break; case const-expr2: /* Statement block 2 */ break;... case const-exprn: /* Statement block N */ break; default: /* Statement block N+1 */ break; } 8

switch statement (II) switch is a multi-way decision test whether an expression matches a number of constant integers Brackets are needed case number is unlimited default is optional break causes an inmediate exit from the switch expr is evaluated and comparison with const-expr in each case starts If any matches, all statements are executed until a break or the end of the switch If none matches default statements are executed (if they exist) until a break or the end of the switch 9

switch statement (III) #include <stdio.h> int main () char grade = 'B'; switch(grade) case 'A' : printf("excellent!\n" ); break; case 'B' : case 'C' : printf("well done\n" ); break; case 'D' : printf("you passed\n" ); break; case 'F' : printf("better try again\n" ); break; default : printf("invalid grade\n" ); } } return 0; 10

while statement while (expr) stat; while (expr) stat; /* block of statements */ } If expr is true, stat is processed After execution expr is evaluated again If false, exit from the while WARNING: if expr doesn t change its value, an infinite loop can be created 11

for statement for (init_expr; cond_expr; update_expr) statement; for (init_expr; cond_expr; update_expr) statement; /* Statement block */ } init_expr is a expression that assign values to one or more variables cond_expr evaluates an expresion: if true statement is precessed. If false loop is finished update_expr are statements that are processed after statement. Typically update the value of the control variable Example: for (i=0; i<n; i++) printf( i= %d, i); 12

do-while statement do statement; while (expr); do statement ; /* Block of statements */ } while (expr); After executing statement, expr is evaluated, and, if true, statement is executed again. If expr is false, exit from the loop. WARING: If expr does not change its value within the loop, an infinite loop can be created. 13

break statement break allows to exit immediatly form the execution of statement switch, while, do-while, for, independently of any other condition. In nested loops, break exits just from the inner loop in which is placed. 14

continue statement continue forces a new iteration in the loop, ignoring the following statements until the end of the loop With while and do-while, jumps to condition evaluation With for, jumps to update and condition In nested loops, continue exits just applies to the inner loop where is placed 15

return statement return ends a function, returning control to the point of the program where it was called return expr The value of expr will be returned to the program It must be of the type declared in the function Function end braket «}» is equivalent to return without expr, and it is used with functions that does nor return any value (equivalent to return 0) 16

goto statement goto is an unconditional jump ABSOLUTELY NOT RECOMMENDED... label:... goto label;... «label:» is a line identifier. It can be in any part of the program 17