CONDITIONAL STATEMENTS AND LOOPS
DECISION MAKING WITHIN A PROGRAM :
- Decision making structure of a program is done when the programmer specifies more than one condition.
- It is tested with the statements provided for the decision which can be true or false.
- C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.
IF AND IF-ELSE STATEMENTS :
- IF STATEMENT
- IF statement is used for checking condition and executing the statements accordingly.
- If the condition is TRUE then the statement which we have written in block will be executed.
- The statement will be ignored if the condition is FALSE.
- SYNTAX :
if(condition) { //STATEMENTS; }
- IF-ELSE STATEMENT
- IF-ELSE statement is similar to IF statement only with one difference.
- The differnce is that it will also execute the statement if the condition false .(here : STATEMENT 2)
- ELSE block is used with IF to executed the statements according to the condition.
- SYNTAX :
if(condition) { //STATEMENT 1; } else { //STATEMENT 2; }
- IF-ELSE-IF STATEMENT
- IF-ELSE-IF statements are used to test the multiple condition and execute accordingly.
- It have more than one condition to be checked.
- ELSE IF statement is used to take multiple conditions.
- SYNTAX :
if(condition 1) { //STATEMENT 1 ; } else if(condition 2) { //STATEMENT 2; } else { //STATEMENT 3; }
LOOPS IN C :
- FOR LOOP
- FOR loop is used to execute a block of code repeatedly until a specified condition is met.
-
INITILIZATION is done only one time and then the condition is checked.
If the CONDITION is FALSE then the loop will be terminated. - If the CONDITION is TRUE, statements inside the body of for loop are executed, and the increment/decrement is done until CONDITION get FALSE.
- SYNTAX :
for(initilization;condition;increment/decrement) { //STATEMENTS; }
- WHILE LOOP
- The WHILE loop test the CONDITION inside the parenthesis ().
- If the CONDITION is TRUE, statements inside the body of for loop are executed, and the increment/decrement is done until CONDITION get FALSE.
- INITILIZATION is done before executing WHILE loop.
- SYNTAX :
initilization; while(condition) { //STATEMENTS; increment/decrement; }
- DO-WHILE LOOP
- The DO-WHILE loop is similar to the WHILE loop with only difference that the body of DO-WHILE loop is executed at least once.
- After executing at least one time the condition is checked.
- SYNTAX :
initilization; do { //STATEMENTS; increment/decrement; }while(condition);
SWITCH STATEMENTS :
- A SWITCH statement allows a variable to be tested for various condition against a list of expression.
- Each expression is known as a CASE.
- When the variable is equal to a case, the statements following that case will execute until a break statement is reached.
- SYNTAX :
switch(expression) { case expression1: statement; break; case expression2: statement; break; case expression3: statement; break; default: statement; }
CONTROL/JUMP STATEMENTS :
- BREAK STATEMENT :
- It can be used to terminate a CASE in the SWITCH statement
- If used in NESTED LOOPS (one loop inside another), the BREAK statement will stop the execution of the innermost loop and start executing the next line of code after the block.
- SYNTAX : break;
- CONTINUE STATEMENT :
- The CONTINUE statement works somewhat like the BREAK statement.
Instead of forcing termination, it forces the next statements of the loop to take place, skipping any code in between. - For the WHILE and DO-WHILE loops, CONTINUE statement causes the program control to pass to the conditional tests.
- SYNTAX : continue;
- The CONTINUE statement works somewhat like the BREAK statement.
Comments
Post a Comment