Professional Documents
Culture Documents
Unit – 2
Control Statements - 1
Control statements:
• Decisions(if-else),
• Loops (while, for, do while)
• break, continue
• case control structure
• go to
• exit statement
Control statements in C
if (expression) statement;
else
if (expression) statement;
else
if (expression) statement;
.
.
.
else statement;
Selection or Control statements
The if-else-if Ladder
• The conditions are evaluated from the top downward. As soon as a
true condition is found, the statement associated with it is executed
and the rest of the ladder is bypassed.
• If none of the conditions are true, the final else is executed. That is, if
all other conditional tests fail, the last else statement is performed.
• If the final else is not present, no action takes place if all other
conditions are false.
Selection or Control statements
The if-else-if Ladder
Similar to the if-else statement as it does follow the same algorithm as of if-else
statement but the conditional operator takes less space and helps to write the if-else
statements in the shortest way possible.
Syntax: Which is equivalent to the code
#include <stdio.h>
int main()
{
int n1 = 5, n2 = 10, max;
max = (n1 > n2) ? n1 : n2;
printf("Largest number between %d and %d is %d", n1, n2, max);
return 0;
}
Output:
Largest number between 5 and 10 is 10
Reference
Reference: