unit 1: introduction to computer fundamentals … · selection statements control statements /...
TRANSCRIPT
![Page 1: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/1.jpg)
Saurabh Khatri
Lecturer
Department of Computer Technology
VIT, Pune
Unit 2
![Page 2: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/2.jpg)
Selection Statements
Control Statements / Branch Statements
IF-ELSE
Nested IF-ELSE
Conditional Expression
Switch statements
![Page 3: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/3.jpg)
IF…ELSE If (expression) Statement1
Else Statement2
Else is optional
Difference between only if and if-else
Use braces and indents to avoid mistakes
Scope of if and scope of else
Non zero means true
Coding shortcuts (expression) vs (expression != 0) (!expression) vs (expression == 0)
![Page 4: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/4.jpg)
What’s the output?
Int I;
Printf (“Enter value of i “);
Scanf (“%d”, &i);
If (i==5);
Printf(“You entered 5\n”);
![Page 5: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/5.jpg)
Nested IF ELSE
Who does the “else” belong to?
if (n>=0)
for (i=0; i<n; i++)
if (s[i]>0) {
printf(“something”);
return i;
}
else
printf( (“error: n is negative”);
![Page 6: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/6.jpg)
Sample Program
Write a C program to develop Temp. Detector
Between 0 to 10
Very cold!
10 to 20
cool
20 to 30
Pleasant
30 to 40
Hot
![Page 7: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/7.jpg)
ELSE-IF
Multi-way decision
if (expression)
statement
else if (expression)
statement
...
else
statement
Last else is optional
Improves efficiency – by skipping evaluation
![Page 8: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/8.jpg)
SWITCH Multi-way decision
Only to match different constant integer values
All case expressions must be different
All subsequent statements are executed, until
break or return is encountered
Use of Default case
“cases” and “default” can occur in any order
![Page 9: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/9.jpg)
More on SWITCH
Multiple statements do not need to be in braces
What happens when there is a statement without
“case”?
No error, but never gets executed
Switch is more efficient than equivalent if-else –
especially when there are many cases
![Page 10: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/10.jpg)
Sample Program
Write a C program to develop Temp. Detector
using Switch
Between 0 to 10
Very cold!
10 to 20
cool
20 to 30
Pleasant
30 to 40
Hot
![Page 11: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/11.jpg)
Home Assignment 1
Convert your name to decimal equivalent and
then convert to Octal, Binary and Hexa-
Decimal.
Five Problems:
To be solved on a notebook
Submit to CR by 23rd Morning 10.30 am – Sign
on the attendance.
Class test on Saturday Unit 1 collect ppts from
CR.
![Page 12: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/12.jpg)
Iteration Statements A repetition structure allows the programmer to
specify that an action is to be repeated while some
condition remains true.
There are three repetition structures in C,
For
Do-While
While
![Page 13: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/13.jpg)
The while Repetition StructureInit;
while ( condition )
{
statement(s)
Inc/dec
}
If the condition is true, the statement is executed, Then the
condition is evaluated again, and if it is still true, the statement is
executed again till condition becomes false.
The braces are not required if the loop body contains only a single
statement.
![Page 14: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/14.jpg)
© 2004 Pearson Addison-Wesley.
All rights reserved5-14
The do-while Statement A do-while statement (also called a do loop) has the
following syntax:
Init;
do{
statement;
}while ( condition );
• The statement is executed once initially, and then the
condition is evaluated
• The statement is executed repeatedly until the condition
becomes false
![Page 15: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/15.jpg)
© 2004 Pearson Addison-Wesley.
All rights reserved5-15
Comparing while and do
statement
true false
condition
evaluated
The while Loop
true
condition
evaluated
statement
false
The do Loop
![Page 16: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/16.jpg)
© 2004 Pearson Addison-Wesley.
All rights reserved5-16
The for Statement A for statement has the following syntax:
for ( initialization ; condition ; increment ){
statement;
}
The initialization
is executed once
before the loop begins
The statement is
executed until thecondition becomes false
The increment portion is executed at
the end of each iteration
![Page 17: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/17.jpg)
© 2004 Pearson Addison-Wesley.
All rights reserved5-17
Logic of a for loop
statement
true
condition
evaluated
false
increment
initialization
![Page 18: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/18.jpg)
Code to Evaluate Sum of N natural No.
Void main()
{
int n, i , sum = 0 ;
Scanf(“%d”, &n);
For(i= 0; i<n;i++)
sum = sum +i ;
Printf(“Sum is %d”, sum);
}
![Page 19: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/19.jpg)
Goto Statement
A goto statement in C programming language provides an
unconditional jump from the goto to a labeled statement in
the same function.
Syntax:
label: statement;
……
goto label;
![Page 20: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/20.jpg)
![Page 21: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/21.jpg)
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
![Page 22: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/22.jpg)
Introduction
There are 2 special statements that can affect the execution
of loop statements (such as a while- for- statement)
The special statements are:
• break
• continue
![Page 23: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/23.jpg)
The break statement
Syntax:
Effect:
break;
• When the break statement is executed inside a loop-
statement, the loop-statement is terminated immediately
![Page 24: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/24.jpg)
The break statement (cont.)
Schematically:
![Page 25: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/25.jpg)
The continue statement
Syntax:
continue;
![Page 26: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/26.jpg)
The continue statement (cont.)
Effect:
•When the continue statement is executed inside a loop-
statement, the program will skip over the remainder of the loop-
body to the end of the loop
![Page 27: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/27.jpg)
The continue statement (cont.)
Effect of a continue statement in a while-loop:
• In the case of a while-loop, when the program reaches end of the loop, the program
will jump back to the testing of the loop-continuation-condition
• the program will skip over the remainder of the loop-body to the end of
the loop
![Page 28: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/28.jpg)
The continue statement (cont.)
Schematically:
![Page 29: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/29.jpg)
Write the Output
![Page 30: Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements / Branch Statements IF-ELSE Nested IF-ELSE Conditional Expression Switch statements](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8a669779b30f78f4345b1e/html5/thumbnails/30.jpg)
Write the Output