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

Similar documents
Introduction to C/C++ Lecture 5 - String & its Manipulation

Flow Control. CSC215 Lecture

Introduction. C provides two styles of flow control:

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Assignment: 1. (Unit-1 Flowchart and Algorithm)

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

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

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

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

Programming for Engineers Iteration

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Introduction to C/C++ Lecture 1 - Introduction to Programming

Chapter 2: Functions and Control Structures

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

Lecture 7 Tao Wang 1

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

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Module 4: Decision-making and forming loops

More Programming Constructs -- Introduction

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

LECTURE 18. Control Flow

Lecture 5 Tao Wang 1

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

SELECTION STATEMENTS:

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

LECTURE 5 Control Structures Part 2

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

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

Conditions, logical expressions, and selection. Introduction to control structures

3. Java - Language Constructs I

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

Prepared by: Shraddha Modi

DELHI PUBLIC SCHOOL TAPI

CHAPTER 9 FLOW OF CONTROL

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

YOLOP Language Reference Manual

INTRODUCTION TO COMPUTER SCIENCE - LAB

Unit 3. Operators. School of Science and Technology INTRODUCTION

Problem Solving and 'C' Programming

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

Chapter 7. Additional Control Structures

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

Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Operators And Expressions

Chapter 4 C Program Control

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

Chapter 4. Flow of Control

Quiz 1: Functions and Procedures

Principles of Computer Science

Lecture 3. More About C

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

8. Control statements

Programming for Electrical and Computer Engineers. Loops

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

CSE 130 Introduction to Programming in C Control Flow Revisited

CS313D: ADVANCED PROGRAMMING LANGUAGE

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

8.3 Common Loop Patterns

Quick Reference Guide

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

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

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

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

CHAPTER : 9 FLOW OF CONTROL

Looping. Arizona State University 1

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

G Programming Languages - Fall 2012

Control Statements. Musa M. Ameen Computer Engineering Dept.

Lecture 3 Operators MIT AITI

Iteration. Side effects

Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Chapter 4 C Program Control

Decision Making in C

Department of Computer Science

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Theory of control structures

Control structures in C. Going beyond sequential

The Design of C: A Rational Reconstruction: Part 2

UNIT 3

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

C-LANGUAGE CURRICULAM

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

Continued from previous lecture

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

REPETITION CONTROL STRUCTURE LOGO

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

1 Lexical Considerations

SFU CMPT Topic: Control Statements

Transcription:

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 May 25, 2016 Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 1 / 22

Fruits for some may seem poison to Others. Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 2 / 22

Conditionals and Loops This topic focuses on: Boolean expressions conditional statements repetition statements iterators Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 3 / 22

Control Flow Until specified, the execution of the statements is LINEAR: One by One Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 4 / 22

Control Flow Until specified, the execution of the statements is LINEAR: One by One Some conditional statements gives power to: Execute particular statement or not Execute a statement repetitively Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 4 / 22

Control Flow Until specified, the execution of the statements is LINEAR: One by One Some conditional statements gives power to: Execute particular statement or not Execute a statement repetitively These decisions are based on the Boolean Expression Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 4 / 22

Conditional Statements There are mainly 3 conditional statements provided in C/C++ if statements if-else statements switch statements Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 5 / 22

IF Statements Syntax if ( condition ) statement; if : C/C++ keyword condition: The condition must be a boolean expression statement: If the condition is true, the statement will be executed Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 6 / 22

Boolean Expressions Expression Meaning = = equal to!= not equal to < less than > greater than <= less than or equal to >= greater than or equal to Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 7 / 22

Logical Operators Logical NOT!a If a is true, then!a is false and vice-versa Logical AND a && b If a is true and b is also true, then a && b is true otherwise false Logical OR a b If a is false and b is also false, then a b is false otherwise true Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 8 / 22

If-else Statements Syntax if ( condition ) statement 1; else statement 2; if,else: C/C++ keyword condition: The condition must be a boolean expression statement: If the condition is true, the statement 1 will be executed otherwise statement 2 will be executed. Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 9 / 22

