Tuesday, May 26, 2020

Conditional statement and loops in c programming language

conditional statement and loops in c programming language

CONDITIONAL STATEMENTS IN C

What is conditional statement and loops in c programming language

To control the flow of the execution of programs, the ‘C’ language provides very powerful conditional statements and unconditional statements . The ‘C’ language supports many conditional statements like :
   Ø  If
   Ø  If-else
   Ø  switch
Loop control statement is :
Ø  for
Loop control conditional statements are :
Ø  while
Ø  do-while
Ø  goto
CONDITIONAL STATEMENTS

These statements enable us to change flow of the program. Most of the programming tools use the if statement to make decisions. If the expression  is true then the statements enclosed in the braces, are executed. If the expression is false, then the statements enclosed in the braces, are not executed, only the statements which exist outside the braces, are executed.

THE IF STATEMENT

This statement allows decision to be made by evaluating a given condition as true or false. The keyboard if tells the compiler that it is a decision control instruction.The condition if is always enclosed within a pair of parenthesis. The relational operators allow us to compare two values to see whether they are equal to each other, unequal, greater or less than the other. The general form of if statement looks like this :
     if (this condition is true )
          execute this statement ;

Syntax :

If (expression)
{
   Statement 1;
   Statement 2;
   …….
   …….
   Statement n;
   Statement 1;
   Statement 2;
   ……
   ……
   Statement m;
If the expression is true then the statements enclosed in the braces, are executed. If the expression is false then the statements enclosed in the braces, are not executed.

NESTED IF STATEMENTS

The if statement can contain another if….else statemet thus improving the flexibility of a programming tool.The statement whose object is if statement is a nested if statement .

Syntax :

if (expression 1)
if (expression 2)
if (expression 3)
{
   Statement 1;
   Statement 2;
   Statement 3;
   …..
   …..
   Statement n;
}
Or
if (expression 1 && expression 2 && expression 3 && expression 4)
{
   Statement 1;
   Statement 2;
   Statement 3;
   …..
   …..
  Statement n;
}
IF-ELSE  STATEMENT

Syntax :

if (expression)
{
   Statement 1;
   Statement 2;
   …..
   …..
   Statement n;
}
else
{
   Statement 1;
   Statement 2;
   …..
   …..
   Statement m;
}

Here , if expression is true, then code of the first block is executed, else second block is executed.

SWITCH-CASE STATEMENT

The control statement, which allows us to make a decision from the number of choices, is called a switch. With the help of switch and case statements we may choose any number of decisions, or more correctly a switch-case default, since the combination of these three keywords go together to make up the control statement.

Syntax :

switch (expression)
{
case 1:
   statement 1;
   statement 2;
case 2:
   statement 1;
   statement 2;
case 3:
   statement 1;
   statement 2;
default :
   statement 1;
   statement 2;
The switch statement checks, whether an expression matches with number of integer or character constant.

NESTED SWITCH STATEMENT
Syntax :

switch (choice)
{
case 1:
switch (choice 1)
{
case ‘a’:
…..
…..
break;
case ‘b’:
…..
…..
break;
}
break;
            case 2:
            …..
            …..
break;
…..
…..
case n:
…..
…..
break;
default :
…..
…..
}

In the nested switch statements, only one default case is used i.e, with the outer switch statement.

THE goto STATEMENT

As a good ‘C’ programmer , avoid using goto statements. A goto statement is always followed by a LABLE. The lable may be an identifier declared, constant, character constant, or a string constant. LABLE does not allow any special characters, but numeric characters are allowed. A lable decides the position of the control to be jumped, and which is allowed by colon(:). A lable may be constant as well as variable.


THE LOOPS IN C

LOOPS

A computer program is a set of statements, which is normally executed sequentially. But in most of the cases , it is necessary to repeat certain steps to meet a specific condition. This repetitive operation is done through a loop control structure.
In other words, a loop is basically the execution of sequence of statements repeatedly unit a particular condition is true or false.

  The loops supported by most of the programming tools are :

Ø  Using a while statement,
Ø  Using a do-while statement,
Ø  Using a for statement.

THE while LOOP

The while loop repeats a statement or a set of statements until a certain condition is no longer true.

Syntax :
while (condition)
{
   statement 1;
   statement 2;
}
   statement 1;
   statement 2;

Here, the condition may be any expression having non-zero value. The loop continues is true. When the condition fails, the program body attached with loop, will not be executed. The flow chart shown below is to understand the operation of the while loop.

THE DO-WHILE LOOP

The do-while looks like as follow :
do
{
   statement 1;
   statement 2;
   …..
} while (condition);
   statement 1;
   statement 2;
From the above syntax, if the condition is false, then at least one time the attached loop is executed.

Example : Write a program that reads an integer N and computes N!.

Solution :
#include<stdio.h>
main ( )
{
Int no, fact;
do
{
printf(“Enter non-negative number \n”);
scanf(“%d”,&no);
} while (no < 0);
For (fact = 1; no > 0; no - -)
fact *=no;
printf(“%d\n”,fact);
}

THE For LOOP

The for loop is ideally used when we know how many times the loop will be executed. This looping statement is generally available in every language. But it is much more powerful and flexible in the ‘C’ language.
Syntax :
for (initialization; condition; counter)
{
   statement 1;
   statement 2;
   …..
   …..
   statement n;
}
The for statement generally allows three statement :

Ø  Initialization is generally on assignment which is used to set the loop control variable e.g., i = 0.
Ø  Condition is always relational expression that determines when to exit the loop. The relational expression may be a part of arithmetic statement e.g.,
< 10 or i < (a + 10)
Here (a + 10) is an arithmetical expression.
Ø  Counter defines how the loop control variable will change each time the loop is repeated. This may be incremented or decremented. The following flow chart shows the complete working of for loop.

NESTED FOR LOOPS

We can have a for loop within another for loop just like if statements can be nested. To understand how nested loops work, look at the example given below :
for (i = 1; i <= 3; i ++)
{
for (j = 1; j <= 3; j++)
{
statements…..
}
}

Vibhor Sharma

Author & Editor

###

1 comments:

  1. Very helpful article for newcomers (in C-Programming).
    Write ✍🏻 more such Articles.

    ReplyDelete