Sie sind auf Seite 1von 102

• To learn and appreciate the following concepts

• The if Statement
• The if-else Statement
• Nested if Statements
• The else if Ladder
• The switch Statement

8/23/2018 CSE 1001 Department of CSE 2


• At the end of session student will be able to learn and
understand
• The if Statement
• The if-else Statement
• Nested if Statements
• The else if Ladder
• The switch Statement

3
➢ control structure




CSE 1001 Department of CSE 4


8/23/2018
CSE 1001 Department of CSE 5
8/23/2018
if

if…else

if…else

else if

8/23/2018 CSE 1001 Department of CSE 6


8/23/2018 CSE 1001 Department of CSE 7
explanation

if (hours > 70)

8/23/2018 CSE 1001 Department of CSE 8


8/23/2018 CSE 1001 Department of CSE 9
8/23/2018 CSE 1001 Department of CSE 10
if

8/23/2018 CSE 1001 Department of CSE 11


if-else

8/23/2018 CSE 1001 Department of CSE 12


8/23/2018 CSE 1001 Department of CSE 13
if-else

yes expressi no
on

Program statement Program statement


1 2

8/23/2018 CSE 1001 Department of CSE 14


8/23/2018 CSE 1001 Department of CSE 15
8/23/2018 CSE 1001 Department of CSE 16
if-else

8/23/2018 CSE 1001 Department of CSE 17


8/23/2018 CSE 1001 Department of CSE 18
8/23/2018 CSE 1001 Department of CSE 19
if (x >= 5 && x <= 10)
printf(“in range“);

if (5 <= x <= 10)


printf(“in range“);

8/23/2018 CSE 1001 Department of CSE 20


if (x >= 5 && x <= 10)
printf(“in range“);

if (5 <= x <= 10)


printf(“in range“);

Syntactically correct, but semantically an error !!!

Because the order of evaluation for the <= operator is left-to-right, the
test expression is interpreted as follows:
(5<= x) <= 10
The subexpression 5 <= x either has the value 1 (for true) or 0 (for
false). Either value is less than 10, so the whole expression is always
true, regardless of x !
8/23/2018 CSE 1001 Department of CSE 21
if-else

8/23/2018 CSE 1001 Department of CSE 22


Explanation


8/23/2018 CSE 1001 Department of CSE 23


8/23/2018 CSE 1001 Department of CSE 24
if

if (number > 5)
if (number < 10)
printf(“1111\n“);
else printf(“2222\n“);

if (number > 5) {
if (number < 10)
printf(“1111\n“);
}
else printf(“2222\n“);

8/23/2018 CSE 1001 Department of CSE 25


if ladder

……

8/23/2018 CSE 1001 Department of CSE 26


Explanation


8/23/2018 CSE 1001 Department of CSE 27


else-if ladder Flow of control

True False
Condi
tion-1

True False
statement-1 Condi
tion-2

True False

statement-2 Condi
tion-3

True False
statement-3 Condi
tion-n

statement-n

default statement

next statement

8/23/2018 CSE 1001 Department of CSE 28


8/23/2018 CSE 1001 Department of CSE 29
For inputs
imarks= 46
grade = D
imarks= 64
grade = B

8/23/2018 CSE 1001 Department of CSE 30


else-if

8/23/2018 CSE 1001 Department of CSE 31


8/23/2018 CSE 1001 Department of CSE 32

8/23/2018 CSE 1001 Department of CSE 33


8/23/2018 CSE 1001 Department of CSE 34

8/23/2018 CSE 1001 Department of CSE 35


switch

8/23/2018 CSE 1001 Department of CSE 36


expression

expression

break statement
switch- control flow

8/23/2018 CSE 1001 Department of CSE 38


example 1

8/23/2018 CSE 1001 Department of CSE 39


example 2

8/23/2018 CSE 1001 Department of CSE 40


8/23/2018 CSE 1001 Department of CSE 41
8/23/2018 CSE 1001 Department of CSE 42
switch

