0% found this document useful (0 votes)
13 views

Chapter 2 Introduction to C part 3 Decision if case

Uploaded by

qadri.tayyaba25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Chapter 2 Introduction to C part 3 Decision if case

Uploaded by

qadri.tayyaba25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Jamia Millia Islamia

A Central University

CS-101:Fundamentals
of Computing
Mr. Hannan Mansoor
Department of Computer Engineering,
FET, JMI
2 Unit 2:
INTRODUCTION TO C PROGRAMMING
 Algorithms,  decision controls,
 flow chart,  if and else, conditional operator

 The C character set,  for loop, while loop and do-while


loop,
 constants,
 switch case
 variable,
 user defined functions,
 keywords,
 call by value and by reference,
 operator and
 array
 Expressions.
 single dimensional,
 2D matrix, multidimensional arrays.

Department of Computer Engineering, JMI CS101


3
Decision Control and Branching
if, if-else

Department of Computer Engineering, JMI CS101


4 Decision Making

 The statements of a C program normally executes sequentially in the order in


which they appear.
 There are time when we need to change the execution of program based on some
conditions.
 C language possess decision making capabilities and supports the following
statements known as control or decision making statements:
I. if statement
II. switch statement
III. conditional control statement
IV. goto statement

Department of Computer Engineering, JMI CS101


5 If statement

 The keyword “if” tells the compiler that the code followed after it is a decision
control instruction.

if( expression/condition)
execute the statement;

 Generally we express a condition using C’s ‘relational’ operators

Department of Computer Engineering, JMI CS101


6 Control flow- flow chart

if condition is “false”.
Condition

if condition is “true”.

Conditional
Code/statement

Department of Computer Engineering, JMI CS101


7 P5_Control_ifstatement1

Department of Computer Engineering, JMI CS101


8 P5_Control_ifstatement2

Department of Computer Engineering, JMI CS101


9 P5_Control_ifstatement2 : another output

Department of Computer Engineering, JMI CS101


10 P5_Control_ifstatement3

Department of Computer Engineering, JMI CS101


11 P5_Control_ifstatement4

Department of Computer Engineering, JMI CS101


12 P5_Control_ifstatement5

Department of Computer Engineering, JMI CS101


13 Control flow : if-else

if condition is “false”.
Condition

if condition is “true”.

Conditional else
Code/statement code

Department of Computer Engineering, JMI CS101


14 P5_Control_ifstatement6

Department of Computer Engineering, JMI CS101


15 P5_Control_ifstatement7: Program to check
whether the given no is odd or even

Department of Computer Engineering, JMI CS101


16 P5_Control_ifstatement8_Largestof2no

Department of Computer Engineering, JMI CS101


17 P5_Control_ifstatement8_Largestof3nob

Department of Computer Engineering, JMI CS101


18

Department of Computer Engineering, JMI CS101


19

Department of Computer Engineering, JMI CS101


20 P5_Control_ifstatement8_dividedbyzero: Program to
divide the first number by the second.

Department of Computer Engineering, JMI CS101


21 P5_Control_ifstatement_9_largestof3no

Department of Computer Engineering, JMI CS101


22 Else if

 if(condition 1 )
 Conditional statement 1
 else if(condition 2)
 Conditional statement 2
 else if(condition 3)
 Conditional statement 3
 else
 statement 4

Department of Computer Engineering, JMI CS101


23 P5_Control_elseif_9_largestof3no

Department of Computer Engineering, JMI CS101


24 P5_Control_ifstatement7

Department of Computer Engineering, JMI CS101


25 P5_Control_ifstatement8_dividedbyzero

Department of Computer Engineering, JMI CS101


26

Switch statement in C

Department of Computer Engineering, JMI CS101


27 Switch

 C has a built-in multi-way decision statement known as switch.


 The control statement that allows us to make a decision from the number of
choices is called a switch.
 The switch statement tests the value of a given variable (or expression) against
list of case values and when a match is found, a block of statement with that case
is executed.

Department of Computer Engineering, JMI CS101


28

 The general form of the switch statement is as shown below:


 switch(input)
{
case 1: statement1;
case 2: statement2;
}
 The expression is an integer expression or characters.
 Value-1, value2,….. are constants or constant expressions (evaluable to an
integral constant) and are known as case labels.

Department of Computer Engineering, JMI CS101


29

Department of Computer Engineering, JMI CS101


30 P6_switch_1

Department of Computer Engineering, JMI CS101


31 P6_switch_1

Department of Computer Engineering, JMI CS101


32 P6_switch_2weekday

Department of Computer Engineering, JMI CS101


33

 Case 0,1,2 can be written. Only integers are allowed.


 If conditions then use constants.
 Case need not be contiguous.
 Using default is optimal.
 “default” can be input put anywhere in switch. (i.e. start, middle, last).

Department of Computer Engineering, JMI CS101


34 P6_switch_3calculator

Department of Computer Engineering, JMI CS101


35

Department of Computer Engineering, JMI CS101


36 WAP in C for finding the largest number using
switch case

Department of Computer Engineering, JMI CS101


37 WAP in C for finding the largest number using
switch case - Output

Department of Computer Engineering, JMI CS101


38 Reference Books

 Thareja, R. Computer Fundamentals & Programming in C”, Oxford University


Press
 Kamthane, A. N. (2011). Programming in C, 2/e. Pearson Education India.
 Doja, M. N. (2005). Fundamentals of Computers and Information Technology
 Yashwant, K. Let us C. 8th edition, BPB publication.
 Balagurusamy, E. (2012). programming in ANSI C. Tata McGraw-Hill Education.
 Ritchie, D. M., Kernighan, B. W., & Lesk, M. E. (1988). The C programming
language. Englewood Cliffs: Prentice Hall.
 Herbert Schildt C-The Complete Reference., Tata McGraw Hill Edition

Department of Computer Engineering, JMI CS101


39

Thank You

Department of Computer Engineering, JMI CS101

You might also like