Control-Statements
Control-Statements
If statements
Switch Statement
Conditional Operator Statement
Goto Statement
Loop Statements
If statement
If statement enables the programmer to choose a set of
instructions, based on a condition. When the condition is
evaluated to true, a set of instructions will be executed and a
different set of instructions will be executed when the
condition is evaluated to false. We have 4 types of if
Statement which are:
1. If..else
2. Nested if
3. Else if ladder
4. Simple if or null else
5. Null else or Simple else
If statement
Syntax for the if selection structure is as follows:
#include <stdio.h>
int main ( )
{
int a = 1, b = 2, c ;
if (a > b)
c = a;
else
c = b;
}
nested-if in C
A nested if in C is an if statement that is the target of another
if statement. Nested if statements mean an if statement inside
another if statement. Yes, both C and C++ allow us to nested
if statements within if statements, i.e, we can place an if
statement inside another if statement.
if (condition1)
{
// Executes when condition1 is
true
if (condition2)
{
// Executes when condition2 is
true
}
}
#include <stdio.h>
int main() {
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
return 0;
}
if-else-if ladder in C
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
if / else if / else Selection
Structures
• Syntax for the if / else if / else selection
structure is as follows:
break;
C continues: This loop control statement is just like the
break statement. The continue statement is opposite to
that of the break statement, instead of terminating the
loop, it forces to execute the next iteration of the loop.
As the name suggests the continue statement forces the
loop to continue or execute the next iteration. When the
continue statement is executed in the loop, the code
inside the loop following the continue statement will be
skipped and the next iteration of the loop will begin.
Syntax:
continue;