Beruflich Dokumente
Kultur Dokumente
Studies in Engineering
Programming in C
Lecture3
1
Decisions
z The if statement
z The if-else statement
z The else-if construct
z The switch statement
z The conditional operator
2
The If Statement
z Syntax: if (expression) statement;
z if(expression)
z statement;
3
z Void main (void)
z {
char ch;
ch=getche();
if(ch==‘y’)
printf(“Yes”);
}
4
Multiple statements with if
z You can group multiple statements together
with braces:
if (expression) {
statement 1;
statement 2;
statement 3;
}
5
z Void main (void)
z {
char ch;
ch=getche();
if(ch==‘y’)
{
printf(“Yes ”);
printf(“you typed y”);
}
}
6
Multiple statements
if(x > y)
{
x = y + 4;
z = 2 + y;
}
7
Nested if statements
z Void main (void)
z {
char ch;
ch1=getche();
ch2=getche();
if(ch1==‘n’)
if(ch2==‘o’)
printf(“you typed no”);
}
8
The If/Else Statement
z Syntax:
if (expression)
statement_1;
else
statement_2;
if (myVal < 3)
printf(“myVal is less than 3.\n”);
else
printf(“myVal is greater than or equal to 3.\n”);
9
If else
10
C Operators in if statement
z More Identical operators:
z Equal to if (x==10)
z Not equal to if (x!=10)
z Less than if (x<10)
z Greater than if (x>10)
z Less than or equal to if (x<=10)
z Greater than or equal to if (x>=10)
11
Nested if-else
z It is possible to nest an entire if-else in the body of
an if or in the body of an else statement.
z If(x==10)
Printf(“x is ten”);
else
{
if(x<10)
printf(“x is less than ten”);
else
printf(“x is greater than 10”);
}
12
Which if gets the else?
z An else is associated with the last if that doesn’t have its own
else.
if(tmp<80){
if(tmp>60)
printf(“Nice day!”);
else
printf(“its cold!”);
}
else
printf(“sure is hot!”);
13
Logical Operators
z Logical AND
if (x==1 && y==2)
z Logical OR
if (x==1 || y==2)
z Logical NOT
if (!x) …
14
Conditional operator
15
Equivalent structure
16
Another conditional example
rate = (hours > 40) ? 3.50 : 0.25;
17
Loops
z for
z While
z Do While
z Nested loops
z Break, continue and goto statements
18
Loops
z Loops allow repetition
z Repeat a series of statements
z Not reasonable to copy statements
multiple times
z Need a way to repeat a block of code
19
The For Loop
z Syntax:
z for (initialization; test; increment)
{
statements;
}
z The for loop will first perform the initialization.
Then, as long as test is TRUE, it will execute
statements. After each execution, it will
increment.
20
Example
z Known number of iterations
int count;
for(count=1;count<=10;count++)
printf(“%d \n”,count);
21
z multiple initializations
z multiple increments/decrements
z Only one test expression
22
Incremental Operators
z i++ or ++i Í==Î i = i + 1
z i-- or --i Í==Î i = i – 1
z i += a Í==Î i = i + a
z i -= a Í==Î i = i - a
z i *= a Í==Î i = i * a
z i /= a Í==Î i = i / a
23
Multiple statements in loops
int count, total;
for(count=1;count<=10;count++)
{
total = count+total;
printf(“%d,%d\n”,count,total);
}
24
for (x=20 ; x <= 80; x +=10)
from 20 to 80 in steps of 10
25
/* Print a Fahrenheit-to-Celsius conversion table */
#include <stdio.h>
void main (void)
{
int fahr;
float celsius;
int lower = 0, upper = 300;
int step = 20;
}
26
Special cases
z int i;
for(i=1;i<=10;)
{
Printf(“%d\n”,i);
i=i+1;
}
z int i=1;
for(;i<=10;i++)
{
Printf(“%d\n”,i);
}
27
Special cases
z int i=0;
for(;i<=10;)
{
printf(“%d\n”,i);
i=i+1;
}
z int i;
for(i=0;i++<=10;)
{
printf(“%d\n”,i);
}
z int i;
for(i=0;++i<=10;)
{
printf(“%d\n”,i);
} 28
While loop
while (condition)
The statements are
{ executed as long as
statement(s); condition is true
}
29
While loop
degree =0;
while (degree <= 360)
{
degree += 1;
}
30
z While loops are more appropriate than for
loops when the condition that terminates
the loop occurs unexpectedly.
31
The While Loop
z An example while loop looks like this:
void main(void)
{
char ch;
while (ch != ‘Q’ || ch !=‘q’)
{
…
ch = getchar( );
}
}
32
Example – factorial
#include <stdio.h>
int main(void)
{
int i,n,fact = 1;
printf("Enter a number\n");
scanf("%d", &n);
i=1; This is a counter
while (i<=n)
{
fact = fact*i; Every iteration i is
i++; incremented by 1.
} Equivalent to i=i+1.
printf("the factorial is %d\n", fact);
return 0;
}
33
Do while loop
z Syntax:
do
{
statement;
}while(test);
34
Do While
int selection = 0;
do
{
printf("\n" );
printf("\n1 - Add a Record" );
printf("\n2 - Change a record");
printf("\n3 - Delete a record");
printf("\n4 - Quit");
printf("\n" );
printf("\nEnter a selection: " );
scanf("%d", &selection );
35
Difference between while & do while
while (4<=1)
{
printf(“anything”);
}
do
{
printf(“anything”);
} while (4<=1);
36
Nested Loops
37
Nested For Loop
/* Print a rectangle of *. The height and width are defined by the user */
#include <stdio.h>
int main(void)
{
int i,j;
int height, width;
printf("\n");
}
}
38
Break in a loop
z When break is encountered, the loop is
exited regardless of whether the condition
is still true.
z The program then continues to run from
the first line after the while loop.
39
Break
z With loops, break can be used to force an early exit
from the loop, or to implement a loop with a test to
exit in the middle of the loop body. A break within a
loop should always be protected within an if
statement which provides the test to control the exit
condition.
z If called within a nested loop, break breaks out of
the inner loop only.
40
int loop;
for(loop=0;loop<50;loop++)
{
if(loop==10)
break;
printf("%d\n",loop);
}
Only numbers 0 through 9 are printed.
41
Continue statement
z Takes back to the beginning of the loop,
bypassing any statements not yet executed.
z It’s use is avoided whenever possible
because it can make program difficult to read
and debug by confusing the normal flow of
operations in the loop.
42
Example
z int i,j;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(i==j)
continue;
printf(“%d %d”,i,j);
}
}
43
continue
44
Example
int loop;
for(loop=0;loop<100;loop++)
{
if(loop==50)
continue;
printf("%i\n",loop);
}
45
Go to
z C has a goto statement which permits
unstructured jumps to be made.
z Examples:
goto skip_point;
printf("This part was skipped.\n");
skip_point: printf("Hi there!\n");
46
SUMMARY
47