SlideShare a Scribd company logo
In this session, you will learn to: Work with operators Use loops Use formatted input-output functions Objectives
An operator: Is a symbol used to command the computer to do mathematical or logical manipulations.  Operates on data and variables. C has a rich set of operators, which can be classified into following various categories: Relational operators Logical operators Unary operators Binary operators Ternary operator Compound assignment operators Increment/Decrement  operators Working with Operators
Notations for logical operators in C are: Operator Notation   OR   | |   AND   &&   NOT   ! Operator precedence: NOT (!) is evaluated before AND (&&), which is evaluated before OR (||). Brackets () are used to change this order. Logical Operators
Write a function that accepts either  y  or  n  only as input. For any other character input, an appropriate error message should be displayed and the input is accepted again.  Practice: 2.1
Solution: #include<stdio.h> main() { char yn; do { puts(“Enter y/n (Yes/No)”); yn=getchar (); fflush (stdin); if(yn!=’y’ && yn!=’n’) puts(“Invalid input”); } while(yn!=’y’ && yn!=’n’); } Practice: 2.1 (Contd.)
Unary Operators: Operates on a single operand. Prefixed to an integer constant.  Tells the compiler to reverse the sign by subtracting the value (or variable) from zero.  Has the same effect as the – sign, which is used to indicate a number less than zero, for example -12. Unary Operators
Which of the following are valid? 1. valuea=-12;   /* valuea is int* / 2. valuea = - valueb – 12  /* valuea and valueb both are int */ Practice: 2.2
Solution: 1. Valid 2. Valid Practice: 2.2 (Contd.)
Binary Operators: Operate on two operands. Are as follows:  + (add) - (subtract) * (multiply) / (divide) % (modulo) Binary Operators
In the following set of statements: char ch; ch=’S’; ch=ch+’a’-‘A’; /*statement A*/ ch=ch+’A’-‘a’; /*statement B*/ What will be the value of ch after: 1. Statement A is executed? 2. Statement B is executed? Practice: 2.3
Solution: 1. ch  is equal to  s . Note that ‘a’-‘A’ gives 32 after statement 1 is executed. 2. ch  is back to  S  after statement 2 is executed. Practice: 2.3 (Contd.)
There are some set or rules, if followed, would prevent unexpected results, at the time of execution of programs: Any operand of type  char  is converted to  int . All floats are converted to doubles. If either operand is  double , the other is converted to a  double , giving a  double  result. Binary Operators (Contd.)
In which of the following assignments is there no loss of data? ( i  is an  int ,  f  a  float , and  d  a  double ) i=d; d=f; f=d; i=f+d; d=i+f; Is type casting necessary in the following example? int i,j; float f; double d; d=f+(float) i + j ; Practice: 2.4
Solution: a. Loss of data.  int  set equal to a double. b. No loss of data.  double  set equal to a  float . c. Loss of data.  float  set equal to a  double . d. Loss of data. Right-hand result is a  double  while left-hand   side is just an  int . e. No loss of data. Right-hand result is a  double  and    left-hand side is also a  double . 2.  Not necessary. The ints will automatically be converted to doubles (following the conversion of the  float  to a  double ). Practice: 2.4 (Contd.)
Ternary Operator: Is a shorthand method for writing  if.else  conditional construct. Helps in reducing lines of code. Has the following form for the expression using the ternary operator: (test-expression) ? T-expression : F-expression; Ternary Operator
Consider the following example: if(condition)  { Statements if condition is true } else  { Statements if condition is false } Can be rewritten using the shorthand operator as follows: larger_of_the_two = ( x > y ) ? x : y;  Ternary Operator (Contd.)
1. State whether True or False: In the general form of an expression that uses a ternary operator, the test expression will be checked. If it is true, the T-expression will be evaluated, otherwise the F-expression will be evaluated. 2. What will the following statement do? quotient = (b==0) ? 0 : (a/b); /*a, b, and quotient are ints*/  3. Can the preceding statement be written as follows? quotient = (b) ? (a/b) : 0;  4. What will the following statement do? always_negative = (j>0) ? j : (-j); Practice: 2.5
Solution: True. If  b  is non-zero, it will determine the quotient of  a  and  b . If  b  equals zero, quotient is set to 0. Yes. Note that if  b  is non-zero, the test expression ( b ) evaluates to true and hence quotient is set to ( a/b ). The variable  always_negative  will always take on a non-negative value, i.e. it will be assigned the absolute value of  j . The name of the variable  always_negative  is just a red herring. Remember that self-documenting variable names will help in writing programs that are readable. Note the unary operator ( -j ). Practice: 2.5 (Contd.)
Compound Assignment Operators: Are useful especially when long variable names are used. Has the following general form:  left-value op= right-expression;  Here  op  can be either  +  (add),  -  (subtract,  *  (multiply),  /  (divide), and  %  (modulo). Consider the following example: a_very_long_identifier=a_very_long_identifier + 2; It can be written as: a_very_long_identifier += 2; Compound Assignment Operators
Increment / Decrement Operators: Are used to increase or decrease the value of a variable by 1. Has the following two forms: The ++ (two plus symbols without a space), called the increment operator while that in ++ before the variable is called  the pre increment operator  and after the variable is called the post increment operator. The -- (two minus symbols without a space), called the decrement operator while that in ++ before the variable is called  the pre decrement operator  and after the variable is called the post increment operator. Increment / Decrement Operators
Consider the following code snippet: total = sum++;  /* statement A */  total = ++sum;  /* statement B */ The first statement is equivalent to: total = sum; sum = sum + 1; While the second is the same as: sum = sum + 1; total = sum; Increment / Decrement Operators (Contd.)
Consider the following code snippet: int sum = 3, total = 5; total = sum++; total = ++sum; /*statement A */ total = sum— total = --sum; /*statement B */ What will be the values of total and sum after: statement A is executed? statement B is executed? Practice: 2.6
State whether True or False: The following statement: for(i = 0; i< 100); i = i + 1) { Some statements }   can be written as: for (i = 0; i < 100; i++) { Some statements } Practice: 2.6 (Contd.)
State whether True or False: The for statement in #2 can also be written as: fori = 0; i < 100; ++i)/*Note: ++i and not i++*/ { Some statements } 4.  Write a program, which reads in a year and reports on whether it is a leap year or not (centuries should also be considered). Practice: 2.6 (Contd.)
Solution: total=5, sum=5 total=3, sum=3 quite a complicated way of reducing total by 2. 2.  True. i+1 is equivalent to i++. True. i+1 is equivalent to 1+i. 4.  Practice: 2.6 (Contd.)
The  while  and  do…while  looping constructs are generally used in situations where the number of execution of the loop is not known. The  for  loop construct is used when the number of execution of the loop is known. Using Loops
The for loop construct: Has three components in the loop control: Initialization Condition Re-initialization (increment/decrement)  Has the following sequence of execution:  Initialization Evaluation of loop condition Body of loop Re-initialization The for Loop Construct
The sequence of execution of a complete for loop construct is shown in the following figure. The for Loop Construct (Contd.) TRUE INITIALIZATION EVALUATE CONDITION BODY OF LOOP REINITIALIZATION FALSE
In a for loop construct: Multiple initializations and/or multiple re- initializations, are separated by commas. Multiple conditions are specified using logical operators.   The for Loop Construct (Contd.)
Write a function to accept twenty characters from the character set, and to display whether the number of lower-case characters is greater than, less than, or equal to number of upper-case characters. Display an error message if the input is not an alphabet. Modify the function to accept characters endlessly until the character ! is input from keyboard. Practice: 2.7
Solution: 1.  2.  Practice: 2.7 (Contd.)
At times there is a need to exit from a loop before the loop condition is re-evaluated after iteration. To exit from loop control,  break  and  continue  statements are used.  Controlling the Loop Execution
Write a function, which accepts numbers until 0 is entered or 10 numbers have been accepted. The function prints the total number of entries, the number of positive entries, and the sum of all the positive numbers. Practice: 2.8
Solution: Practice: 2.8 (Contd.)
C provides the following functions for formatted input-output: printf() scanf() Using Formatted Input-Output Functions
Syntax of the formatted output function  printf()  is:   printf (format, data1, data 2, ….); Consider the following example:   printf(“%c”, inp); The character specified after % is called a conversion character. The conversion character allows one data type to be converted to another type and printed. Formatted Output
The conversion characters and their meanings are shown in the following table. Formatted Output (Contd.) Character Meaning d the data is converted to decimal (integer) c the data is taken as a character s the data is a string and characters from the string are printed until a NULL character is reached f the data is output as a double or float with a default precision to 6
What is the output of the statement: printf(“Integer is: %d; Alphabet is: %c\n”, inum, inp); where  inum  contains  15  and  inp  contains  Z . Practice: 2.9
Solution: Integer is: 15; Alphabet is Z. Practice: 2.9 (Contd.)
The  scanf()  function is used for formatted input. The syntax for the  scanf()  functions is as follows: scanf (format, data1, data2……); Here format   - The format-specification string data1, data2  - Data names where the input data    is to be stored as per the   format-specification string Formatted Input
The format-specification string in  scanf()  consists of: Blanks, tabs, (also called white space characters). New line which are ignored. Conversion consisting of %, an optional number specification specifying the width and a conversion character. While accepting strings using  scanf() , a space is considered as a string terminator. Formatted Input (Contd.)
Practice: 2.10 Write a function to accept and display the element number and the weight of a Proton. The element number is an integer and weight is fractional.
Practice: 2.10 (Contd.) Solution: #include<stdio.h> main() { int e_num; float e_wt; printf(“Enter the Element No. and Weight of a Proton\n”); scanf(“%d %f”, &e_num, &e_wt); fflush(stdin); printf(“The Element No. is: “, e_num); printf(“The weight of a Proton is: %f\n“, e_wt); }
Practice: 2.11 Write a function to input a string of continuous characters with no white space as part of the input. The function should assign the input to variables of the types specified in the following table. The function should also print out each of the assigned data items in separate lines. Position of character from start of string Number of characters Type of argument to assign 1 2 int 3 4 float 7 2 char (string) 9 3 int
Practice: 2.11 Solution: #include<stdio.h> main() { int i,j; char str[3]; float fnum; printf(“Enter a string of 11 chrs\n”); /*why 11: because 11 is the total length of */ /*input.*/ scanf(“%2d %4f  %2s %3d”,&i, &fnum, str, &j); fflush(stdin); printf(“%2d\n %4f\n  %2s\n %3d\n”, i, fnum, str, j); }
In this session, you learned that: An operator is a symbol that is used to command the computer to do mathematical or logical manipulations. The operators in C language are classified into the following categories: Logical operators Unary operators Binary operators Ternary operator Compound assignment operators Increment/Decrement  operators Summary
The logical operators of C and their notations are as follows. The unary operator prefixed to an integer constant or variable tells the compiler to reverse the sign by subtracting the value or variable from zero. Binary operators in C language are  +  (add),  -  (subtract),  *  (multiply),  /  (divide), and  %  (modulo). Ternary operator offers a shorthand way of writing the commonly used  if…else  construct. Summary (Contd.) Operator Notation OR || AND && NOT !
The syntax for using the ternary operator is: (test-expression) ? T-expression : F-expression; Compound assignment operators simplify the statements. Increment / Decrement operators are used to increment/decrement a variable by 1. A  for  loop is used when the number of execution of the loop is known. The components of a for loop construct are: initialization loop condition reinitialization (increment/decrement) Summary (Contd.)
The sequence of execution of a complete for loop is: initialization evaluation of the loop condition the body of the loop reinitialization The  break  and  continue  statements are used to exit from loop control. The  break  statement is used to exit from all loop constructs ( while ,  do...while , and  for ) and  switch...case  statements. The  continue  statement is used to skip all subsequent instructions and brings the control back to the loop. The function  printf()  is used for formatted output to standard output based on a format specification.  Summary (Contd.)
The syntax of the function  printf()  is:  printf(format, datal, data 2,,..); The function  scanf()  is used for formatted input from standard input and provides many of the conversion facilities of the function  printf() . The syntax of the function  scanf()  is: scanf (format, datal, data2, ...); Summary (Contd.)
Ad

