if...else Statement:
- if...else statement
- switch statement
if (test expression)
statements to be executed if test expression is true;
Nested if...else statement (if...elseif....else Statement):
The if...else statement can be used in nested form when a serious decision are involved. The ANSI standard specifies that 15 levels of nesting may be continued.Syntax of nested if...else statement:
if (test expression) statements to be executed if test expression is true; else if(test expression 1) statements to be executed if test expressions 1 is true; else if . . . else statements to be executed if all test expressions are false;
How nested if...else works?
If the test expression is true, it will execute the relevant code
but, if it is false, the control of the program jumps to the else part
and check test expression 1 and the process continues. If all the test
expression are fa;se then, only the last statement is executed.for Loop
C programming loops:
Loops causes program to repeat the certain block of code until some conditions are satisfied. Loops are used in performing repetitive work in programming.Syntax of for loop:
for(initial expression; test expression; update expression)
{
codes to be executed;
}
How for loops in C programming works?
The initial expression is initialized only once at the beginning of the for loop. Then, the test expression is checked by the program.If the test expression is false, for loop is terminated.
If test expression is true then, the codes are executed and update expression is updated. Again, the test expression is checked. If it is false, loop is terminated and if it is true, the same process repeats until test expression is false.
For the easier understanding of how for loop works, analyze the flowchart of for loop below.
Importent:
- Initial, test and update expression in C for loop are separated by semicolon(;).
- Two or more expression of same type in for loop are separated by comma(,).
o...while Loop
C programming loops
Loops causes program to repeat the certain block of code until some conditions are satisfied. Loops are used in performing repetitive work in programming.C while loops:
Syntax of while loop:
while (test expression)
{
statements to be executed.
}
In the beginning of while loop, test expression is checked. If it is true, codes inside the body of while loop is executed and again the test expression is checked and process continues until, the test expression becomes false.
C do...while loops:
C do...while loop is very similar to while loop. Only difference between these two loops is that, in while loops, test expression is checked at first but, in do...while loop code is executed at first then the condition is checked. So, the code are executed at least once in do...while loops.Syntax of do...while loops:
do {
some codes;
}
while (test expression);
At first codes inside body of do is executed. Then, the test expression is checked. If it is true(nonzero), codes inside body of do are executed again and the process continues until test expression is zero(false).
Notice, there is semicolon in the end of while (); in do...while loop.
break and continue Statement:
C break statement:
In C programming, break is used in terminating the loop immediately after it is encountered. The break statement is used with conditional if statement.Syntax of break statement:
break;
The break statement can be used in terminating all three loops for, while and do...while loops.
C continue statement:
It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used. Continue statements are only used in while, do...while and for loops in C programming.Syntax of continue statement:
continue;
Just like break, continue is also used with conditional if statement.
For better understanding of how continue statements works in C programming. Analyze the illustration of bypassing and continuing in loops below.
switch....case Statement:
Syntax of C switch...case:
switch (expression)
{
case constant1:
codes to be executed if expression equals to constant1;
break;
case constant2:
codes to be executed if expression equals to constant3;
break;
.
.
.
default:
codes to be executed if expression doesn't match to any cases;
}
In switch...case, expression is an integer or character expression asked to a user. If the value of switch expression matches any of the constant in cases, the relevant codes are executed and control moves out of the switch case statement. If the expression doesn't matches any of the constant in cases, then the default statement is executed.
goto Statement:
Syntax of goto statement:
goto label;
.............
.............
.............
label:
statement;
In this syntax, label is an identifier. When, the control of program reaches to goto label statement, the control of the program will jump to the label.
Reasons to avoid goto statement:
Though, using goto statement give power to programmar to jump to any part of program, using goto statement in programming makes the logic of the program complex and tangled. In modern programming, goto statement is considered a harmful construct and a bad programming practice.The goto statement can be replaced in most of C program with the use of break and continue statements. In fact, any program in C programming can be perfectly written without the use of goto statement. All programmar should try to avoid goto statement as possible as they can.
No comments:
Post a Comment