‘if’ statement exercise solution
Exercise
- Write a program that takes the user’s age and decides whether a user can cast a vote or not.
- The minimum age for casting a vote is 18 years
- Print appropriate messages
Here I have a project. The project name is ifstatementCastVote. And here, I have included some header files, and there is a main.
Let me create a variable, int age = 0. This variable is used to hold the age. Let me ask the user to enter their age. So, printf(“Enter your age here :”). After that, I have to use scanf. So, scanf(“%d”, &age);
Now let’s use the if statement to compare the age with a minimum age, that is 18. So, let me use the if statement. if (age < 18) and after that hit enter. When you hit enter, you can see that the cursor is automatically indented here, as shown in Figure 1.
Remember that, in ‘C’ programming, indentation is not compulsory. So, indentation is given to make your code look beautiful, or a properly indented code is more readable, and it looks beautiful. So, that’s the reason we use indentation. So, unlike python, in ‘C’ programming, indentation is not compulsory.
In eclipse, when you press enter, the code is automatically indented. On some IDEs, this is not true; the code will not be automatically indented. In that case, I would prefer to give a tab. You can indent the code by giving a tab.
Here, let me print the message. printf(“Sorry! you are not eligible to vote\n”). Here you can see that this statement is inside the curly braces. So, some people prefer this. That is, even if there is only one statement to execute, they usually keep that inside the curly braces; you can even do this no problem.
#include<stdio.h> #include<stdint.h> int main(void) { int age = 0; printf("Enter your age here :"); scanf("%d",&age); if(age < 18) { printf("Sorry ! you are not eligible to vote\n"); }
And some programmers usually give this opening bracket here, as shown in Figure 2. So, this also looks good. I would suggest you to follow this method. This makes a code look more compact. So, this is also correct.
And again, let’s use another if condition,if(age >= 18), then you can print printf(“Congrats! you are eligible to vote\n”);
#include<stdio.h> #include<stdint.h> int main(void) { int age = 0; printf("Enter your age here :"); scanf("%d",&age); if(age < 18){ printf("Sorry ! you are not eligible to vote\n"); } if(age >= 18){ printf("Congrats ! you are eligible to vote\n"); } printf("Press enter key to exit this application"); while(getchar() != '\n') { //just read the input buffer and do nothing } getchar(); }
This is a simple code.
Now let’s try to execute this code on the host machine. You need not use target here, so execute on the host machine.
First, let’s compile this code and launch this application. The output is shown in Figure 3. Enter your age, let me type 9, and it prints, “Sorry! you are not eligible to vote”.
Let’s execute once again. Enter your age, let me type 67. It prints, “Congrats! you are eligible to vote”.
Now, let’s analyze the execution flow.
First if(age<18) expression is evaluated. I enter age is 9. So, 9 is lesser than 18, which is true. That’s why “Sorry! you are not eligible to vote” is printed.
And after that, the execution control goes to the next if statement if(age>=18), 9 is greater than or equal to 18, which is false. That’s why the “Congrats! you are eligible to vote” statement is not executed. So, like that.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1