More Related Content

What's hot (20)

Computer programming(CP)
Computer programming(CP)Computer programming(CP)
Computer programming(CP)
nmahi96
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi96
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
MomenMostafa
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
nmahi96
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
K Durga Prasad
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
tanmaymodi4
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 
C# String
C# StringC# String
C# String
Raghuveer Guthikonda
 
C Programming Assignment
C Programming AssignmentC Programming Assignment
C Programming Assignment
Vijayananda Mohire
 
C programming & data structure [character strings & string functions]
C programming & data structure   [character strings & string functions]C programming & data structure   [character strings & string functions]
C programming & data structure [character strings & string functions]
MomenMostafa
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
Icaii Infotech
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of C
Suchit Patel
 
Data type in c
Data type in cData type in c
Data type in c
thirumalaikumar3
 
Computer
ComputerComputer
Computer
Hanaa Ahmed
 
Structures-2
Structures-2Structures-2
Structures-2
arshpreetkaur07
 
Computer programming(CP)
Computer programming(CP)Computer programming(CP)
Computer programming(CP)
nmahi96
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi96
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
MomenMostafa
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
nmahi96
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
K Durga Prasad
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
tanmaymodi4
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
C programming & data structure [character strings & string functions]
C programming & data structure   [character strings & string functions]C programming & data structure   [character strings & string functions]
C programming & data structure [character strings & string functions]
MomenMostafa
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
Icaii Infotech
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of C
Suchit Patel
 

