IIT Kanpur C Course Lecture 3 Aug 31, 2008 1 Rishi Kumar, Final year BT-MT, CSE.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
true (any other value but zero) false (zero) expression Statement 2
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Statement-Level Control Structures Sections 1-4
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
UNIT II Decision Making And Branching Decision Making And Looping
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Conditional Statement
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Chapter 05 (Part III) Control Statements: Part II.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
COMP Loop Statements Yi Hong May 21, 2015.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Modulus Operator #include main() { int a, b, q, r; printf(" a b a/b a%b\n"); while (scanf("%d%d", &a, &b) != EOF) { q = a / b; /* quotient */ r = a % b;
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Branching statements.
Chapter 4 – C Program Control
Decisions Chapter 4.
Week 3 C Program Structures (Selection Structures)
Week 3 – Selection Structures
DKT121: Fundamental of Computer Programming
JavaScript: Control Statements I
Chapter 8: Control Structures
- Additional C Statements
Chapter 6 Decision Making and Looping
Chapter 4 - Program Control
Professor Jodi Neely-Ritz CGS3460
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Week 3 – Program Control Structure
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
CSC215 Lecture Control Flow.
Presentation transcript:

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

Recap Signed and Unsigned data types in C Let’s consider signed and unsigned int in C. C allocates 2 bytes(can vary from one compiler to another) For unsigned int, All bits are used to represent the magnitude. Thus 0 to 2 16 – 1 can be represented. For signed int, 1 bit is reserved for sign. ( 0 for +ve and 1 for –ve) Thus +ve numbers range from 0 to 2 15 – 1 For –ve numbers we use 2’s complements. What’s 2’s complement? 2

Recap Signed and Unsigned data types in C Let’s consider signed and unsigned int in C. C allocates 2 bytes(can vary from one compiler to another) For unsigned int, All bits are used to represent the magnitude. Thus 0 to 2 16 – 1 can be represented. For signed int, 1 bit is reserved for sign. ( 0 for +ve and 1 for –ve) Thus +ve numbers range from 0 to 2 15 – 1 For –ve numbers we use 2’s complements. What’s 2’s complement? In 2’s complement to represent a –ve number (say -x) in n bits - Compute 2 n – x. Represent this magnitude as unsigned int in n bits. - The range is 0 to – How? 3

Logical Expressions Formed using – 4 relational operators:, >= – 2 equality operators: ==, != – 3 logical connectives: &&, ||, ! int type: 1(true) or 0 (false) Some examples are -If x = 8, y = 3, z = 2 what is the value of x >= 10 && y < 5 || z ==2 4

Logical Expressions Formed using – 4 relational operators:, >= – 2 equality operators: ==, != – 3 logical connectives: &&, ||, ! int type: 1(true) or 0 (false) Some examples are -If x = 8, y = 3, z = 2 the value of x >= 10 && y < 5 || z ==2 is 1. - Precedence comes into picture. Remember last lecture? 5

Conditional Operator [ ?: ] A conditional expression is of the form expr1 ? expr2 : expr3 The expressions can recursively be conditional expressions. A substitute for if-else Example : (a<b)?((a<c)?a:c):((b<c)?b:c) What does this expression evaluate to? 6

Conditional Operator [ ?: ] A conditional expression is of the form expr1 ? expr2 : expr3 The expressions can recursively be conditional expressions. A substitute for if-else Example : (a<b)?((a<c)?a:c):((b<c)?b:c) This evaluates to min(a,b,c) 7

if-else statement The syntax is – if(expr) stmt – if(expr) stmt1 else stmt2 Note that stmt, stmt1, stmt2 can either be simple or compound or control statements. – Simple statement is of the form expr; – Compound statement is of the form { stmt1; stmt2; ………. stmtn; } – Control Statement: will be discussed through this lecture. involves if-else,for,switch, etc e.g- if(expr) stmt1 else stmt2 8

if-else : some examples x = 1; y = 10; if(y 0) x = 3; else x = 5; printf(“%d\n”, x); What is the output here? if(z = y < 0) x = 10; printf(“%d %d\n”, x, z); What is the output here? 9

