In this program we’ll show you the usage of else-if clause in C programming language. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.
First in if‘s condition we check if the user entered percentage is above 100%. If true – in that case we let the user know that he entered wrong marks and he / she needs to re-enter the marks. Next in first else if we check if the percentage is greater than or equal to 60. Here we need not check if the percentage is less than 100, because in if clause itself we’ve checked if percentage is greater than 100, and we are checking the else if condition only because the condition in if is false. So percentage will be less than 100. Same logic goes to consecutive Grades.
Student Grade Calculation using else if clause: C Program
In this program we’ll show you nesting of if else statements. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.
Simple Logic
When the condition in if statement is false then only else block gets executed. In our program, we check, if percentage is greater than or equal to 60. If it is false, then only control shifts to else block. So inside else(nested if else) we need not once again check if the percentage is less than 60.
Note: We are not using else if and logical operator in this C program purposefully. We want to show nesting of if else in this program.
After going through the basics of Jade, lets learn about loops and conditional statements in Jade.
Note: Since most text editors designed for web remove the indentation(and some retain only new line characters), thus compressing the file and optimizing it for the web use. But this doesn’t serve any good for writing Jade. Because indentation is at the heart of Jade syntax!
So in this tutorial, we are using a Chrome extension called Jade Editor to illustrate loops and conditional statements. You can simply test your code in this Google Chrome extension and then copy over the code to your actual file. This would even help reduce the complicated errors in lengthy files.
each loop in Jade arrays
1
2
3
4
5
- names = ["Apple", "Microsoft", "Oracle"];
ul
each name in names
li #{name}
- names = ["Apple", "Microsoft", "Oracle"];
ul
each name in names li #{name}
output
Apple
Microsoft
Oracle
– sign is optional for each loop. Here it loops through the array names and prints out each element of the array.
each loop and loop index in Jade arrays
1
2
3
4
5
- names = ["Apple", "Microsoft", "Oracle"];
ul
each name, i in names
li #{i+1}: #{name}
- names = ["Apple", "Microsoft", "Oracle"];
ul
each name, i in names li #{i+1}: #{name}
output
1: Apple
2: Microsoft
3: Oracle
This prints out name along with the index number. Since index in JavaScript array starts with 0, we add 1 to it, to make it more sensible for non-programmers.
for loop and loop index in Jade arrays
1
2
3
4
5
- names = ["Apple", "Microsoft", "Oracle"];
ul
for name, i in names
li #{i+1}: #{name}
- names = ["Apple", "Microsoft", "Oracle"];
ul
for name, i in names li #{i+1}: #{name}
output
1: Apple
2: Microsoft
3: Oracle
Same holds good even for for loop.
each loop in Jade objects: Key, Value pair
1
2
3
4
5
- obj = {cmp1: "Apple", cmp2: "IBM"};
ul
each val, key in obj
li #{key}: #{val}
- obj = {cmp1: "Apple", cmp2: "IBM"};
ul
each val, key in obj li #{key}: #{val}
output
cmp1: Apple
cmp2: IBM
Since objects in JavaScript has {key: value} pair, we make use of it to extract and print from the object.
for loop in Jade objects: Key, Value pair
1
2
3
4
5
- obj = {cmp1: "Apple", cmp2: "IBM"};
ul
for val, key in obj
li #{key}: #{val}
- obj = {cmp1: "Apple", cmp2: "IBM"};
ul
for val, key in obj li #{key}: #{val}
output
cmp1: Apple
cmp2: IBM
Same holds good to for loop too.
Conditional Statements
if, else if, else In Jade Conditional Statements
1
2
3
4
5
6
7
8
- var fc = 2;
if( fc === 0 )
p You have no followers
else if( fc === 1 )
p You have 1 follower
else
p You have #{fc} followers
- var fc = 2;
if( fc === 0 ) p You have no followers
else if( fc === 1 ) p You have 1 follower
else p You have #{fc} followers
output You have 2 followers
Here we use the condition to check for execution of block of code. Also note that – sign before if, else if and else keywords are optional.
#include<iostream .h>
#include<conio .h>
void main()
{
int a, b, c;
clrscr();
cout< <"Enter 3 no's\n";
cin>>a>>b>>c;
int big = ( a>b && a>c )?a:(b>c?b:c);
/*
if( a > b && a > c )
big = a;
else if( b > c )
big = b;
else
big = c;
*/cout< <"\nAmong "<<a<<" , "<<b<<" , "<<c<<" Biggest is "<<big;
getch();
}
#include<iostream .h>
#include<conio .h>
void main()
{
int a, b, c;
clrscr();
cout< <"Enter 3 no's\n";
cin>>a>>b>>c;
int big = ( a>b && a>c )?a:(b>c?b:c);
/*
if( a > b && a > c ) big = a;
else if( b > c ) big = b;
else big = c;
*/cout< <"\nAmong "<<a<<" , "<<b<<" , "<<c<<" Biggest is "<<big;
getch();
}
In this program we take 3 integer values from the user and using ternary operator decide the biggest of 3 numbers.
int big = ( a>b && a>c )?a:(b>c?b:c);
int big = ( a>b && a>c )?a:(b>c?b:c);
if ( a > b && a > c ) is true, then value of a will be stored in variable big; else ( b > c ? b : c ) will be evaluated. Here, if value of b is greater than c, value of b will be stored in variable big else value of c will be stored in the variable big.