Viewers also liked (13)

C programming session 03
C programming session 03C programming session 03
C programming session 03
Dushmanta Nath
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
Dushmanta Nath
 
Dacj 1-2 c
Dacj 1-2 cDacj 1-2 c
Dacj 1-2 c
Niit Care
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
Dushmanta Nath
 
Session no 4
Session no 4Session no 4
Session no 4
Saif Ullah Dar
 
Session No1
Session No1 Session No1
Session No1
Saif Ullah Dar
 
Java script session 3
Java script session 3Java script session 3
Java script session 3
Saif Ullah Dar
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
Dushmanta Nath
 
Session no 2
Session no 2Session no 2
Session no 2
Saif Ullah Dar
 
Niit
NiitNiit
Niit
Mahima Narang
 
Java script Session No 1
Java script Session No 1Java script Session No 1
Java script Session No 1
Saif Ullah Dar
 
An Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java ScriptAn Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java Script
Fahim Abdullah
 
Session 3 Java Script
Session 3 Java ScriptSession 3 Java Script
Session 3 Java Script
Muhammad Hesham
 
Ad

Similar to C programming session 02 (20)

C program
C programC program
C program
AJAL A J
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
ishorishore
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
Fiaz Khokhar
 
ICP - Lecture 5
ICP - Lecture 5ICP - Lecture 5
ICP - Lecture 5
Hassaan Rahman
 
learn basic to advance C Programming Notes
learn basic to advance C Programming Noteslearn basic to advance C Programming Notes
learn basic to advance C Programming Notes
bhagadeakshay97
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
Don Dooley
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
MaryJacob24
 
05 operators
05   operators05   operators
05 operators
dhrubo kayal
 
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
Module 2_PPT_P1 POP Notes module 2 fdfd.pdfModule 2_PPT_P1 POP Notes module 2 fdfd.pdf
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Vivek Singh
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
Rohit Shrivastava
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
Rohit Shrivastava
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Mohammed Saleh
 
operators and arithmatic expression in C Language
operators and arithmatic expression in C Languageoperators and arithmatic expression in C Language
operators and arithmatic expression in C Language
ParamesswariNataraja
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
Few Operator used in c++
Few Operator used in c++Few Operator used in c++
Few Operator used in c++
sunny khan
 
Coper in C
Coper in CCoper in C
Coper in C
thirumalaikumar3
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2
Tekendra Nath Yogi
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
Kathirvel Ayyaswamy
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
ishorishore
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
Fiaz Khokhar
 
learn basic to advance C Programming Notes
learn basic to advance C Programming Noteslearn basic to advance C Programming Notes
learn basic to advance C Programming Notes
bhagadeakshay97
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
Don Dooley
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
MaryJacob24
 
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
Module 2_PPT_P1 POP Notes module 2 fdfd.pdfModule 2_PPT_P1 POP Notes module 2 fdfd.pdf
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Vivek Singh
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
operators and arithmatic expression in C Language
operators and arithmatic expression in C Languageoperators and arithmatic expression in C Language
operators and arithmatic expression in C Language
ParamesswariNataraja
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
Few Operator used in c++
Few Operator used in c++Few Operator used in c++
Few Operator used in c++
sunny khan
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2
Tekendra Nath Yogi
 