8/23/2018 CSE 1001 Department of CSE 43


8/23/2018 CSE 1001 Department of CSE 44
8/23/2018 CSE 1001 Department of CSE 45
IVS E&R CCD

8/23/2018 CSE 1001 Department of CSE 46


8/23/2018 CSE 1001 Department of CSE 47
8/23/2018 CSE 1001 Department of CSE 48
8/23/2018 CSE 1001 Department of CSE 49
8/23/2018 CSE 1001 Department of CSE 50
8/23/2018 CSE 1001 Department of CSE 51





52
• To learn and appreciate the following concepts

• The for Statement


• Nested for Loops
• for Loop Variants
• The while Statement
• The do Statement
• The break Statement
• The continue Statement
• Typedef and Enum

8/23/2018 CSE 1001 Department of CSE 54










55



branching Statement1
Statement2
– Statement3
Statement4
looping Statement5
Statement6
Statement7
Statement8

8/23/2018 CSE 1001 Department of CSE 56


8/23/2018 CSE 1001 Department of CSE 57


8/23/2018 CSE 1001 Department of CSE 58

✓ while
✓ do while
✓ for

8/23/2018 CSE 1001 Department of CSE 59




✓ →

8/23/2018 CSE 1001 Department of CSE 60


Entry
Entry

Test False
Cond Body of
ition The loop

True

Body of
Test True
The loop
Conditi
on

False

8/23/2018 CSE 1001 Department of CSE 61


Statement before triangularNumber =
loop 0

init_expression n=1

no
loop_condition n<=200

yes
triangularNumber =
Statement(s) triangularNumber + n

loop_expression n=n+1

Print
Statement after loop triangularNumber

8/23/2018 CSE 1001 Department of CSE 62


Note: braces optional if
only one statement.




8/23/2018 CSE 1001 Department of CSE 63


while

while ( expression )
program statement Loop with the
test in the
statement before beginning !
loop Body might
never be
executed !

Loop_ex
3 1 4
pression No
yes
2 statement (s)

Next statement

8/23/2018 CSE 1001 Department of CSE 64


8/23/2018 CSE 1001 Department of CSE 65
8/23/2018 CSE 1001 Department of CSE 66



8/23/2018 CSE 1001 Department of CSE 67


do
do
program statement
while ( loop_expression );
Loop with the
test at the end !
Body is
executed at least
3 1 once !
Statement(s)

yes loop_exp
ression
2
No

Next statement 4

8/23/2018 CSE 1001 Department of CSE 68


do
{
sum = sum + n;
n = n +1;
} while (n < =100);

8/23/2018 CSE 1001 Department of CSE 69


8/23/2018 CSE 1001 Department of CSE 70
for’
for ( init_expression; loop_condition; loop_expression )
{ program statement(s)
}

1 init_expression

loop_cond no
5 2
ition

yes

3 Program statement

4 Loop expression

Next Statement

8/23/2018 CSE 1001 Department of CSE 71


for

8/23/2018 CSE 1001 Department of CSE 72


for
sum = 0;

no

1 2 5 4
for ( n = 1; nyes
<= 200; n = n + 1 )
{ sum = sum + n; }

Next Statement
for ( init_expression; loop_condition; loop_expression )
{ program statement(s)
}

8/23/2018 CSE 1001 Department of CSE 73


for(n = 1; n <=100; n=n + 1)
{
sum=sum + n;
}

8/23/2018 CSE 1001 Department of CSE 74


What is wrong here ?


Does the loop end?

8/23/2018 CSE 1001 Department of CSE 75


for

// Program to generate a table of triangular numbers

#include <stdio.h>
int main()
{
int n, triangularNumber=0;

printf(“TABLE OF TRIANGULAR NUMBERS\n\n“);


printf(“Sum from 1 to n\n“);

for ( n = 1; n <= 10; n++ )


{
triangularNumber += n;
printf(“The %d th triangular number is
%d\n”,n,triangularNumber);
}
return 0;
}

