Control Statement

Execution of a program begins at the main () function.
  • Statements within the main () function are then executed from top to down style.
  • The first statement, then the second and so forth, until the end of the main () function is reached in real C/C++ program.
  • The order of the execution of the statements within the main () body may be redirected, not in sequence anymore.
  • This concept of changing the order in which statements are executed is called program control and is accomplished by using program control statements. This is how we can control the program flows.
Program Control Types: There are three types of program controls
Sequence control structure:

Sequence instruction means executing one instruction after another, in the order in which they occur in the source file. This is usually built into the language as a default action, as it is with C. If an instruction is not a control statement, then the next instruction to be executed will simply be the next one in sequence.

Selection structures:

Selection means executing different sections of code depending on a specific condition or the value of a variable. This allows a program to take different courses of action depending on different conditions. Those are if, if-else, nested if, if–else if and switch…case…break:

Repetition:

Repetition/Looping means executing the same section of code more than once. A section of code may either be executed a fixed number of times, or while some condition is true. Those are for, while and do…while

Terms used…

Selection: consists of if and switch statements.

Iteration (Loops): consists of while and for statements.

Jump: consists of goto, break, continuo, and return switch statements.

Label: consists of case and default statements.

Expression: Built with operators and operands.

Block: code written inside the curly braces.

USES:

  • Operation is simple to trace
  • Structures are self-documenting
  • Structures are easy to define in flowcharts
  • Structured programming increases programmer productivity
  • Structures lead to functions

Conditional statements

These are the statements which alter the flow of execution and provide better control to the programmer on the flow of execution of statements. We have three types of control statements those are
1. Conditional control statements
2. Loop control statements
3. Transfer control statements

C Language Control Statement
if -else: Syntax: if { //statements } [else { //statements }]
If else statement in C

From the above syntax we can easily understand that how it will work. If the if condition is TRUE, then the statements written inside the “if-block” will be executed. If the condition is false (if), then the “else-block” is executed.

Note: Here “else-block” is completely optional. If you want to provide information through that block then you can provide otherwise you can leave.

#include #include main() { int n; clrscr(); printf(“enter one number :”); scanf(“%d”,&n); if(n%2==0) { printf(“even number”); } else { printf(“odd number”) }} void main() { if (1) printf(“Hai”); else CW: unreachable code in function main printf(“bye”) }

NOTE: No need to write the statements in a block if you want to write only one statement as if block statement. That means COMPILER will recognize that the statement followed by the if is the that if block statement.

Void main() {
if (1)
printf(“Hello World!”);
printf(“Hai!”);
}}

NOTE: in the above program we have to write two statements inside the block, we have to make the statements as block by using {}, otherwise COMPILER will thinks that the second statement is the statement that is belongs to outside the block

Void main() {
if (1)
printf(“if block statement”);
printf(“outside if block”);
else //CE: ‘misplaced else’
printf(“else block statement”);
}}

Void main(){
if (1){
printf(“Hello World!”);
printf(“inside if block”);
}
printf(“outside of if block”);
else{ //CE:
printf(“inside else block”);
}}

NOTE: ‘else’ block should be followed by ‘if’ block, you should not write any statements between these two blocks.

In computer programming, ‘?:’ is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages. It is commonly referred to as the conditional operator or inline if (iif)

main(){
int a=20;
int b=30;
int max=a > b ? a : b;
printf(“%d”,max);
}