Contd... Nested if-else Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 10 / 22

Contd... Nested if-else else-if ladder Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 10 / 22

switch Statements Syntax switch ( expression ) { case value1 : statement1; case value2 : statement2; case... } switch,case: C/C++ keyword If expression matches with any value, then corresponding statement will be executed Often a break statement is used as the last statement in each case s statement list Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 11 / 22

Compound Assignment C provides compound assignment operators which enable a more concise notation Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 12 / 22

Compound Assignment C provides compound assignment operators which enable a more concise notation temp = temp + 1 is same as temp += 1 temp = temp - 1 is same as temp -= 1 temp = temp * 1 is same as temp *= 1 temp = temp / 1 is same as temp /= 1 Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 12 / 22

Increment and Decrement Operators The increment (i.e., ++) or decrement (i.e., - -) operators are the frequently used operators which take only one operand. Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 13 / 22

Increment and Decrement Operators The increment (i.e., ++) or decrement (i.e., - -) operators are the frequently used operators which take only one operand. The ++ operator is same as the compound assignment with Increment of 1 and the - - operator is same as the compound assignment with Decrement of 1 Prefix Increment/Decrement Postfix Increment/Decrement j = ++i; Increment i and then use it j = i++; Use i and then increment it Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 13 / 22

Precedence of operators Table: Precedence order from higher to lower Operators Associativity () [] ->. left to right! ++ - - right to left * / % left to right + - left to right < <= > >= left to right ==!= left to right && left to right left to right = += -= *= /= %= right to left Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 14 / 22

Loops Loop is a control structure that repeats a group of steps in a program. Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 15 / 22

Loops Loop is a control structure that repeats a group of steps in a program. There are mainly 3 C/C++ loop control statements: for statement while statement do-while statement Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 15 / 22

for statement in C/C++ Syntax for ( expr1 ; expr2 ; expr3 ) { statement-list; } expr1 is used to initialize the loop. expr2 is a logical expression controlling the iteration. The loop exits when expr2 becomes false. expr3 typically modifies a variable in expr2 eventually causing expr2 to become false. Any or all of the expressions in a for statement can be missing, but the two semicolons must remain. Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 16 / 22

Contd... One can use the comma operator in a for statement to do: Multiple Initialization Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 17 / 22

Contd... One can use the comma operator in a for statement to do: Multiple Initialization for ( expr1, expr2 ; expr3 ; expr4 ) Here expr1 and expr2 will initialize at the same time Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 17 / 22

Contd... One can use the comma operator in a for statement to do: Multiple Initialization Multiple processing of indices Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 18 / 22

Contd... One can use the comma operator in a for statement to do: Multiple Initialization Multiple processing of indices for ( expr1, expr2 ; expr3 ; expr4, expr5 ) Here expr4 and expr5 will process the initialized expr1 and expr2 at the same time Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 18 / 22

while statement in C/C++ Syntax while ( expr ) { statement-list; } First expr is evaluated. If it is true, then statement-list is executed, and control is passed back to the beginning of the while loop. Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 19 / 22

while statement in C/C++ Syntax while ( expr ) { statement-list; } First expr is evaluated. If it is true, then statement-list is executed, and control is passed back to the beginning of the while loop. Any for loop can be converted into while loop and vice-versa. Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 19 / 22

do-while statement in C/C++ Syntax do { statement-list; } while ( expr ); statement-list will first executed and then check the expr, if it is true then loop continues. Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 20 / 22

do-while statement in C/C++ Syntax do { statement-list; } while ( expr ); statement-list will first executed and then check the expr, if it is true then loop continues. The do statement is a variant of the while statement that tests its condition at the bottom of the loop. Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 20 / 22

goto statement in C/C++ goto causes an unconditional jump to a labeled statement somewhere in the current function. Syntax label: goto label;...... goto label; label:...... goto can make a program very difficult to read and understand. It is never needed. Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 21 / 22

The End Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 22 / 22