Outline if...else...elseif Statements Switch Loops Functions Arrays Forms.
Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements /...
Transcript of Unit 1: Introduction to Computer Fundamentals … · Selection Statements Control Statements /...
Saurabh Khatri
Lecturer
Department of Computer Technology
VIT, Pune
Unit 2
Selection Statements
Control Statements / Branch Statements
IF-ELSE
Nested IF-ELSE
Conditional Expression
Switch statements
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)
What’s the output?
Int I;
Printf (“Enter value of i “);
Scanf (“%d”, &i);
If (i==5);
Printf(“You entered 5\n”);
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”);
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
ELSE-IF
Multi-way decision
if (expression)
statement
else if (expression)
statement
...
else
statement
Last else is optional
Improves efficiency – by skipping evaluation
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
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
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
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.
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
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.
© 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
© 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
© 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
© 2004 Pearson Addison-Wesley.
All rights reserved5-17
Logic of a for loop
statement
true
condition
evaluated
false
increment
initialization
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);
}
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;
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
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
The break statement
Syntax:
Effect:
break;
• When the break statement is executed inside a loop-
statement, the loop-statement is terminated immediately
The break statement (cont.)
Schematically:
The continue statement
Syntax:
continue;
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
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
The continue statement (cont.)
Schematically:
Write the Output
Write the Output