8/23/2018 CSE 1001 Department of CSE 76



8/23/2018 CSE 1001 Department of CSE 77


Remember indentations!

8/23/2018 CSE 1001 Department of CSE 78


8/23/2018 CSE 1001 Department of CSE 79
• …

• …

8/23/2018 CSE 1001 Department of CSE 80






8/23/2018 CSE 1001 Department of CSE 81


8/23/2018 CSE 1001 Department of CSE 82


………
…… ……
………… …………

……… ………
Exit Exit
From ……… From ………
loop loop

………… …………

8/23/2018 CSE 1001 Department of CSE 83


for (……….)
for {…….
{……. for(……..)

………… { ………
If(condition)
If(condition)
break;
break;
… stmts of inner loop;
Exit ………
From } // inner for loop ends
loop ………. Exit ….stmts of outer loop;
From
} } // outer for loop ends
inner
……next Stmts; loop …… next Stmts;

8/23/2018 CSE 1001 Department of CSE 84


int j, prime=1;
scanf(“%d”,&N);
for( int j=2; j<N; j++ )
{
if( (N % j) == 0)
{
prime=0;
break; /* break out of for loop */
}
}
if (prime == 1)
printf(“%d is a prime no”,N);
else
printf(“%d is a not a prime no”,N);

8/23/2018 CSE 1001 Department of CSE 85


scanf(“%d %d”,&m,&n);

for( int i=m; i<=n; i++)


{
int prime=1;
for( int j=2; j<i; j++ )
{
if( i % j == 0)
{
prime=0;
break; /* break out of inner loop */
}
}
if (prime == 1) printf(“%d\t”,i);
}
8/23/2018 CSE 1001 Department of CSE 86

……… do
{
Statement-1;
Statement-2;

If(condition)
continue;

Statement-3;
Statement-4;
} while(…);

Next_statement Next_statement

8/23/2018 CSE 1001 Department of CSE 87


10 11 12 15

8/23/2018 CSE 1001 Department of CSE 88


for ( i = 1 ; i <= 2 ; i++ )
{
for ( j = 1 ; j <= 2 ; j++ )
{
if ( i == j ) 1 2
continue ; 2 1

printf(“\n %d\t %d\n ;


}
}

8/23/2018 CSE 1001 Department of CSE 89


▪typedef
▪ Type definition - lets you define your own identifiers.

▪enum
▪ Enumerated data type - a type with restricted set of values.

8/23/2018 CSE 1001 Department of CSE 90


▪ typedef type identifier;


typedef int marks;
typedef float units;

marks m1,m2 ; //m1 & m2are declared as integer variables


units u1, u2; //u1 & u2 are declared as floating point variables

8/23/2018 CSE 1001 Department of CSE 91


8/23/2018 CSE 1001 Department of CSE 92


enum week { Monday =1, Tuesday,
Wednesday, Thursday,
Friday, Saturday,
Sunday};

CSE 1001 Department of CSE 93


8/23/2018
8/23/2018 CSE 1001 Department of CSE 94
← ←


8/23/2018 CSE 1001 Department of CSE 95


8/23/2018 CSE 1001 Department of CSE 96

8/23/2018 CSE 1001 Department of CSE 97


e.g.- num = 31467
OUTPUT
2 even & 3 odd digits

8/23/2018 CSE 1001 Department of CSE 98


Palindrome (number)
e.g.- 121

8/23/2018 CSE 1001 Department of CSE 99


Armstrong Number
e.g. - 371
∑ (cubes of digits )= number
33 + 73 + 13 = 371

10
8/23/2018 CSE 1001 Department of CSE
0






• Summary
• The for Statement
• Nested for Loops
• for Loop Variants
• The while Statement
• The do Statement
• The break Statement
• The continue Statement
• Typedef and Enum

102

Das könnte Ihnen auch gefallen