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

32
Introduction to C/C++ Lecture 3 - Program Flow Control Rohit Sehgal Nishit Majithia Association of Computing Activities, Indian Institute of Technology,Kanpur [email protected] [email protected] May 25, 2016 Rohit Sehgal Nishit Majithia (IITK) ACA May 25, 2016 1 / 22

Transcript of Introduction to C/C++ Lecture 3 - Program Flow Control · Introduction to C/C++ Lecture 3 - Program...

Page 1: Introduction 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,

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

Rohit SehgalNishit Majithia

Association of Computing Activities,Indian Institute of Technology,Kanpur

[email protected]@cse.iitk.ac.in

May 25, 2016

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

Page 2: Introduction 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,

Fruits for some may seem poison to Others.

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

Page 3: Introduction 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,

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

Page 4: Introduction 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,

Control Flow

Until specified, the execution of the statements is LINEAR: One byOne

Some conditional statements gives power to:

Execute particular statement or notExecute a statement repetitively

These decisions are based on the Boolean Expression

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

Page 5: Introduction 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,

Control Flow

Until specified, the execution of the statements is LINEAR: One byOne

Some conditional statements gives power to:

Execute particular statement or notExecute a statement repetitively

These decisions are based on the Boolean Expression

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

Page 6: Introduction 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,

Control Flow

Until specified, the execution of the statements is LINEAR: One byOne

Some conditional statements gives power to:

Execute particular statement or notExecute a statement repetitively

These decisions are based on the Boolean Expression

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

Page 7: Introduction 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,

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

Page 8: Introduction 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,

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

Page 9: Introduction 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,

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

Page 10: Introduction 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,

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

Page 11: Introduction 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,

If-else Statements

Syntax

if ( condition )statement 1;

elsestatement 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 executedotherwise statement 2 will be executed.

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

Page 12: Introduction 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,

Contd...

Nested if-else

else-if ladder

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

Page 13: Introduction 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,

Contd...

Nested if-else

else-if ladder

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

Page 14: Introduction 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,

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 statementwill be executed

Often a break statement is used as the last statement in each case’sstatement list

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

Page 15: Introduction 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,

Compound Assignment

C provides compound assignment operators which enable a moreconcise 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

Page 16: Introduction 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,

Compound Assignment

C provides compound assignment operators which enable a moreconcise 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

Page 17: Introduction 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,

Increment and Decrement Operators

The increment (i.e., ++) or decrement (i.e., - -) operators are thefrequently used operators which take only one operand.

The ++ operator is same as the compound assignment withIncrement of 1 and the - - operator is same as the compoundassignment with Decrement of 1

Prefix Increment/Decrement

j = ++i;Increment i and then use it

Postfix Increment/Decrement

j = i++;Use i and then increment it

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

Page 18: Introduction 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,

Increment and Decrement Operators

The increment (i.e., ++) or decrement (i.e., - -) operators are thefrequently used operators which take only one operand.

The ++ operator is same as the compound assignment withIncrement of 1 and the - - operator is same as the compoundassignment with Decrement of 1

Prefix Increment/Decrement

j = ++i;Increment i and then use it

Postfix Increment/Decrement

j = i++;Use i and then increment it

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

Page 19: Introduction 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,

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

Page 20: Introduction 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,

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 statementwhile statementdo-while statement

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

Page 21: Introduction 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,

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 statementwhile statementdo-while statement

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

Page 22: Introduction 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,

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 exitswhen expr2 becomes false.

expr3 typically modifies a variable in expr2 eventually causing expr2to become false.

Any or all of the expressions in a for statement can be missing, butthe two semicolons must remain.

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

Page 23: Introduction 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,

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

Page 24: Introduction 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,

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

Page 25: Introduction 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,

Contd ...

One can use the comma operator in a for statement to do:

Multiple InitializationMultiple processing of indices

for ( expr1 , expr2 ; expr3 ; expr4 , expr5 )

Here expr4 and expr5 will process the initialized expr1 and expr2 atthe same time

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

Page 26: Introduction 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,

Contd ...

One can use the comma operator in a for statement to do:

Multiple InitializationMultiple processing of indices

for ( expr1 , expr2 ; expr3 ; expr4 , expr5 )

Here expr4 and expr5 will process the initialized expr1 and expr2 atthe same time

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

Page 27: Introduction 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,

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

Page 28: Introduction 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,

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

Page 29: Introduction 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,

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 truethen loop continues.

The do statement is a variant of the while statement that tests itscondition at the bottom of the loop.

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

Page 30: Introduction 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,

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 truethen loop continues.

The do statement is a variant of the while statement that tests itscondition at the bottom of the loop.

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

Page 31: Introduction 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,

goto statement in C/C++

goto causes an unconditional jump to a labeled statementsomewhere in the current function.

Syntax

label: goto label;... ...

goto label; label:... ...

goto can make a program very difficult to read and understand. It isnever needed.

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

Page 32: Introduction 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,

The End

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