C What if User Types Something Other Then Yes to Run Again
In this C programming language tutorial we take a look at the "if statement" and "switch statement". Both are used to alter the flow of a program if a specified test condition is true.
Boolean Operators
Before we can take a look at test conditions we have to know what Boolean operators are. They are called Boolean operators because they give you either true or false when you use them to test a condition. The greater than sign ">"
for instance is a Boolean operator. In the table below you see all the Boolean operators:
| == | Equal |
| ! | Not |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
| && | And |
| || | Or |
The if statement
The if statement can be used to test conditions so that we can alter the flow of a program. In other words: if a specific statement is true, execute this instruction. If not true, execute this instruction. So lets take a look at an example:
#include<stdio.h> int main() { int mynumber; scanf("%d",&mynumber); if ( mynumber == 10 ) printf("Is equal\n"); return 0; } In the example above the user can input a number. The number is stored in the variable mynumber.
Now take a look at the "if statement": if the number stored in the variable mynumber is equal to ten, then print "is equal" on the screen. Simple, isn't it. If the number is not equal to ten, then nothing gets printed.
Now take another look at the "if statement": look at the placement of the semi-colon. As you can see there is no semi-colon behind the "if statement". If there is just one instruction (if the statement is true), you can place it after the
"if statement" (with an indentation). Are multiple instructions necessary then you will have to use curly brackets, like so:
if ( mynumber == 10) { printf("is equal\n"); printf("closing program\n"); } Now we like to also print something if the "if statement" is not equal. We could do this by adding another "if statement" but there is an easier / better way. Which is using the so called "else statement" with the "if statement".
#include<stdio.h> int main() { int mynumber; scanf("%d",&mynumber); if ( mynumber == 10 ) { printf("Is equal\n"); printf("Closing program\n"); } else { printf("Not equal\n"); printf("Closing program\n"); } return 0; } Note: Take a look at the placement of the curly brackets and how the indentations are placed.
This is all done to make reading easier and to make less mistakes in large programs.
Nested if statements
If you use an "if statement" in an "if statement" it is called nesting. Nesting "if statements" can make a program very complex, but sometimes there is no other way. So use it wisely. Take a look at a nested "if statement" example below:
#include<stdio.h> int main() { int grade; scanf("%d",&grade); if ( grade <= 10 ) { printf("YOU DID NOT STUDY.\n"); printf("YOU FAILED ! \n"); } else { if ( grade < 60 ) { printf("You failed \n"); printf("Study harder\n"); } else { if ( grade >= 60 ) printf("YOU PASSED ! \n"); } } return 0; } Note: Again take a look at the placing of the curly brackets and the placing of the indentations.
So let's walk through the example above: If the grade is equal or below ten, you did not study. If the grade is above ten, the program will go into the "else statement". In the "else statement" there is another "if statement" (this is nesting).
This "if statement" checks the grade again. If the grade is below sixty, you must study harder. In the "else statement" from this "if statement" there is another "if statement". It checks whether the grade is above or equal to sixty. If it is, you passed the test.
Multiple condition testing
It is possible to test two or more conditions at once in an "if statement" with the use of the AND (&&) operator. Example:
if ( a > 10 && b > 20 && c < 10 ) If a is greater then ten and b is greater then twenty and c is smaller then ten, do something. So all three conditions must be true, before something happens.
With the OR ( || ) operator you can test if one of two conditions are true. Example:
If a equals ten or b is smaller then twenty then do something. So if a or b is true, something happens.
The switch statement
The switch statement is almost the same as an "if statement". The switch statement can have many conditions. You start the switch statement with a condition. If one of the variable equals the condition, the instructions are executed. It is also possible to add a default. If none of the variable equals the condition the default will be executed. See the
example below:
#include<stdio.h> int main() { char myinput; printf("Which option will you choose:\n"); printf("a) Program 1 \n"); printf("b) Program 2 \n"); scanf("%c", &myinput); switch (myinput) { case 'a': printf("Run program 1\n"); break; case 'b': { printf("Run program 2\n"); printf("Please Wait\n"); break; } default: printf("Invalid choice\n"); break; } return 0; } Note: break is used to exit the switch. See the next tutorial for more details.
That's all for this tutorial.
We encourage you to make your own programs or to make changes in the example programs. This is the only way you can learn programming C. Make little programs, the more the better. Have fun!
This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.
Tweet This! or use to share this post with others.
tedeschifittold00.blogspot.com
Source: https://www.codingunit.com/c-tutorial-the-if-and-switch-statement
0 Response to "C What if User Types Something Other Then Yes to Run Again"
Post a Comment