# Help me with my If Else

Page 1 of 1

## 7 Replies - 2025 Views - Last Post: 17 January 2010 - 01:41 PM

### #1 k3nnt0ter0

Reputation: 0
• Posts: 181
• Joined: 16-January 10

# Help me with my If Else

Posted 16 January 2010 - 09:28 PM

We're asked to Create a program that will accept input from prelim grade, midterm grade and final grade and calculate it. The formula of the calculation for the semestral grade will be sg=((pg*.30)+(mg*.30)+(fg*.40)). If the input is <=65 and >100, a prompt will appear and say "INVALID ENTRY" and request to input again.

If the input is ='0', a prompt will appear and say "NO GRADE", If the input is ='20', a prompt will appear and say "Not Attending",If the input is ='30', a prompt will appear and say "Dropped", If the input is ='40', a prompt will appear and say "Incomplete",It will show what you've input and continue to input midterm grade. Same with the midterm and finals.

I got here my program for Prelim Grade but I got problem with the <=65 and >100. If I input 65, it will prompt and say INVALID ENTRY.After I input an Invalid Entry again, nothing will happen. After I input another invalid entry, a prompt will appear but after I input a valid entry, the valid entry won't show. It must be not stopping to prompt INVALID ENTRY and Request to input again until you Input a valid entry. After you input a valid entry, it will be shown on the browser.

we're asked to use do..while and while

```<html>
<title>Web Programming Laboratory Exam</title>
<body>
<script language="Javascript">

do
{
{

if(pg==0)
{
}
else if(pg==20)
{
}
else if(pg==30)
{
}
else if(pg==40)
{
}

else if (pg<=65)
{
}
else if (pg>100)
{
}
else
ppg=pg*.30
}

}

while (pg<=65)
{

}

</script>
</body>
</html>

```

I'm asking how my if..else on <=65 and >100 won't stop to ask me to input a valid entry and after I input a valid entry, the valid entry must be shown and continue to Midterm Grade.

Is This A Good Question/Topic? 0

## Replies To: Help me with my If Else

### #2 no2pencil

• Professor Snuggly Pants

Reputation: 5892
• Posts: 28,862
• Joined: 10-May 07

## Re: Help me with my If Else

Posted 16 January 2010 - 09:30 PM

What is the purpose of nested brackets under the do loop?

Quote

```do
{
{

```

One pet peve that I have is if/else if/else if/ else if when the programmer is checking the same value each time.

Since this is an int value, why not run a select case on it?

or just check with multiple ifs? Why drag it out when your 1st check has failed or passed?

```	if(pg==0)
{
}
if(pg==20)
{
}
if(pg==30)
// ... and so on

```

```switch(pg)
{
case 0:
break;
case 20:
break;
case 30:
break
case 40:
break
default:
break;
}
```

& so on...

### #3 k3nnt0ter0

Reputation: 0
• Posts: 181
• Joined: 16-January 10

## Re: Help me with my If Else

Posted 16 January 2010 - 10:21 PM

no2pencil, on 16 Jan, 2010 - 08:30 PM, said:

What is the purpose of nested brackets under the do loop?

Quote

```do
{
{

```

One pet peve that I have is if/else if/else if/ else if when the programmer is checking the same value each time.

Since this is an int value, why not run a select case on it?

or just check with multiple ifs? Why drag it out when your 1st check has failed or passed?

```	if(pg==0)
{
}
if(pg==20)
{
}
if(pg==30)
// ... and so on

```

```switch(pg)
{
case 0:
break;
case 20:
break;
case 30:
break
case 40:
break
default:
break;
}
```

& so on...

I tried your code but everything I input is "Error" even I input 0,20,30 or 40. I put your code inside the do...while loop.

I changed this line
```default:
break;
```

to this

```default:
break;
```

So it won't prompt ERROR again.
This is my new program

```<html>
<title>Web Programming Laboratory Exam</title>
<body>
<script language="Javascript">

do
{
switch(pg)
{
case 0:
break;
case 20:
break;
case 30:
break;
case 40:
break;
default:
break;
}
if (pg<=65)
{
}
else if (pg>100)
{
}

}
while (pg<=65)
{

}

</script>
</body>
</html>
```

The problem here now is when I put an Invalid Entry or 0,20,30, or 40, the Prompt will say Invalid Entry. How can I run this properly? If I input 0,20,30 or 40, it must not prompt Invalid Entry. Another problem here is if I input an invalid Entry, the entry that I entered will be shown. it must not. If I input again an invalid entry, nothing will happen, it will just ask me to input again. If I input a valid entry, none is shown. I'm getting confused. I think switch statement is not com applicable for this program

This post has been edited by k3nnt0ter0: 16 January 2010 - 10:23 PM

### #4 no2pencil

• Professor Snuggly Pants

Reputation: 5892
• Posts: 28,862
• Joined: 10-May 07

## Re: Help me with my If Else

Posted 16 January 2010 - 10:34 PM

The following works for me :

```<html>
<title>Web Programming Laboratory Exam</title>
<script language="Javascript">
function pg_func() {
var pg;
switch(pg) {
case "0":
break;
case "20":
break;
case "30":
break;
case "40":
break;
default:
break;
}
return pg;
}
</script>
<body>
<script language="Javascript">
var pg_test;
pg_test=pg_func();
</script>
</body>
</html>
```

### #5 k3nnt0ter0

Reputation: 0
• Posts: 181
• Joined: 16-January 10

## Re: Help me with my If Else

Posted 16 January 2010 - 10:45 PM

no2pencil, on 16 Jan, 2010 - 09:34 PM, said:

The following works for me :