if-else : some examples x = 1; y =10; if(y 0) x = 3; else x = 5; printf(“%d\n”, x); Output is : 1 Dangling else : else clause is always associated with the closest preceding unmatched if. if(z = y < 0) x = 10; printf(“%d %d\n”, x, z); The above code is equiv to the following one: z = y <0; if (z) x = 10; printf(“%d %d\n”, x,z); Output is:

While and do-while Syntax is – while(expr) stmt As long as expr is true, keep on executing the stmt in loop – do stmt while(expr) Same as before, except that the stmt is executed at least once. Example: int i=0, x=0 ; while (i<10) { if(i%3==0) { x += i; printf(“%d “, x); } ++i; } What is the output here? 11

While and do-while Syntax is – while(expr) stmt As long as expr is true, keep on executing the stmt in loop – do stmt while(expr) Same as before, except that the stmt is executed at least once. Example: int i=0, x=0 ; while (i<10) { if(i%3==0) { x += i; printf(“%d “, x); } ++i; } Output is:

for statement Syntax is – for(expr1; expr2; expr3) stmt expr1 is used to initialize some parameters expr2 represents a condition that must be true for the loop to continue expr3 is used to modify the values of some parameters. – It is equiv to expr1; while (expr2) { stmt expr3; } 13

for statement This piece of code has equivalent for statement as follows: expr1a; expr1b; while (expr2) { stmt expr3a; expr3b; } for ( expr1a, expr1b; expr2; expr3a, expr3b) stmt Note that in the for statement expr1, expr2, expr3 need not necessarily be present. If expr2 is not there, then the loop will go forever. 14

for statement: some examples int i, j, x; for(i=0, x=0; i<5; ++i) for(j=0; j<i; ++j) { x += (i+j-1); printf(“%d ”, x); } What is the output here? 15

for statement: some examples int i, j, x; for(i=0, x=0; i<5; ++i) for(j=0; j<i; ++j) { x += (i+j-1); printf(“%d ”, x); } Output is :

switch statement Syntax is – switch (expr) stmt – expr must result in integer value; char can be used(ASCII integer value A-Z: 65-90, a-z: ) – stmt specifies alternate courses of action case prefixes identify different groups of alternatives. Each group of alternatives has the syntax case expr: stmt1 stmt2 ……… stmtn Note that parentheses { } are not needed in case block Multiple case labels case expr1:case expr2 :… … …: case exprn: stmt1 stmt2 ……… stmtm 17

switch statement: example switch (letter = getchar()) { case‘a’: case ’A’: case ‘e’ : case ‘E’: case‘i’: case ‘I’: case ’o’ : case ‘O’: case‘u’: case ‘U’: printf(“Vowel”); break; default: printf(“Consonant”); } -Note the use of multiple cases for one group of alternative. Also note the use of default. Statement corresponding to default is always executed. break to be discussed soon. 18

Power of break Syntax is – break; used to terminate loop or exit from a switch. In case of several nested while,do-while, for or switch statements, a break statement will cause a transfer of control out of the immediate enclosing statement. 19

break statement: Example int count =0; while (count <=n) { while( c=getchar()!=‘\n’) { if ( c break; … … … } ++count; } 20

continue statement Used to bypass the remainder of the current pass through a loop. Computation proceeds directly to the next pass through the loop. Example: for( count=1; x <=100; ++count) { scanf ( “%f “, &x); if (x < 0) { printf(“ it’s a negative no\n”) continue; } /*computation for non-negative numbers here*/ } 21

goto statement Note that you can tag any statement in C with an identifier. And then, can use goto to directly transfer the program control to that statement. Example: while ( x <= 10) { … … … if (x<0) goto chkErr; … … … scanf(“%f”, &x); } chkErr: { printf(“found a negative value!\n”); … … … } Note that use of goto is discouraged. It encourages logic that skips all over the program. Difficult to track the code. Hard to debug. 22

Questions!! 23