You are on page 1of 21

PROGRAMMING IN C

Advanced Control Structures

Dr. S. Usha Kiruthika


Dept. of CSE
NITT.
What is the output?

#include <stdio.h>
int main(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("%d",j);
}
printf("\n");
}
return 0;
}
Switch..case statement
• switch..case is used for switching control to one block out of many
alternatives
• The value in switch is matched with corresponding case
switch (n)
{
case 1:
// code to be executed if n = 1;
break;
case 2:
// code to be executed if n = 2;
break;
default:
// code to be executed if n doesn't match any cases
}
If..else and Switch..case Statement
int main() {
int main() int choice;
{ printf("Enter your choice (1 or 2 or 3):");
int choice; scanf("%d",&choice);
printf("Enter your choice (1 or 2 or 3):"); switch(choice) {
scanf("%d",&choice); case 1:
if (choice==1) printf("Your choice is 1\n");
printf("Your choice is 1\n"); break;
else if(choice==2) case 2:
printf("Your choice is 2\n"); printf("Your choice is 2\n");
else if(choice==3) break;
printf("Your choice is 3\n"); case 3:
else printf("Your choice is 3\n");
printf("Enter a valid choice\n"); break;
return 0; default:
} printf("Enter a valid choice\n");
}
return 0;
}
Switch..case statement
• Break statement is required for exiting switch without executing the
subsequent cases
• Default case will execute if none of the cases match. Default case is
optional.
• Cases cannot be duplicate
• Only integers or characters are allowed in switch expression
What does this code do?
switch (day) {
case 1 :
case 7 :
puts("Weekend day!");
break;
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
puts("Weekday");
break;
default :
puts("Illegal day");
}
Prints “Weekend day!” when 1 or 7 is entered
Prints “Weekday” when a number between 2 and 6 is entered
Otherwise prints “Illegal day”
Practice program
• Write a program to do basic calculator functionality. You must use
switch case. The program must loop infinitely asking for the next
operation to do until the user wishes to exit.
Break and Continue
• Break statement is used to exit from a loop – usually on a condition
• Continue statement is used to skip the rest of the statements in the
loop and begin the next iteration
do { while(1) {
printf("Enter a number: "); printf("Enter a number: ");
scanf("%d", &number); scanf("%d", &number);
sum += number; if(number==0)
} break;
while(number != 0); sum += number;
}
printf("Sum = %d",sum); printf("Sum = %d",sum);
Find the value of ‘average’

index=0;sum=0;
for(;;){
sum=sum+index;
++index;
if(sum>100) break;
}
average=sum/index;
What does this code do?
for ( x = 1; x <= 10; x++ )
{
if ( x % 2 != 0 )
continue;
printf("\n%d",x);
}

Prints even numbers from 1 to 10

What happens when you replace ‘continue’ with ‘break’?


What is the Output?

void main()
{
int i = 0, j = 0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 1;)
{
break;
}
printf("\nHello");
}
}
Output: Hello is printed 5 times
What is the Output?

int main()
{
int count;
for (count = 0; count < 10; ++count) {
printf("#");
if (count > 6)
continue;
printf("%d", count);
}
return 0;
}
Output: #0#1#2#3#4#5#6###
Goto statement
• Unconditional jump
• Branches to another statement using labels
• Goto makes the code hard to understand – it has to be avoided
Eg.
goto label;
.
.
.
label: printf(“Another Statement”);
What is the output of this program?
int main(void) {
int n = 1;
label: printf("%d\n",n);
n++;
if (n <= 10)
goto label;
return 0;
}

Rewrite the above code without goto


Comma Operator
• A comma expression uses comma as an operator
• The operator permits multiple expressions to be used where a single
expression is normally allowed
• The expressions are evaluated from left to right
eg. n=10,20,30;
x=(a=10,a*a);
Comma Operator in for loop

for(i=1,j=1;j<=n;i+=2,j++)
sum+=i;

Calculation of sum of n odd numbers


What is the output?
#include <stdio.h>
int main()
{
int i = 0, j = 0;
while (i<5,j<10)
{
i++;
j++;
}
printf("%d %d", i, j);
}

Output: 10 10
Scope of a variable
• A scope is a region of the program, and the scope of variables refers
to the area of the program where the variables can be accessed after
its declaration.
• Variables may have local scope (Block scope) or global scope
Local Scope
• Variable is visible within the block in which it is defined

int main()
{
{
int x = 10;
}
{
// Error: x is not accessible here
printf("%d", x);
}
return 0;
}
Local Scope
• A variable defined in an outer block is visible in all inner blocks
• But definition of a variable in an inner block hides the variable with
same name defined in the outer block
int main() int main()
{ {
int x = 10; //x defined in outer block int x = 10; //x defined in outer block
if (x>5)
if (x>5) {
{ int x = 5; //this hides x in the outer block
printf("%d", x); printf("%d", x);
} }
return 0; return 0;
} }
Output: 10 Output: 5
What is the Output?
int main()
{
int x = 1, y = 2, z = 3;
printf(" x = %d, y = %d, z = %d \n", x, y, z);
{
int x = 10;
float y = 20;
printf(" x = %d, y = %f, z = %d \n", x, y, z);
{
int z = 100;
printf(" x = %d, y = %f, z = %d \n", x, y, z);
}
}
Output:
return 0; x = 1, y = 2, z = 3
} x = 10, y = 20.000000, z = 3
x = 10, y = 20.000000, z = 100

You might also like