Control Statements
Control Statements
Control Statements
1
01/18/2020
Examples
2
01/18/2020
Zero
Indicates FALSE.
Non-zero
Indicates TRUE.
Typically the condition TRUE is represented by the
3
01/18/2020
A decision can be
made on any
false expression.
grade >= 60
zero - false
true nonzero - true
if (grade>=60)
{
\
\
}
Example
#include <stdio.h>
main()
{
int a,b,c;
scanf ( %d %d %d , &a, &b, &c);
if ((a>=b) && (a>=c))
printf ( \n The largest number is: %d , a);
if ((b>=a) && (b>=c))
printf ( \n The largest number is: %d , b);
if ((c>=a) && (c>=b))
printf ( \n The largest number is: %d , c);
}
4
01/18/2020
Dangerous error
Does not ordinarily cause syntax errors.
Any expression that produces a value can be used in
control structures.
Nonzero values are true, zero values are false.
Example:
if (payCode = = 4)
printf( You get a bonus!\n );
if (payCode = 4)
printf( You get a bonus!\n );
WRONG
Some Examples
if (10<20) { a = b + c; printf ( %d , a); }
5
01/18/2020
false true
grade >= 60
if ( grade >= 60 )
printf ("Passed\n");
else
printf ("Failed\n");
6
01/18/2020
part.
Confusion??
Rule to be remembered:
if e1 s1
else if e2 s2
if e1 s1
else if e2 s2
?
else s3
if e1 if e2 s1
else s2
else s3
if e1 if e2 s1
else s2
7
01/18/2020
if e1 s1 if e1 s1
else if e2 s2 else if e2 s2
if e1 s1 if e1 s1
else if e2 s2 else if e2 s2
else s3 else s3
if e1 if e2 s1 if e1 if e2 s1
else s2 else s2
else s3 else s3
if e1 if e2 s1 if e1 if e2 s1
else s2 else s2
Programming and Data Structure 15
Example
#include <stdio.h>
main()
{
int a,b,c;
scanf ( %d %d %d , &a, &b, &c);
if (a>=b)
if (a>=c)
printf ( \n The largest is: %d , a);
else printf ( \n The largest is: %d , c);
else
if (b>=c)
printf ( \n The largest is: %d , b);
else printf ( \n The largest is: %d , c);
}
8
01/18/2020
Example
#include <stdio.h>
main()
{
int a,b,c;
scanf ( %d %d %d , &a, &b, &c);
if ((a>=b) && (a>=c))
printf ( \n Largest number is: %d , a);
else if (b>c)
printf ( \n Largest number is: %d , b);
else
printf ( \n Largest number is: %d , c);
}
Returns a value
9
01/18/2020
Examples:
switch (expression) {
case expression-
case expression-
case expression-
10
01/18/2020
Example
switch (letter)
{
case 'A':
printf ( First letter \n );
break;
case 'Z':
printf ( Last letter \n );
break;
default :
printf ( Middle letter \n );
break;
}
11
01/18/2020
Example
switch (choice = getchar()) {
printf ( RED \n );
break;
printf ( GREEN \n );
break;
printf ( BLUE \n );
break;
default: printf ( Invalid choice \n );
Example
switch (choice = toupper(getchar())) {
printf ( RED \n );
break;
printf ( GREEN \n );
break;
printf ( BLUE \n );
break;
default: printf ( Invalid choice \n );
}
12
01/18/2020
int main () { -
int operand1, operand2; result = operand1 - operand2;
int result = 0; break;
char operation;
/* Get the input values */ result = operand1 * operand2;
printf ( Enter operand1 : ); break;
scanf( %d , &operand1) ;
printf ( Enter operation : ); if (operand2 !=0)
scanf ( \n%c , &operation); result = operand1 / operand2;
else
printf ( Enter operand 2 : );
printf ( Divide by 0 error );
scanf ( %d , &operand2);
break;
default:
switch (operation) { printf ( Invalid operation\n );
}
result = operand1 + operand2; printf ( Result: %d\n ,result);
break; }
-
entry / single-exit structure.
switch statement
13
01/18/2020
14
01/18/2020
Examples
x = 50 + ++a; a = 11, x = 61
x = 50 + a++; x = 60, a = 11
15