Lect9 Storage Class
Lect9 Storage Class
There come situations in real life when we need to make some decisions
and based on these decisions, we decide what should we do next. Similar
situations arise in programming also where we need to make some
decisions and based on these decisions we will execute the next block of
code. For example, in C if x occurs then execute y else execute z. There
can also be multiple conditions like in C if x occurs then execute p, else if
condition y occurs execute q, else execute r. This condition of C else-if is
one of the many ways of importing multiple conditions.
int main() {
int i = 10;
if (i > 15)
{
printf("10 is less than 15");
}
Example:
int main() {
int i = 20;
if (i < 15)
printf("i is smaller than 15");
else
printf("i is greater than 15");
return 0;
}
Output:
i is greater than 15
The block of code following the else statement is executed as the condition
present in the if statement is false.
nested-if in C
Example:
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;
}
Output:
i is smaller than 15
i is smaller than 12 too
if-else-if ladder in C
Here, a user can decide among multiple options. The C if statements are
executed from the top down. As soon as one of the conditions controlling
the if is true, the statement associated with that if is executed, and the rest
of the C else-if ladder is bypassed. If none of the conditions are true, then
the final else statement will be executed.
Syntax:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Example:
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");
}
Output:
i is 20
Jump Statements in C
These statements are used in C orC++ for unconditional flow of control
through out the funtions in a program. They support four type of jump
statements:
Example:
// C program to illustrate
// Linear Search
#include <stdio.h>
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };
// no of elements
int n = 6;
// key to be searched
int key = 3;
return 0;
}
Output:
Element found at position: 3
3. C continue: This loop control statement is just like the break
statement. The continue statement is opposite to that of
break statement, instead of terminating the loop, it forces to execute
the next iteration of the loop.
As the name suggest 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 next iteration of the loop will begin.
Syntax:
4. continue;
Example:
int main() {
// loop from 1 to 10
for (int i = 1; i <= 10; i++) {
// If i is equals to 6,
// continue to next iteration
// without printing
if (i == 6)
continue;
else
// otherwise print the value of i
printf("%d ", i);
}
return 0;
}
Output:
1 2 3 4 5 7 8 9 10
5. C goto: The goto statement in C/C++ also referred to as unconditional
jump statement can be used to jump from one point to another within a
function.
Syntax:
6. Syntax1 | Syntax2
7. ----------------------------
8. goto label; | label:
9. . | .
10. . | .
11. . | .
12. label: | goto label;
In the above syntax, the first line tells the compiler to go to or jump to
the statement marked as a label. Here label is a user-defined identifier
which indicates the target statement. The statement immediately
followed after ‘label:’ is the destination statement. The ‘label:’ can also
appear before the ‘goto label;’ statement in the above syntax.
// returns void
// function to print
void Print(int s2)
{
printf("The sum is %d", s2);
return;
}
int main()
{
int num1 = 10;
int num2 = 10;
int sum_of = SUM(num1, num2);
Print(sum_of);
return 0;
}
Output:
The sum is 20