Ad

More from Dushmanta Nath (6)

Global Warming Project
Global Warming ProjectGlobal Warming Project
Global Warming Project
Dushmanta Nath
 
DBMS Practical File
DBMS Practical FileDBMS Practical File
DBMS Practical File
Dushmanta Nath
 
Manufacturing Practice (MP) Training Project
Manufacturing Practice (MP) Training ProjectManufacturing Practice (MP) Training Project
Manufacturing Practice (MP) Training Project
Dushmanta Nath
 
BSNL Training Project
BSNL Training ProjectBSNL Training Project
BSNL Training Project
Dushmanta Nath
 
IT- 328 Web Administration (Practicals)
IT- 328 Web Administration (Practicals)IT- 328 Web Administration (Practicals)
IT- 328 Web Administration (Practicals)
Dushmanta Nath
 
IT-314 MIS (Practicals)
IT-314 MIS (Practicals)IT-314 MIS (Practicals)
IT-314 MIS (Practicals)
Dushmanta Nath
 
Global Warming Project
Global Warming ProjectGlobal Warming Project
Global Warming Project
Dushmanta Nath
 
Manufacturing Practice (MP) Training Project
Manufacturing Practice (MP) Training ProjectManufacturing Practice (MP) Training Project
Manufacturing Practice (MP) Training Project
Dushmanta Nath
 
IT- 328 Web Administration (Practicals)
IT- 328 Web Administration (Practicals)IT- 328 Web Administration (Practicals)
IT- 328 Web Administration (Practicals)
Dushmanta Nath
 
IT-314 MIS (Practicals)
IT-314 MIS (Practicals)IT-314 MIS (Practicals)
IT-314 MIS (Practicals)
Dushmanta Nath
 

Recently uploaded (20)

Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 

