C-Tutorial: STATEMENTS

Latest

Showing posts with label STATEMENTS. Show all posts
Showing posts with label STATEMENTS. Show all posts

Thursday, 10 November 2016

while loop in C

Thursday, November 10, 2016 0
while statement:         While statement is used for repetitive execution of statements more than once.

         


            Syntax:           while(condition)
                                    {
-          -     -          
-          -     -           code block (Statements)
                                    }

Here,
Ø  First the condition is evaluated.  It produces either TRUE or FALSE.
Ø  If the condition is TRUE, then Code Block (Statements) will be executed, again control reaches to condition section and is evaluated.
Ø  The process is repeated until the condition becomes FALSE.
Ø  When the condition reaches to FALSE, then the control is transferred out of the loop.

Flowchart:
           



/* PROGRAM TO PRINT FIRST N NATURAL NUMBERS */

#include<stdio.h>
#include<conio.h>

main()
{
            int i=1,n;
            clrscr();
            printf("\n Enter how many numbers:");
            scanf("%d",&n);
            printf("\n Natural Numbers Are:");
            while(i<=n)
            {
                        printf(" %3d",i);
                        i++;
            }

}
Read More

Sunday, 9 October 2016

break statement, continue statement in C-language

Sunday, October 09, 2016 0
break statement:         The break statement is used in loop control statements such as while, do-while, for and switch statements to terminate the execution of the loop or switch statement.  The general format of break statement is:


           
               Syntax:           break;

            When the keyword break is encountered inside any C loop, control automatically skip entire loop and passes to the statements available after the loop.

/* Example program for break statement */

main()
{
int i;
clrscr();
for(int i=1;i<10;i++)
{
            if(i==5)
            {
            printf("\n Come out from for loop when i equal to 5");
            break;
            }
printf("%3d",i);
getch();
}
continue statement:            The continue statement is used in while, do-while and for statements to terminate an iteration.  The general format of continue statement is:

            Syntax:           continue;

            When the keyword continue is encountered inside any C loop, compiler skips the remaining statements available after the continue statement and control reaches to next iteration of the loop.

/* Example program for continue statement */

main()
{
int i;
clrscr();
for(int i=1;i<10;i++)
{
            if(i==5 || i==6)
            {
            continue;
            }
printf("%3d",i);
getch();

}
Read More

goto statement in control statements

Sunday, October 09, 2016 0
goto statement:           The goto statement is used to alter the normal sequence of program execution by transferring the control to some other part of the program.  In its general form, the goto statement can be written as:


            
                   Syntax:           goto Label;

Where,
            Label is an identifier used to specify the target statements to which control will be necessary to transfer.   The target statements must be labeled and the label must be followed by a colon as:

            Syntax:           Label : Statements

Each label statement with in the program must have a unique name.

Depending on passing the control, goto statements can be classified as forward jump and backward jump.



Example program for Goto statement:

#include<stdio.h>
#include<conio.h>
#include<math.h>

main()
{
int n,s;
clrscr();
read:
printf("\n Enter n value: ");
scanf("%d",&n);
if(n>0)
{
goto square;
}
else
{
goto read;
}
square:
s=sqrt(n);
printf("\n Square root value = %d",s);
getch();
}
Read More

for loop control statement in C-language

Sunday, October 09, 2016 0
FOR LOOP: for is also a loop control statement used for repetitive execution of statements.  The general format of a for statement is


Syntax:           for(Initialization ; Condition ; Increment/Decrement)
                                    {
-          -  -
-          -  - Code Block (Statements)
-          -  -
                                    }

           

Flowchart:




1. The initialization step is executed first, and only once. The initialization part is used to assign a values to variables.


2.         Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

3.         After the body of the for loop executes, the flow of control jumps back up to the increment/decrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

4.         The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
/* PROGRAM TO PRINT FIRST N NATURAL NUMBER USING FOR STATEMENT */

main()
{
            int i,n;
            clrscr();
            printf("\nEnter how many numbers:");
            scanf("%d",&n);
            printf("\nNATURAL NUMBERS ARE:");
            for(i=1;i<=n;i++)
            printf("%5d",i);
            getch();
}
Read More

Saturday, 8 October 2016

do-while loop control statements in C-language

Saturday, October 08, 2016 0
do-while statement:               do-while is also a loop control statement used for repetitive execution of statements.  The general format of a do-while statement is:


                        
                          Syntax:           do
                                                {
-          -  -
-          -  -      Code Block (Statements)
-          -  -
                                                }

                                                while(condition);


Ø  First the compiler executes Block statements and then enters into condition section.
Ø  Condition is evaluated and produces either TRUE or FALSE.
Ø  If the condition outcome is TRUE, then again control enters into Block Statement and is executed.  This procedure is repeated until the condition becomes FALSE.
Ø  When the condition outcome reaches to FALSE, then the control is transferred out of the loop.

/* PROGRAM TO PRINT FIRST N NATURAL NUMBER USING DO-WHILE STATEMENT */

 main()
{
            int i=1,n;
            clrscr();
            printf("\nEnter how many numbers:");
            scanf("%d",&n);
            printf("\nNATURAL NUMBERS ARE:");
            do
            {
                        printf("%5d",i);
                        i=i+1;
            }
            while(i<=n);
}
Read More

switch Statement in C-language

Saturday, October 08, 2016 0
switch Statement:                   

switch statement is also a multi-way decision that allows for placing different block statements and execution depends on the result of the expression value.  The general format of switch statement is:


Syntax:           switch(Expression)
                        {
                                    case value1:   
                                                Block-1 code to be executed if expression equals to value1;
                                                break;
                                    case value2:   
                                                Block-2 code to be executed if expression equals to value2;
                                                break;
                                                .
                                                .
                                                .
                                    case valuen:   
                                                Block-n code to be executed if expression equals to valuen;
                                                break;
                                    default:          
                                    code/s to be executed if expression doesn't match to any cases;

                        }    next statements after switch statements



Ø  First Expression is evaluated and it produces an integer value.
Ø  Now, the expression value will be compared with case values value1, value2, ---, valuen by the compiler.  If any case value coincide with the expression value then that particular block statements are executed until break statement is encountered.
Ø  break is a branch control statement used to transfer the control out of the loop.
Ø  If the expression value doesn’t match with any case value then default block statements will be executed.  Default block is optional block.
Ø  Case values value1, value2, …. are either integer constants (or) character constants.
Ø  Case labels must be unique.  No two case labels should have the same name.
Ø  Generally switch statements are used for creating menu programs.

#include<stdio.h>
#include<conio.h>
main()
{
            int n;
            clrscr();
            printf("\n Enter a number (1 to 7):");
            scanf("%d",&n);
            switch(n)
            {
            case 1: printf(" Monday");
                        break;
            case 2: printf(" Tuesday");
                        break;
            case 3: printf(" Wednesday");
                        break;
            case 4: printf(" Thursday");
                        break;
            case 5: printf(" Friday");
                        break;
            case 6: printf(" Saturday");
                        break;
            case 7: printf(" Sunday");
                        break;
            }
getch();
}
Read More