Could someone help me debug this program, only the else part is processed on every input. This id a program for grading students. a student inputs a mark and the the grade is displayed
func main(){
var x int
fmt.Println("Enter your marks")
fmt.Scanf("%d",&x)
if (100 <= x) && (x<=75){
fmt.Println("D1")
}else if (74 <= x)&&(x <= 70){
fmt.Println("D2")
}else if (69 <= x )&&(x<=65){
fmt.Println("C3")
}else if (64 <= x)&&(x <= 60){
fmt.Println("C4")
}else if (59 <= x)&&(x <= 55){
fmt.Println("C5")
}else if (54 <= x)&&( x<= 50){
fmt.Println("C6")
}else if (49 <= x )&&(x<= 45){
fmt.Println("P7")
}else{
fmt.Println("Work harder")
}
}
You have a logic problem.
Change
if (100 <= x) && (x<=75){
to
if 75 <= x && x <= 100 { // numbers here are ordered from smallest to greatest
because a number can't be greater than 100 and smaller than 75.
And it's the same for the other lines of course.
Note that you could make less comparisons. Suppose you test if the number is smaller than 100 initially, then you don't have to test if it's smaller than 75 just after you tested it's smaller than 75.
A typical Go code would probably have a switch
here instead of all those if/else
. See switch in the documentation. Here's how it could be written with a switch
:
switch {
case x > 100:
fmt.Println("Congrats!") // you forgot this one
case x >= 75:
fmt.Println("D1")
case x >= 70:
fmt.Println("D2")
case x >= 65:
fmt.Println("C3")
case x >= 60:
fmt.Println("C4")
case x >= 55:
fmt.Println("C5")
case x >= 50:
fmt.Println("C6")
case x >= 45:
fmt.Println("P7")
default:
fmt.Println("Work harder")
}
A last comment : This type of switching code rarely occurs because normally the thresholds and related notes are stored as data, for example in a struct
.
Your IF statement says:
if 100 is less than x (which means x has to be greater than 100)
AND
x less than (or equal to) 75
do this--
x will never be greater than 100 AND less than 75, so it always does the ELSE ...
Thankx finaly got with your help. hope it helps someone else some time in future
package main
import "fmt"
func main(){
var x int
fmt.Println("Enter your marks")
fmt.Scanf("%d",&x)
if (75<= x) && (x<=100){
fmt.Println("D1")
}else if (70 <= x)&&(x <= 74){
fmt.Println("D2")
}else if (65 <= x )&&(x<=69){
fmt.Println("C3")
}else if (60 <= x)&&(x <= 64){
fmt.Println("C4")
}else if (55 <= x)&&(x <= 59){
fmt.Println("C5")
}else if (50 <= x)&&( x<= 54){
fmt.Println("C6")
}else if (45 <= x )&&(x<= 49){
fmt.Println("P7")
}else{
fmt.Println("Work harder")
}
}
all if and if else statements provided in this code are logically incorrect
example:
if (100 <= x) && (x<=75)
here if (100 <= x) is false, the compiler will not even consider the next condition. This is called SHORT CIRCUIT, the lazy evaluation of condition statements.
this is also applicable to OR conditions i.e in case the first condition is true the second condition will not be evaluated.
So according to the code you have written, the ideal solution will be
func main() {
var x int
fmt.Println("Enter your marks")
fmt.Scanf("%d", &x)
if (100 >= x) && (x >= 75) {
fmt.Println("D1")
} else if (74 >= x) && (x >= 70) {
fmt.Println("D2")
} else if (69 >= x) && (x >= 65) {
fmt.Println("C3")
} else if (64 >= x) && (x >= 60) {
fmt.Println("C4")
} else if (59 >= x) && (x >= 55) {
fmt.Println("C5")
} else if (54 >= x) && (x >= 50) {
fmt.Println("C6")
} else if (49 >= x) && (x >= 45) {
fmt.Println("P7")
} else {
fmt.Println("Work harder")
}