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

Similar documents
Conditional Statement

Flow Control. CSC215 Lecture

DECISION MAKING STATEMENTS

Decision Making and Loops

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

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

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Prepared by: Shraddha Modi

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Iteration. Side effects

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

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

C-LANGUAGE CURRICULAM

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Conditional Statement

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

C: How to Program. Week /Mar/05

DECISION CONTROL AND LOOPING STATEMENTS

COMP 208 Computers in Engineering

Chapter 2 - Introduction to C Programming

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

These are reserved words of the C language. For example int, float, if, else, for, while etc.

C - Basics, Bitwise Operator. Zhaoguo Wang

Decision Making -Branching. Class Incharge: S. Sasirekha

The Design of C: A Rational Reconstruction: Part 2

Continued from previous lecture

n Group of statements that are executed repeatedly while some condition remains true

Chapter 4. Flow of Control

Lecture 10. Daily Puzzle

CS1100 Introduction to Programming

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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

Course Outline Introduction to C-Programming

Unit 7. Functions. Need of User Defined Functions

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

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

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

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

MODULE 2: Branching and Looping

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

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

Structured Programming. Jon Macey

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Subject: PIC Chapter 2.

Review of the C Programming Language

Computer Programming

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement

Introduction to C Programming

Review of the C Programming Language for Principles of Operating Systems

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics

Module 4: Decision-making and forming loops

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

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

LESSON 6 FLOW OF CONTROL

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C Syntax Out: 15 September, 1995

Lecture 2: C Programming Basic

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

Questions Bank. 14) State any four advantages of using flow-chart

AMCAT Automata Coding Sample Questions And Answers

CHAPTER 5 FLOW OF CONTROL

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

MIDTERM EXAMINATION - CS130 - Spring 2003

Structured Programming. Dr. Mohamed Khedr Lecture 9

LECTURE 18. Control Flow

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Conditional Expressions

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Low level programming (37-023)

C Program Development and Debugging under Unix SEEM 3460

STEVEN R. BAGLEY ARM: LOOPS AND ADDRESSING

Computer System and programming in C

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

CS1100 Introduction to Programming

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

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

In this lab, you will learn more about selection statements. You will get familiar to

Chapter 4 C Program Control

Creating, Compiling and Executing

Chapter 2, Part III Arithmetic Operators and Decision Making

Lecture 02 C FUNDAMENTALS

C++ PROGRAMMING SKILLS Part 2 Programming Structures

CSE 582 Autumn 2002 Exam 11/26/02

Chapter 8. Statement-Level Control Structures

Chapter 4: Making Decisions

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Why Is Repetition Needed?

Chapter 8 Statement-Level Control Structures

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Transcription:

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

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 2 15. How? 3

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 what is the value of x >= 10 && y < 5 z ==2

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

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: 0 3 9 18 12

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: 0 1 3 5 8 12 15 19 24 30 16

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: 97-122) 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

Syntax is break; Power of 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