Keywords related to control statements:
1. Selection statements [if and switch]: if, else, switch, case, default
2. Iterative statements [loops] : for, while, do
3. Jump statements : break, continue, goto, return
#include <stdio.h>
int main()
{
int a=-10;
if(a<0)
{
printf("a is negative number\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int a=100, b=20;
if(a>b)
{
printf("a is bigger\n");
}
if(b>a)
{
printf("b is bigger\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int a=100, b=20;
if(a>b)
{
printf("a is bigger\n");
}
else
{
printf("b is bigger\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int a=100, b=20, c=50, max;
if(a>b && a>c)
{
max = a;
}
if(b>a && b>c)
{
max = b;
}
if(c>a && c>b)
{
max = c;
}
printf("max=%d\n", max);
return 0;
}
#include <stdio.h>
int main()
{
int a=100, b=20, c=50, max;
if(a>b && a>c)
{
max = a;
}
else if(b>a && b>c)
{
max = b;
}
else if(c>a && c>b)
{
max = c;
}
printf("max=%d\n", max);
return 0;
}
#include <stdio.h>
int main()
{
int a=100, b=20, c=50, max;
if(a>b && a>c)
{
max = a;
}
else if(b>a && b>c)
{
max = b;
}
else
{
max = c;
}
printf("max=%d\n", max);
return 0;
}
#include <stdio.h>
int main()
{
int a, b, c, d, max;
a=40;
b=20;
c=50;
d=25;
if(a>b && a>c && a>d)
{
max=a;
}
else if (b>a && b>c && b>d)
{
max=b;
}
else if (c>a && c>b && c>d)
{
max=c;
}
else
{
max=d;
}
printf("max=%d\n",max);
return 0;
}
Dangling Else Problem:
#include<stdio.h>
int main(void)
{
char grade='A';
int marks = 94;
if(grade == 'A')
if(marks > 95)
printf("Excellent\n");
else
printf("Work hard for getting A grade\n");
return 0;
}
Solution:
#include<stdio.h>
int main(void)
{
char grade='A';
int marks = 94;
if(grade == 'A')
{
if(marks > 95)
{
printf("Excellent\n");
}
}
else
{
printf("Work hard for getting A grade\n");
}
return 0;
}
#include<stdio.h>
int main(void)
{
int r;
r=3;
if(r==1)
{
printf("rno 1 marks: 10\n");
}
else if(r==2)
{
printf("rno 2 marks: 20\n");
}
else if(r==3)
{
printf("rno 3 marks: 30\n");
}
else if(r==4)
{
printf("rno 4 marks: 40\n");
}
else if(r==5)
{
printf("rno 5 marks: 50\n");
}
else
{
printf("invalid roll number\n");
}
return 0;
}
#include<stdio.h>
int main(void)
{
int r;
r=3;
switch(r)
{
case 1: printf("rno 1 marks: 10\n");
case 2: printf("rno 2 marks: 20\n");
case 3: printf("rno 3 marks: 30\n");
case 4: printf("rno 4 marks: 40\n");
case 5: printf("rno 5 marks: 50\n");
default: printf("invalid roll number\n");
}
return 0;
}
#include<stdio.h>
int main(void)
{
int r;
r=10;
switch(r)
{
case 1: printf("rno 1 marks: 10\n");
break;
case 2: printf("rno 2 marks: 20\n");
break;
case 3: printf("rno 3 marks: 30\n");
break;
case 4: printf("rno 4 marks: 40\n");
break;
case 5: printf("rno 5 marks: 50\n");
break;
default: printf("invalid roll number\n");
}
return 0;
}
#include <stdio.h>
int main(void)
{
int i;
/*
printf("%d\t", 1);
printf("%d\t", 2);
printf("%d\t", 3);
printf("%d\t", 4);
printf("%d\t", 5);
printf("%d\t", 6);
printf("%d\t", 7);
printf("%d\t", 8);
printf("%d\t", 9);
printf("%d\t", 10);
*/
/*
i=1; //start
while(i<=10) //exit
{
printf("%d\t", i); //task
i++; //step
}
*/
/*
for(i=1 ; i<=10 ; i++) //start; exit; step
{
printf("%d\t", i); //task
}
*/
i=1; //start
do
{
printf("%d\t", i); //task
i++; //step
}while(i<=10); //exit
return 0;
}
//start-1
//end -1000
//step --> inc by 1
//task - print int and give tab space
/*P5.27 Program to understand the use of break and continue*/
#include<stdio.h>
int main(void)
{
int n;
for(n=1; n<=5; n++)
{
if(n==3)
{
break;
}
printf("Number=%d\n",n);
}
printf("Out of for loop\n");
return 0;
}
#include<stdio.h>
int main(void)
{
int n;
for(n=1; n<=5; n++)
{
if(n==3)
{
continue;
}
printf("Number=%d\n",n);
}
printf("Out of for loop\n");
return 0;
}
/*P5.24 Program to understand nesting in for loop*/
#include<stdio.h>
int main(void)
{
int i,j;
for(i=1; i<=3; i++) /*outer loop*/
{
printf("i=%d\n",i);
for(j=1; j<=4; j++) /*inner loop*/
{
printf("j=%d\t",j);
}
printf("\n");
}
return 0;
}
/*P5.31 Program to find whether a number is even or odd*/
#include<stdio.h>
int main(void)
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
if(n%2==0)
goto even;
else
goto odd;
even:
printf("Number is even\n");
goto end;
odd:
printf("Number is odd\n");
goto end;
end:
printf("\n");
return 0;
}