Beruflich Dokumente
Kultur Dokumente
• The if Statement
• The if-else Statement
• Nested if Statements
• The else if Ladder
• The switch Statement
3
➢ control structure
✓
▪
✓
▪
✓
▪
if…else
if…else
else if
yes expressi no
on
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
➢
➢
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“);
……
▪
▪
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
expression
break statement
switch- control flow
52
• To learn and appreciate the following concepts
55
•
–
–
branching Statement1
Statement2
– Statement3
Statement4
looping Statement5
Statement6
Statement7
Statement8
Test False
Cond Body of
ition The loop
True
Body of
Test True
The loop
Conditi
on
False
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
✓
✓
✓
✓
✓
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
✓
✓
yes loop_exp
ression
2
No
Next statement 4
1 init_expression
loop_cond no
5 2
ition
yes
3 Program statement
4 Loop expression
Next Statement
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)
}
#include <stdio.h>
int main()
{
int n, triangularNumber=0;
…
…
• …
•
•
•
……… ………
Exit Exit
From ……… From ………
loop loop
…
………… …………
………… { ………
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;
……… do
{
Statement-1;
Statement-2;
If(condition)
continue;
Statement-3;
Statement-4;
} while(…);
Next_statement Next_statement
▪enum
▪ Enumerated data type - a type with restricted set of values.
▪
typedef int marks;
typedef float units;
←
←
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