Académique Documents
Professionnel Documents
Culture Documents
Announcements
Tutorial on algorithm design by Prof Somenath Biswas on Prof. Saturday, 6th August at 10:30 AM in CS building. Tutorial in Hindi by Arpan Maheshwari on Sunday, 7th August at 5:00 PM in CS building. Monday lab scheduled on 15th August will instead be held on Saturday, 20th August. M d l b scheduled on 22nd A Monday lab h d l d August will instead be held on t ill i t d b h ld th August. Saturday, 27 Wednesday lab scheduled on 31st August will instead be held on Saturday, 3rd September.
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 1
Recap
Variables names, types, sizes names types Comments printf, scanf Arithmetic operators Arithmetic expressions Precedence, Associativity y
Lec-06
Lec-06
Lec-06
Functions
We are eventually going to organize our large programs into functions. A function is part of the program that takes some p p p g parameters, , does some processing, and returns some result. For now, we will define only one function in our programs, main() All C programs start their execution from the main function. We will use functions written by someone else and provided by the system printf, scanf, etc. Information about them is in header files, which were mentioned after #include Compiler somehow includes them in your program
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6
More functions
Several mathematical functions supported by system. system In Thursday and Friday labs, you have to use sqrt()
takes a floating point number a parameter, and returns a floating point number which is the square-root of the parameter. the header file corresponding to this function has to be added
#include <math.h> <math h>
Lec-06
Assignment Statement
A statement is some instruction followed by a semicolon (;) Assignment statement looks like: mass = 20 + 5; There is a variable name on the left hand side The operator = is in the middle An expression is on the right hand side Execution: The expression on the right hand side is evaluated The value is stored in the memory area whose name is given by the variable
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8
More Assignments
Variables can appear on the right hand side as well, e.g., well e g
j = 5; i = j * 2;
This will place number 5 in the memory area represented by j and number 10 in the memory area represented by i. k = 7 * i + 36 / j * 2 ;
Lec-06
Variable Initialization
Variables can be initialized at the time of declaration. declaration The initial value is stored in the memory corresponding to that variable. For example: int year = 2011; float average = 8.5; long huge_number = 5678901234;
Lec-06
10
Lec-06
if statement
Making decisions The general form of the statement is:
if ( expression ) <program statement>
if - else
If your algorithm requires that you take some action if the condition is true, and some other action if the condition is false, then a more general statement is: if ( condition ) {
statements1 } else { statements2 }
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 14
Relational Operators
Meaning M i Equal to Less than Less than or Equal to Greater than Greater than or Equal to q Not equal to
Example E l count == 10 a<b low <= high age > 18 j >= 0 marks != 0
Lec-06
16
Understanding if
Condition must evaluate to a boolean (true or false) value When condition is true, the if part is executed Otherwise (i.e., when condition is false), the else part is executed It is not necessary to have else part Multiple statements can be used in either if part or else part or both.
Lec-06
17
Lec-06
19
10
Any Questions?
Lec-06
20
11