Press "Enter" to skip to content

C – Else-If , Continue , Break Expressions

In this essay , I will talk about the else-if , continue and break expressions. These expressions helps us to write easily code.

Else-if help us the add another condition to if – else. Like i will make program which is about the letter grade. If your note bigger or equal to 90 and smaller than 100 , your letter grade AA. In here , using with the else if i will add another condition. If your note bigger or equal to 80 and smaller than 90, your letter grade is BB. If your note bigger or equal to 70 and smaller than 80, your letter grade CC . If your note bigger or equal to 60 and smaller than 70 , your letter grade is DD . If your note smaller than 60, your letter grade is FF. I think if you look code syntax , you will understand better.

#include<stdio.h>
int main()
{
int mynote;
printf("**Welcome To The Letter Grade**\n");
printf("Enter the your note:");
scanf("%d",&mynote);
if(mynote >=90 && mynote<=100){
printf("Your letter grade is AA");
}
else if (mynote >=80 && mynote<90){
printf("Your letter grade is BB");
}
else if (mynote >=70 && mynote<80){
printf("Your letter grade is CC");
}
else if (mynote>=60 && mynote<70){
printf("Your letter grade is DD");
}
else if(mynote>=0 && mynote <60){
printf("Your letter grade is FF");
}
else{
printf("You entered wrong number");
}	
return 0;
}

Screen Output;

**Welcome To The Letter Grade**
Enter the your note:85
Your letter grade is BB

Now , I will make programs about the continue and break phrases. Continue means dont read the after the continue phrases. When the program see continue phrase , jump the code syntax . Break means is finish the program . I mean , When the program see break phrase , program will be finish there. When you see the code syntax , you will understand better also you should write these code yourself.

First , I will make program about break . Program will take the number from the user and program will add 10 to the user’s number . When the user wanna finish program , user will press the -1. Break help us to finish program when we press -1.

#include<stdio.h>
int main()
{
int number;
while(1){
printf("Enter the number (Press -1 to the exit program):");
scanf("%d",&number);
if(number==-1){
break;
}
else
{
number =number+10;
printf("Your new number is :%d \n\n",number);
}
}		
return 0;
}

Screen Output;

Enter the number (Press -1 to the exit program):43
Your new number is :53

Enter the number (Press -1 to the exit program):156
Your new number is :166

Enter the number (Press -1 to the exit program):1
Your new number is :11

Enter the number (Press -1 to the exit program):-1

Finally , I will make a program about the continue. Program will take numbers from user  and will collect each other.If the sum divide by 3 without remaining , program won’t collect each other. I will do this using with the continue.

#include<stdio.h>
int main()
{
int number1 , number2 ,sum=0;
while(1){
printf("Enter the first number:");
scanf("%d",&number1);
printf("Enter the second number:");
scanf("%d",&number2);
sum=number1+number2;
// % remaining 
if(sum%3==0){
continue;
}
else{
printf("Sum of numbers you entered = %d\n\n",sum);
}
}
return 0;
}

 

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *