Flow Control. CSC215 Lecture

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

Decision Making in C

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

8. Control statements

Introduction. C provides two styles of flow control:

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

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

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

Module 4: Decision-making and forming loops

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

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

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

C-LANGUAGE CURRICULAM

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Chapter 4. Flow of Control

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

Problem Solving and 'C' Programming

The following expression causes a divide by zero error:

Computers Programming Course 7. Iulian Năstac

Unit 3 Decision making, Looping and Arrays

LESSON 6 FLOW OF CONTROL

REPETITION CONTROL STRUCTURE LOGO

Quick Reference Guide

Part I Part 1 Expressions

MODULE 2: Branching and Looping

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Operators And Expressions

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

COMP 208 Computers in Engineering

CS313D: ADVANCED PROGRAMMING LANGUAGE

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

CHAPTER 9 FLOW OF CONTROL

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

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017


CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Programming for Electrical and Computer Engineers. Loops

Programming and Data Structures

3 The L oop Control Structure

LECTURE 18. Control Flow

Lecture 6. Statements

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

Type Conversion. and. Statements

CHAPTER : 9 FLOW OF CONTROL

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

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

C++ Programming: From Problem Analysis to Program Design, Third Edition

OER on Loops in C Programming

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Fundamental of Programming (C)

Lecture 7 Tao Wang 1

Data Types and Variables in C language

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Introduction to C Programming

Chapter 2 Control in Programming. lecture 2 1

Programming for Engineers Iteration

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

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

5. Control Statements

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Operators. Java operators are classified into three categories:

Control structures in C. Going beyond sequential

Lecture 3. More About C

C: How to Program. Week /Mar/05

Software Design & Programming I

Lecture 5 Tao Wang 1

SELECTION STATEMENTS:

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

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

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Quick Reference Guide

Variables and literals

Prepared by: Shraddha Modi

Iteration. Side effects

Technical Questions. Q 1) What are the key features in C programming language?

DECISION CONTROL AND LOOPING STATEMENTS

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Chapter 4: Making Decisions

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

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

COMS W4115 Programming Languages & Translators GIRAPHE. Language Reference Manual

Chapter 2 - Introduction to C Programming

Lecture 2: C Programming Basic

Model Viva Questions for Programming in C lab

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Accelerating Information Technology Innovation

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Transcription:

Flow Control CSC215 Lecture

Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements for - statement while - statement do-while - statement Nested repetitive statements Break and continue statements Unconditional jump: goto

Blocks and Compound Statements A simple statement ends in a semicolon: z = foo(x+y); Consider the multiple statements: temp = x+y ; z = foo (temp) ; Curly braces combine into compound statement/block Block can substitute for simple statement Compiled as a single unit Variables can be declared inside No semicolon at end { int temp = x+y; z = foo(temp); } Block can be empty {}

Blocks and Compound Statements Blocks nested inside each other { int temp = x+y ; z = foo ( temp ) ; { float temp2 = x y ; z += bar ( temp2 ) ; } } Variables declared inside a block are only visibly within this block and its internatl blocks

Conditional Statements if - Statement if-else - Statement switch - Statement? : Ternary operator No boolean type in ANSI C introduced in C99 Relational and logical expressions are evaluated to: 1 if they are logically true 0 if they are logically false Numeric expressions are considered false if they are evaluated to integer 0 Pointer expressions are considered false if they are evaluated to null

if- Statement Syntax: Example: if (<condition>) <statement>; if ( x % 2 == 0) y += x / 2 ; Evaluate condition: (x % 2 == 0) If true, execute inner statement: y += x/2; Otherwise, do nothing Inner statements may be block

if-else - Statement Syntax: if (<condition>) <statement1>; else <statement2>; Example: if ( x % 2 == 0) y += x / 2 ; else y += ( x + 1 ) / 2; Evaluate condition: (x % 2 == 0) If true, execute first statement: y += x/2; Otherwise, execute second statement: y += ( x + 1 ) / 2; Either inner statements may be block

Nesting if/if-else Statements Can have additional alternative control paths by nesting if statements: if (<condition>) <statement1>; /* can be an if or if-else statement*/ else <statement2>; /* can be an if or if-else statement*/ Conditions are evaluated in order until one is met; inner statement then executed if multiple conditions true, only first executed Example: if ( x % 2 == 0) y += x / 2 ; else if ( x % 4 == 1) y += 2 (( x + 3 )/ 4 ); else y += ( x +1 )/ 2 ;

Nesting if/if-else Statements Dangling else, example: if ( x % 4 == 0) if ( x % 2 == 0) y = 2; else y = 1; if ( x % 4 == 0) if ( x % 2 == 0) y = 2; else y = 1; if ( x % 4 == 0) if ( x % 2 == 0) y = 2; else y = 1; To which if statement does the else keyword belong? Belongs to the nearest if in the same block To associate else with outer if statement: use braces if ( x % 4 == 0) { if ( x % 2 == 0) y = 2; } else y = 1;

switch - Statement Syntax: switch (<int or char expression>) { case <literal1>: <statements> [break;] [more cases] [default: <statements>] } Provides multiple paths Case labels: different entry points into block Compares evaluated expression to each case in order: When match found, starts executing inner code until break; reached Execution falls through if break; is not included

switch - Statement Example: switch ( ch ) { case Y : / ch == Y / / do something / break ; case N : / ch == N / / do something else / break ; default : / otherwise / / do a third thing / }

Loops (Iterative Statements) while - loop for - loop do-while - loop break and continue keywords

Loops: while - Statement Syntax: while ( <condition> ) <loop body> Simplest loop structure evaluate body as long as condition is true Condition evaluated first, so body may never be executed Example:

Loops: for - Statement Syntax: for ([<initialization>];[<condition>];[<modification>]) <loop body> Example: int i, j = 1; for ( i = 1; i <= n ; i ++) j = i ; printf( %d\n, j); A counting loop Inside parentheses, three expressions, separated by semicolons: Initialization: i = 1 Condition: i <= n Modification: i++

Loops: for - Statement Any expression can be empty (condition assumed to be true ): for (;;) /* infinite loop */ <loop body> Compound expressions separated by commas Comma: operator with lowest precedence, evaluated left-to-right for ( i = 1, j = 1; i <= n ; j = i, i ++) <loop body> Equivalent to while loop: <initialization> while (<condition>) { <loop body> <modification> }

Loops: do-while - Statement Syntax: do { <loop body> } while(<condition>); Differs from while loop condition evaluated after each iteration Body executed at least once Note semicolon at end Example: char c ; do { / loop body / puts( "Keep going? (y/n) " ) ; c = getchar(); / other processing / } while ( c == y && / other conditions / );

Loops: Nested Loops A nested loop is a loop within a loop an inner loop within the body of an outer one. for ([<initialization>];[<condition>];[<modification>]) <loop body> /* another loop here */ Can nest any loop statement within the body of any loop statement Can have more than two levels of nested loops

Loops: break - Statement Sometimes want to terminate a loop early break; exits innermost loop or switch statement to exit early Consider the modification of the do-while example: char c ; do { /* loop body / puts ( "Keep going? (y/n) " ) ; c = getchar() ; if ( c!= y ) break ; / other processing / } while ( / other conditions / ) ;

Loops: continue - Statement Use to skip an iteration continue; skips rest of innermost loop body, jumping to loop condition Example: int i, ret = 1, minval; for ( i = 2; i <= (a > b? a:b); i++) { } if ( a % i ) / a not divisible by b / continue; if ( b % i == 0) / b and a are multiple of i / ret = i; printf( %d\n, ret);

Unconditional Jump goto: transfers program execution to a labeled statement in the current function DISCOURAGED easily avoidable requires a label Label: a plain text, except C keywords, followed by a colon, prefixing a code line may occur before or after the goto statement Example: int main () { int a = 10; LOOP:do { if ( a == 15) { a = a + 1; goto LOOP; } printf("value of a: %d\n", a++); } while( a < 20 ); return 0;