C programming session 02

  • 1. In this session, you will learn to: Work with operators Use loops Use formatted input-output functions Objectives
  • 2. An operator: Is a symbol used to command the computer to do mathematical or logical manipulations. Operates on data and variables. C has a rich set of operators, which can be classified into following various categories: Relational operators Logical operators Unary operators Binary operators Ternary operator Compound assignment operators Increment/Decrement operators Working with Operators
  • 3. Notations for logical operators in C are: Operator Notation OR | | AND && NOT ! Operator precedence: NOT (!) is evaluated before AND (&&), which is evaluated before OR (||). Brackets () are used to change this order. Logical Operators
  • 4. Write a function that accepts either y or n only as input. For any other character input, an appropriate error message should be displayed and the input is accepted again. Practice: 2.1
  • 5. Solution: #include<stdio.h> main() { char yn; do { puts(“Enter y/n (Yes/No)”); yn=getchar (); fflush (stdin); if(yn!=’y’ && yn!=’n’) puts(“Invalid input”); } while(yn!=’y’ && yn!=’n’); } Practice: 2.1 (Contd.)
  • 6. Unary Operators: Operates on a single operand. Prefixed to an integer constant. Tells the compiler to reverse the sign by subtracting the value (or variable) from zero. Has the same effect as the – sign, which is used to indicate a number less than zero, for example -12. Unary Operators
  • 7. Which of the following are valid? 1. valuea=-12; /* valuea is int* / 2. valuea = - valueb – 12 /* valuea and valueb both are int */ Practice: 2.2
  • 8. Solution: 1. Valid 2. Valid Practice: 2.2 (Contd.)
  • 9. Binary Operators: Operate on two operands. Are as follows: + (add) - (subtract) * (multiply) / (divide) % (modulo) Binary Operators
  • 10. In the following set of statements: char ch; ch=’S’; ch=ch+’a’-‘A’; /*statement A*/ ch=ch+’A’-‘a’; /*statement B*/ What will be the value of ch after: 1. Statement A is executed? 2. Statement B is executed? Practice: 2.3
  • 11. Solution: 1. ch is equal to s . Note that ‘a’-‘A’ gives 32 after statement 1 is executed. 2. ch is back to S after statement 2 is executed. Practice: 2.3 (Contd.)
  • 12. There are some set or rules, if followed, would prevent unexpected results, at the time of execution of programs: Any operand of type char is converted to int . All floats are converted to doubles. If either operand is double , the other is converted to a double , giving a double result. Binary Operators (Contd.)
  • 13. In which of the following assignments is there no loss of data? ( i is an int , f a float , and d a double ) i=d; d=f; f=d; i=f+d; d=i+f; Is type casting necessary in the following example? int i,j; float f; double d; d=f+(float) i + j ; Practice: 2.4
  • 14. Solution: a. Loss of data. int set equal to a double. b. No loss of data. double set equal to a float . c. Loss of data. float set equal to a double . d. Loss of data. Right-hand result is a double while left-hand side is just an int . e. No loss of data. Right-hand result is a double and left-hand side is also a double . 2. Not necessary. The ints will automatically be converted to doubles (following the conversion of the float to a double ). Practice: 2.4 (Contd.)
  • 15. Ternary Operator: Is a shorthand method for writing if.else conditional construct. Helps in reducing lines of code. Has the following form for the expression using the ternary operator: (test-expression) ? T-expression : F-expression; Ternary Operator
  • 16. Consider the following example: if(condition) { Statements if condition is true } else { Statements if condition is false } Can be rewritten using the shorthand operator as follows: larger_of_the_two = ( x > y ) ? x : y; Ternary Operator (Contd.)
  • 17. 1. State whether True or False: In the general form of an expression that uses a ternary operator, the test expression will be checked. If it is true, the T-expression will be evaluated, otherwise the F-expression will be evaluated. 2. What will the following statement do? quotient = (b==0) ? 0 : (a/b); /*a, b, and quotient are ints*/ 3. Can the preceding statement be written as follows? quotient = (b) ? (a/b) : 0; 4. What will the following statement do? always_negative = (j>0) ? j : (-j); Practice: 2.5
  • 18. Solution: True. If b is non-zero, it will determine the quotient of a and b . If b equals zero, quotient is set to 0. Yes. Note that if b is non-zero, the test expression ( b ) evaluates to true and hence quotient is set to ( a/b ). The variable always_negative will always take on a non-negative value, i.e. it will be assigned the absolute value of j . The name of the variable always_negative is just a red herring. Remember that self-documenting variable names will help in writing programs that are readable. Note the unary operator ( -j ). Practice: 2.5 (Contd.)
  • 19. Compound Assignment Operators: Are useful especially when long variable names are used. Has the following general form: left-value op= right-expression; Here op can be either + (add), - (subtract, * (multiply), / (divide), and % (modulo). Consider the following example: a_very_long_identifier=a_very_long_identifier + 2; It can be written as: a_very_long_identifier += 2; Compound Assignment Operators
  • 20. Increment / Decrement Operators: Are used to increase or decrease the value of a variable by 1. Has the following two forms: The ++ (two plus symbols without a space), called the increment operator while that in ++ before the variable is called the pre increment operator and after the variable is called the post increment operator. The -- (two minus symbols without a space), called the decrement operator while that in ++ before the variable is called the pre decrement operator and after the variable is called the post increment operator. Increment / Decrement Operators
  • 21. Consider the following code snippet: total = sum++; /* statement A */ total = ++sum; /* statement B */ The first statement is equivalent to: total = sum; sum = sum + 1; While the second is the same as: sum = sum + 1; total = sum; Increment / Decrement Operators (Contd.)
  • 22. Consider the following code snippet: int sum = 3, total = 5; total = sum++; total = ++sum; /*statement A */ total = sum— total = --sum; /*statement B */ What will be the values of total and sum after: statement A is executed? statement B is executed? Practice: 2.6
  • 23. State whether True or False: The following statement: for(i = 0; i< 100); i = i + 1) { Some statements } can be written as: for (i = 0; i < 100; i++) { Some statements } Practice: 2.6 (Contd.)
  • 24. State whether True or False: The for statement in #2 can also be written as: fori = 0; i < 100; ++i)/*Note: ++i and not i++*/ { Some statements } 4. Write a program, which reads in a year and reports on whether it is a leap year or not (centuries should also be considered). Practice: 2.6 (Contd.)
  • 25. Solution: total=5, sum=5 total=3, sum=3 quite a complicated way of reducing total by 2. 2. True. i+1 is equivalent to i++. True. i+1 is equivalent to 1+i. 4. Practice: 2.6 (Contd.)
  • 26. The while and do…while looping constructs are generally used in situations where the number of execution of the loop is not known. The for loop construct is used when the number of execution of the loop is known. Using Loops
  • 27. The for loop construct: Has three components in the loop control: Initialization Condition Re-initialization (increment/decrement)  Has the following sequence of execution: Initialization Evaluation of loop condition Body of loop Re-initialization The for Loop Construct
  • 28. The sequence of execution of a complete for loop construct is shown in the following figure. The for Loop Construct (Contd.) TRUE INITIALIZATION EVALUATE CONDITION BODY OF LOOP REINITIALIZATION FALSE
  • 29. In a for loop construct: Multiple initializations and/or multiple re- initializations, are separated by commas. Multiple conditions are specified using logical operators.   The for Loop Construct (Contd.)
  • 30. Write a function to accept twenty characters from the character set, and to display whether the number of lower-case characters is greater than, less than, or equal to number of upper-case characters. Display an error message if the input is not an alphabet. Modify the function to accept characters endlessly until the character ! is input from keyboard. Practice: 2.7
  • 31. Solution: 1. 2. Practice: 2.7 (Contd.)
  • 32. At times there is a need to exit from a loop before the loop condition is re-evaluated after iteration. To exit from loop control, break and continue statements are used. Controlling the Loop Execution
  • 33. Write a function, which accepts numbers until 0 is entered or 10 numbers have been accepted. The function prints the total number of entries, the number of positive entries, and the sum of all the positive numbers. Practice: 2.8
  • 35. C provides the following functions for formatted input-output: printf() scanf() Using Formatted Input-Output Functions
  • 36. Syntax of the formatted output function printf() is: printf (format, data1, data 2, ….); Consider the following example: printf(“%c”, inp); The character specified after % is called a conversion character. The conversion character allows one data type to be converted to another type and printed. Formatted Output
  • 37. The conversion characters and their meanings are shown in the following table. Formatted Output (Contd.) Character Meaning d the data is converted to decimal (integer) c the data is taken as a character s the data is a string and characters from the string are printed until a NULL character is reached f the data is output as a double or float with a default precision to 6
  • 38. What is the output of the statement: printf(“Integer is: %d; Alphabet is: %c\n”, inum, inp); where inum contains 15 and inp contains Z . Practice: 2.9
  • 39. Solution: Integer is: 15; Alphabet is Z. Practice: 2.9 (Contd.)
  • 40. The scanf() function is used for formatted input. The syntax for the scanf() functions is as follows: scanf (format, data1, data2……); Here format - The format-specification string data1, data2 - Data names where the input data is to be stored as per the format-specification string Formatted Input
  • 41. The format-specification string in scanf() consists of: Blanks, tabs, (also called white space characters). New line which are ignored. Conversion consisting of %, an optional number specification specifying the width and a conversion character. While accepting strings using scanf() , a space is considered as a string terminator. Formatted Input (Contd.)
  • 42. Practice: 2.10 Write a function to accept and display the element number and the weight of a Proton. The element number is an integer and weight is fractional.
  • 43. Practice: 2.10 (Contd.) Solution: #include<stdio.h> main() { int e_num; float e_wt; printf(“Enter the Element No. and Weight of a Proton\n”); scanf(“%d %f”, &e_num, &e_wt); fflush(stdin); printf(“The Element No. is: “, e_num); printf(“The weight of a Proton is: %f\n“, e_wt); }
  • 44. Practice: 2.11 Write a function to input a string of continuous characters with no white space as part of the input. The function should assign the input to variables of the types specified in the following table. The function should also print out each of the assigned data items in separate lines. Position of character from start of string Number of characters Type of argument to assign 1 2 int 3 4 float 7 2 char (string) 9 3 int
  • 45. Practice: 2.11 Solution: #include<stdio.h> main() { int i,j; char str[3]; float fnum; printf(“Enter a string of 11 chrs\n”); /*why 11: because 11 is the total length of */ /*input.*/ scanf(“%2d %4f %2s %3d”,&i, &fnum, str, &j); fflush(stdin); printf(“%2d\n %4f\n %2s\n %3d\n”, i, fnum, str, j); }
  • 46. In this session, you learned that: An operator is a symbol that is used to command the computer to do mathematical or logical manipulations. The operators in C language are classified into the following categories: Logical operators Unary operators Binary operators Ternary operator Compound assignment operators Increment/Decrement operators Summary
  • 47. The logical operators of C and their notations are as follows. The unary operator prefixed to an integer constant or variable tells the compiler to reverse the sign by subtracting the value or variable from zero. Binary operators in C language are + (add), - (subtract), * (multiply), / (divide), and % (modulo). Ternary operator offers a shorthand way of writing the commonly used if…else construct. Summary (Contd.) Operator Notation OR || AND && NOT !
  • 48. The syntax for using the ternary operator is: (test-expression) ? T-expression : F-expression; Compound assignment operators simplify the statements. Increment / Decrement operators are used to increment/decrement a variable by 1. A for loop is used when the number of execution of the loop is known. The components of a for loop construct are: initialization loop condition reinitialization (increment/decrement) Summary (Contd.)
  • 49. The sequence of execution of a complete for loop is: initialization evaluation of the loop condition the body of the loop reinitialization The break and continue statements are used to exit from loop control. The break statement is used to exit from all loop constructs ( while , do...while , and for ) and switch...case statements. The continue statement is used to skip all subsequent instructions and brings the control back to the loop. The function printf() is used for formatted output to standard output based on a format specification. Summary (Contd.)
  • 50. The syntax of the function printf() is: printf(format, datal, data 2,,..); The function scanf() is used for formatted input from standard input and provides many of the conversion facilities of the function printf() . The syntax of the function scanf() is: scanf (format, datal, data2, ...); Summary (Contd.)

Editor's Notes

  • #2: Begin the session by explaining the objectives of the session.
  • #3: Tell the students that they cannot create a useful programs without using the operators. A simple program such as adding two variables and showing the result needs the use of variables.
  • #4: Operator precedence is very important in a program. If the operator precedence is not considered then the program might produce an unexpected result. Give an example, which produces a result different from the expected result because of incorrect operator precedence.
  • #5: Use Slide 4 to test the student’s understanding on logical operators.
  • #8: Use Slide 7 to test the student’s understanding on unary operators.
  • #11: Use Slide 10 to test the student’s understanding on binary operators.
  • #14: Use this slide to test the student’s understanding on binary operators.
  • #18: Use this slide to test the student’s understanding on ternary operators.
  • #23: Use this slide to test the student’s understanding on increment/decrement operators.
  • #27: Explain definite and indefinite loops. Give some examples, which distinguish between definite and indefinite loops.
  • #29: The for loop consists of three parts : Initialization Condition Re-initialization (increment/decrement) In a for loop, after initialization, the condition is first checked. If the condition is valid, the body of the for loop is executed. For each iteration, re-initialization is done before the condition is checked again. Any or all of these parts may be left out of the construct. For example : for ( ; ; ) { } is an infinite loop (as there is no condition).
  • #31: Use this slide to test the student’s understanding on loops.
  • #34: Use this slide to test the student’s understanding on controlling the loop execution.
  • #37: When used with strings, the format string could be quite confusing. The statement: printf ( “%15.5s”, string) prints the first 5 characters of the string right-justified in 15 spaces. When used with floats, it represents the precision required.
  • #39: Use this slide to test the student’s understanding on formatted output.
  • #43: Use this slide to test the student’s understanding on formatted input.
  • #45: Use this slide to test the student’s understanding on formatted input-output.
  • #46: The output buffer is flushed only if it is full, or if the string to be printed contains a newline character at the end or if it is explicitly flushed using fflush () . The last option is used if the string does not contain a new line character. The string might not be displayed at the right place at the right time otherwise.
  • #47: Use this and the next 4 slides to summarize this session.
  翻译: