Items in this page:
|
| ||||||||||||
-----------------Output--------------- 100.0 d 20.0 d 60.0 w 200.0 w Your account now is -140.00 dollars.
|
| ||||||||||||
-----------------Output---------------- 7.80 5.70 5.80 8.95 0.0 Lowest men’s: 5.80 Highest women’s: 8.95
|
-------------------- | ||||||||||||
#include<stdio.h>
int main(void) { int i; for(i = 1; 5 - i; i = i + 1) printf("%d\n", i); printf("You are coming to the end of the loop\n"); return 0; }
The loop runs only 4 times and then stops. What is really happened here in the loop is that 5 – i is not a condition at all, it is arithmetic expression. How is that interpreted? Whenever an arithmetic expression evaluates to zero, that expression is considered to have a false value. Conversely, whenever an expression is non-zero, it is considered to be true. Hence, for the first four values of i,5 – i has a non-zero or a true value. When i becomes 5, then 5 – i becomes zero, the expression becomes false and the loop stops. Writing condition like this takes fewer instruction cycles from CPU/processor and the code runs faster than the if conditional operators were used. |
| ||||||||||||
| |||||||||||||
The Conditional Operator (?:), ternary
| |||||||||||||
| |||||||||||||
#include<stdio.h>
int main(void) { // initialize with dummy value... char x = 'n'; printf("Enter 'y' or non-'y': \n"); scanf_s(" %c", &x); // the following codes can be converted using ?: operator if(x == 'y') printf("If 'y', answer is = 1\n"); else printf("Else, answer is = 0\n"); return 0; } |
| ||||||||||||
#include<stdio.h>
int main(void) { // initialize with dummy value... char x ='n'; printf("Enter 'y' or non-'y': \n"); scanf_s(" %c", &x); // using ?: operator (x == 'y') ? printf("If 'y', answer is = 1\n") : printf("Else, answer is = 0\n"); return 0; } | A sample input and output are given below.
| ||||||||||||
From the program example, if x =='y' is true, the followingprintf() will be executed.
printf("If 'y', answer is = 1\n")
Otherwise the second printf() will be executed.
printf("Else, answer is = 0\n") | |||||||||||||
The switch-case-break Statement
| |||||||||||||
#include<stdio.h>
int main(void) { char code = 'd'; float balance = 0.0, amount = 0.0; printf("Enter your transaction code, d - deposit, w - withdrawal: \n"); scanf_s(" %c", &code); // the following code can be converted to using // switch-case-break statement... if(code =='d') { printf("Your deposit...\n"); balance = balance + amount; } else if(code == 'w') { printf("Your withdrawal...\n"); balance = balance - amount; } else printf(" %c code not allowed\n", code); return 0; } | #include<stdio.h>
int main(void) { char code = 'd'; float balance = 0.0, amount = 0.0; printf("Enter your transaction code, d - deposit, w - withdrawal: \n"); scanf_s(" %c", &code); // switch-case-break statement... switch(code) { case'd': { printf("Your deposit...\n"); balance = balance + amount; break; } case'w': { printf("Your withdrawal...\n"); balance = balance - amount; break; } default: printf("%c code not allowed. Try again!\n", code); } return 0; } | ||||||||||||
| |||||||||||||
![]() | |||||||||||||
More Activities
| |||||||||||||
Let us first consider the condition for the case where type is equal to “residential” only. If the type is residential and unit of consumption are less than or equal to 200, then the rate is 0.8, according to the Table. If the units are not less than or equal to200, then the rate is made equal to 0.7. Complete the coding for this part of the logic.
if(u <= 200) rate = _________________; else rate = _________________;
| if(u <= 200) rate = 0.8; else rate = 0.7; | ||||||||||||