Professional Documents
Culture Documents
P4 Control Statements in C
P4 Control Statements in C
in C
Presented by: P4
Dr. Rakesh Rathi
Assistant Professor & Head
Department of Computer Science and IT
Govt. Engineering College, Ajmer
Introduction
▪ A Program contains several statements, and statements are executed by the compiler.
▪ The sequence of execution of the statements by the compiler is called the execution flow.
Note:
▪ A statement is executed only once where as a loop executes repeatedly several times.
if(condition1) if(condition1)
statements1; if(condition2)
else if(condition2) if(condition3)
statements2; statements1;
else if(condition3) else statements2;
statements3; else statements3;
else statements4; else statements4;
}
getch();
}
Output: 2, 4, 6, 8, 10
Result=Result * X
OUTPUT
OUTPUT
int i=1;
for(; i<10; ) /* Here, declaration of i value is done outside the for loop and increment of
i value can be done inside the loop after displaying i value */
{
printf(“\n%d”, i);
i++;
}
infinite loop:
▪ An infinite loop is a loop which executes forever.
▪ Infinite loops can be created using for, do…while or while loops also.
▪ In do…while or while loops, infinite loops created by taking 1 in the place of the condition.
▪ in for loops, infinite loops created by not providing any condition in expression2.
int i=1;
for( ; ; )
{
printf(“\n%d”, i);
i++;
if(i>10) break;
}
▪= 0 1 1 2 3 5 8 13 21 34
OUTPUT
▪Statements will be executed according to the variable value entered matches case value.
▪If no variable value matches case value, then default statements will be executed.
▪break statement in the switch block is useful to come out of the block.
Note:
▪ These functions will not work in UNIX C. They work only in Turbo C/C++ compiler.
gotoxy() function:
▪This function is useful to place the cursor at any position on the screen.
▪The screen size in DOS is 80 columns width and it accommodates 25 rows.
gotoxy(30, 5); /* goto column 30 and row 5 */
▪gotoxy() function is defined in the header file <conio.h>
getche() function:
▪This function is similar to getchar() in that it receive a character from the keyboard and stores it into a
variable.
▪In getche(), need not to press the enter button, when character is typed getche() receives it automatically.
▪It displays the character being received.
▪The ‘e’ for ‘echo’ at the end of the getche() name, ‘echo’ means ‘display’.
exit(0) function:
▪Useful to terminate the program.
▪This function is defined in the header file <stdlib.h>.
▪Integer number is passed to this function, generally a 0 or non zero value.
▪A 0 represents that the program is terminated normally.
▪A non zero value like a 1 represents that the program is terminated because of some error.
▪0 or non zero value which is passed to this function is called ‘status code’, it represents the status of program
termination.
{ gotoxy(32,13); printf(“--------------------------------”);
OUTPUT
Note:
▪goto statements are not recommended in programming. This main reason is that they form infinite loops
easily which increase the complexity of a program.
Output
x 1 2 3
e = 1 + x /1 + x /2! + x /3! + ….
Output