```<html>
<title>Web Programming Laboratory Exam</title>
<script language="Javascript">
function pg_func() {
var pg;
switch(pg) {
case "0":
break;
case "20":
break;
case "30":
break;
case "40":
break;
default:
break;
}
return pg;
}
</script>
<body>
<script language="Javascript">
var pg_test;
pg_test=pg_func();
</script>
</body>
</html>
```

It works on me too but we're getting far with the instruction to use do..while and while.
Let's go back to the problem on my first post.

Quote

I'm asking how my if..else on <=65 and >100 won't stop to ask me to input a valid entry and after I input a valid entry, the valid entry must be shown and continue to Midterm Grade.

This post has been edited by k3nnt0ter0: 16 January 2010 - 10:46 PM

### #6 no2pencil

• Professor Snuggly Pants

Reputation: 5892
• Posts: 28,862
• Joined: 10-May 07

## Re: Help me with my If Else

Posted 16 January 2010 - 11:07 PM

Using the switch case, I would set a variable & adjust it accordingly.

If I understand you correctly, you want the prompt to continue to ask if the value is less than 65 or greater than 100, but only ask once & report if the value is inbetween?

### #7 k3nnt0ter0

Reputation: 0
• Posts: 181
• Joined: 16-January 10

## Re: Help me with my If Else

Posted 16 January 2010 - 11:53 PM

Ok. This is my code from prelim to finals. The missing part here is when the input is less than or equal to 65 and greater than 100, the prompt will say INVALID ENTRY. However, after it says Invalid Entry, it must ask you to enter again an Entry until you input a valid entry. It must be terminated after you input either 0,20,30,40 or after you input a valid entry and after the program calculated it. This program does not terminate after I input valid entries. It just continue to ask grade again and again. I don't know how will I put those code inside the program. I'm getting really confused.
```<html>
<title>Web Programming Laboratory Exam</title>
<body>
<script language="Javascript">
do
{
{
if(pg==0)
{
}
else if(pg==20)
{
}
else if(pg==30)
{
}
else if(pg==40)
{
}
else
}
{
if(mg==0)
{
}
else if(mg==20)
{
}
else if(mg==30)
{
}
else if(mg==40)
{
}
else
}
{
if(fg==0)
{
}
else if(fg==20)
{
}
else if(fg==30)
{
}
else if(fg==40)
{
}
else
{
sg=((pg*.3)+(mg*.3)+(fg*.4));
}
}
if (sg>=74.5)
{
document.write('<br>Remarks: Passed');
document.write('</br>This Program is developed by -k3nN');
}
else
if (sg<74.5)
{
document.write('<br>Remarks:Failed');
document.write('</br>This Program is developed by -k3nN');
}
}
while (sg>=74.5|sg<74.5)
{
document.write('</br>Thanks')
}
</script>
</body>
</html>
```

This post has been edited by k3nnt0ter0: 17 January 2010 - 01:55 AM

### #8 psyking

Reputation: 20
• Posts: 165
• Joined: 17-January 10

## Re: Help me with my If Else

Posted 17 January 2010 - 01:41 PM

You could try using this code:
```

<html>
<title>Web Programming Laboratory Exam</title>
<script type="text/javascript">
var valid = 0;
var pg , mg , fg;
//This is the function that determines if the preliminary grade is valid:
//A while statement to continue requesting a grade until a valid grade is input
do
{
/*This checks if the grade is less than or equal to 65 or greater than 100,
it also checks to see if it is equal to 0, 20, 30, or 40 when it is less than 65*/
if (pg <= 65 && pg != 0 && pg != 20 && pg != 30 && pg != 40 || pg > 100)
{
}else{
if (pg <= 100 && pg > 65) { valid = 1; }
else{
switch(pg){
case "0":
valid = 1;
break;
case "20":
valid = 1;
break;
case "30":
valid = 1;
break;
case "40":
valid = 1;
break;
default:
break;
}
}
}
}
while (valid == 0);
return pg;
}
//This is the function that determines if the midterm grade is valid:
do
{
/*This checks if the grade is less than or equal to 65 or greater than 100,
it also checks to see if it is equal to 0, 20, 30, or 40 when it is less than 65*/
if (mg <= 65 && mg != 0 && mg != 20 && mg != 30 && mg != 40 || mg > 100)
{
}else{
if (mg <= 100 && mg > 65) { valid = 2; }
else{
switch(mg){
case "0":
valid = 2;
break;
case "20":
valid = 2;
break;
case "30":
valid = 2;
break;
case "40":
valid = 2;
break;
default:
break;
}
}
}
}
while (valid == 1);
return mg;
}
//This is the function that determines if the final grade is valid:
do
{
/*This checks if the grade is less than or equal to 65 or greater than 100,
it also checks to see if it is equal to 0, 20, 30, or 40 when it is less than 65*/
if (fg <= 65 && fg != 0 && fg != 20 && fg != 30 && fg != 40 || fg > 100)
{
}else{
if (fg <= 100 && fg > 65) { valid = 3; }
else{
switch(fg){
case "0":
valid = 3;
break;
case "20":
valid = 3;
break;
case "30":
valid = 3;
break;
case "40":
valid = 3;
break;
default:
break;
}
}
}
}
while (valid == 2);
return fg;
}
</script>
<body>
<script>
var pre , mid , fin , sem;
sem = ((pre*.3) + (mid*.3) + (fin*.4));
//This writes the grades to the webpage:
document.write("<br>Prelim Grade is " + pre + "<br>Midterm Grade is " + mid + "<br>Final Grade is " +

fin + "<br>Semester Grade is " + sem);
</script>
</body>
</html>

```
It does exactly what you are describing and uses do...while statements.

This post has been edited by psyking: 17 January 2010 - 02:14 PM