You are on page 1of 34

Conditionals, Loops, Other Statements

Dr. Odelu Vanga

Department of Computer Science and Information Systems


Birla Institute of Technology and Science Pilani
Hyderabad Campus
odelu.vanga@hyderabad.bits-pilani.ac.in

February 11, 2019

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 1 / 34


Today’s Topics

Reading using scanf


if-else Statements
switch Statement
Loops: while, for, do-while

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 2 / 34


Reading using function scanf

Function scanf is written in the following form:

scanf (format − control − string, other − arguments);

format-control-string
Describes the formats of the input
Consists of conversion specifiers and literal characters
Other-arguments are pointers to variables in which the input will
be stored.
It has the following input formatting capabilities:
Inputting all types of data.
Inputting specific characters from an input stream.
Skipping specific characters in the input stream.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 3 / 34


Reading using function scanf

Function scanf is written in the following form:

scanf (format-control-string, other -arguments);

format-control-string
Describes the formats of the input
Consists of conversion specifiers and literal characters
Other-arguments are pointers to variables in which the input will be
stored.
It has the following input formatting capabilities:
Inputting all types of data.
Inputting specific characters from an input stream.
Skipping specific characters in the input stream.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 4 / 34


Reading using function scanf

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 5 / 34


Reading using function scanf

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 6 / 34


Reading using function scanf

Reads integers with the various integer conversion specifiers and


displays the integers as decimal numbers.
Conversion specifier %i can input decimal, octal and hexadecimal
integers.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 7 / 34


Reading using function scanf
When inputting floating-point numbers, any of the floating-point conversion specifiers e, E,
f, g or G can be used.
Reads three floating-point numbers, one with each of the three types of floating conversion
specifiers, and displays all three numbers with conversion specifier f.
The program output confirms the fact that floating-point values are imprecise-this is
highlighted by the third value printed.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 8 / 34


Reading using function scanf

In above case, scanf would not eliminate the unnecessary characters.


For this reason, scanf provides the assignment suppression character ∗.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 9 / 34


Conditional Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 10 / 34


Conditional Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 11 / 34


Conditional Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 12 / 34


Conditional Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 13 / 34


Conditional Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 14 / 34


Conditional Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 15 / 34


Conditional Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 16 / 34


Conditional Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 17 / 34


Conditional Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 18 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 19 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 20 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 21 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 22 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 23 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 24 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 25 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 26 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 27 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 28 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 29 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 30 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 31 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 32 / 34


Iterative Statements

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 33 / 34


.

Write a program to count


digits, white space characters, and other characters ?

Thank You

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 February 11, 2019 34 / 34

You might also like