if{ //statements } else if{ //statements } else{ //statements }

nested if else in C
Nested- if

if there is a need to check one condition depend on another condition then we can go for nested-if

 

Leap year program:
#include<stdio.h>
#include<conio.h>
main() {
int leap_year;
clrscr();
printf(“enter one year:”);
scanf(“%d”,&leap_year);
if(leap_year%100==0){
if(leap_year%400==0)
printf(“%d is a leap year”,leap_year);
else
printf(“%d is not a leap year”,leap_year);
}
else{
if(leap_year%4==0)
printf(“%d is a leap year”,leap_year);
else
printf(“%d is not a leap year”,leap_year);
}
getch();
}
void main()
{
int n;
clrscr();
printf(“enter one year:”);
scanf(“%d”,&n);
(n%100==0)&&(n%400==0)||(n%100!=0)&&(n%4==0)?printf(“leap”):printf(“not a leap”);
getch();
}

Do-while:
It is the loop control statement, which executes the statements written inside this block repeatedly, as long as the condition is true.

Syntax:
do
{
//statements;
}
while (<condition>);

 

program:
int main()
{
int i=1;
do
{
printf(“i value :%d\n”,i);
i++;
}while (i<=10);
return 0;

NOTE: if there is a need to the programmer that the statements written inside do-while block must be executed once even though the condition is false, then we can go for do-while loop. That means this loop control statement will execute the statements for the first time, then only it will check the condition, if the condition is false, execution will be terminated. If the condition is true, then it will continue the process of execution.

The following program explains the flow of execution if the condition is false for the first time.

void main(){
int i=1;
do{
printf(“i value :”+i);
i++;
}while (i>10);
}
}

While: This is also a loop control statement, but in while-loop, the condition is tested first and then only the statements are executed.

While Loop in C

Code1:
int main()
{
int i=1;
while (i>10)
{
printf(“i value :%d\n”,i);
i++;
}
return 0;
}

Code2:
Void main(){
while(1){
printf(“inside while loop”);
}}

Code3:
Void main(){
while(1){
printf(“inside while loop”);
}
printf(“outside the while loop”); //CW: unreachable statement
}

Out of do-while and while, which is efficient:

 In a do-while loop, the statements are executed without testing the condition, the first time. From the second time only the condition is observed. This means that the programmer does not have control right from the beginning of its execution. In a while loop, the condition is tested first and then only the statements are executed. This means it provides better control right from the beginning. Hence, while loop is more efficient than do-while loop.

for loop: The for loop is also same as do-while or while loop, but it is more compact (binding
together) syntactically. The for loop executes a group of statements as long as a condition is 1.
Syntax:
for(expr1;expr2;expr3)
{
//statements
}
Expr1 is the initialization expression
Expr2 is the conditional expression
Expr3 is the modifying (increment (or) decrement) expression

Code1:
Void main(){
for(int i=1;i<=10;i++){
printf(“i value is :”+i);
}
printf(“outside of for-loop”);
}

Code2:
Void main(){
int I;
for( i=1;1;i++){
printf(“i value is :”+i);
}
printf(“outside of for-loop”);//CW:unreachable statement
}

Code3:
Void main(){
int b=1,i;
for( i=1;b;i++){
printf(“i value is :”+i);
}
printf(“outside of for-loop”);//CW:unreachable statement
}

Nested for loops:
We can also write the for-loop inside another for-loop called as nested for-loops.
Programs to print tables, patterns, matrix form………..
Void main(){
int i,j;
clrscr();
for(i=1;i<=5;i++){
for(j=1;j<=5;j++)
printf(“* “);
printf(“\n”);
}}

Switch statement:

When there are several options and we have to choose only one option from the available ones, we can use ‘switch’ statement. Depending on the selected option, a particular task can be
performed. A task represents one or more statements.

Syntax:
switch(variable)
{
case value1 : statements1;
case value2 : statements2;
case value3 : statements3;
……….
[default : default statements;]
}

void main()
{
char color=’g’;
switch(color){
case ‘r’ : printf(“color is red “);
case ‘g’ : printf(“color is green “);
case ‘b’ : printf(“color is blue “);
case ‘o’ : printf(“color is orange “);
default :printf(“no color “);
}
}

Here depending upon the value of the variable, a particular task (statements) will be
executed.

But the output of the above program is not as expected. We expected that it would display Green, but it is displaying all colors starting from the Green color. What might be the reason? When color value is g, it has displayed Green and after that it has come down to execute the
rest of the statements under it leading to the preceding output.

NOTE: The solution is to come out of the ‘switch’ statement, after displaying Green. For this purpose, we can use ‘break’ statement.One of the used of ‘break’ is to terminate a loop and come out of it. Another use of ‘break’ statement is to come out of the ‘switch’ block.

Void main(){
char color=’g’;
switch(color){
case ‘r’ : printf(“color is red “);
break;
case ‘g’ : printf(“color is green “);

break statement:

break statement ca e used in 3 ways.
1. break is used inside a loop to come out of it.
2. break is used inside the switch block to come out of the switch block.
3. break can be used in nested blocks to go to the end of a block. Nested blocks represent a

block written within another block.

Code1:
Void main(){
for(int i=1;i<=10;i++){
if(i==6)
break;
printf(“%d”,i);
}}

Code2:
Void main(){
for(int i=1;i<=10;i++){
break;
printf(“i value is : “+i);
}}

Code3:
Void main(){
for(int i=1;i<=10;i++){
for (int j=1;j<=10 ;j++ ){
if(i==j)
break;
printf(“i : “+i+”\tj: “+j);
}}}

Continue:

 Continue statement is a jump statement.
 The continue statement can be used only inside for loop, while loop and do-while loop.
 Execution of these statement does not cause an exit from the loop but it suspend the
execution of the loop for that iteration and transfer control back to the loop for the next
iteration.

Code1:
Void main(){
for(int i=1;i<=10;i++){
if(i==5)
continue;
printf(“i value is :”+i);
}}

Code2:
Void main(){
for(int i=1;i<=10;i++){
for (int j=1;j<=10 ;j++ ){
if(i==j)
continue;
printf(“i : “+i+”\tj: “+j);
}}}

goto:

  • C supports an unconditional control statement that is goto.
  • goto is used to transfer control from one point to other in a C program.
  • goto is a branching statement.
  • It requires a label.
  • goto is a keyword.

The syntax of goto statement is:
goto END:
…………….
…………….
…………….
END:
…………….

Void main(){
int i=0;
clrscr();
top:
printf(“%d\n”,i);
i++;
if(i<=10)
{
goto top;
} }

Logical programming(interview codes)

PROGRAM TO PRING SUM OF FIRST N NUMBERS

main()
{
int n,sum=0,i;
clrscr();
printf(“enter the limit:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++){
sum=sum+i;
}
printf(“sum of first %d numbers is :%d”,n,sum);
getch();
}

PRIME NUMBER PROGRAM:

main(){
int n,i,j,count=0;
clrscr();
printf(“enter one number :”);
scanf(“%d”,&n);
for(i=1;i<=n;i++){
count=0;
for(j=1;j<=i;j++){
if(i%j==0){
count++;
}}
if(count==2)
printf(“%d\t”,i);
}
getch();
}

Scroll to top