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