+ 1
[CLOSED] Is it just me, or are if/else statements and while loops the 'best'?
I've been learning about various statements and loops recently. I've learned the if/else and switch statement, along with the while, do... while, and for loop. If/else statements and while loops seem to be most effective in the ways I use them, but I feel stupid because I can't figure out why the other statements and loops are useful... 😓
2 Respostas
+ 3
They are also used for the same purpose but have different feature.
For example for loop:
A while loop has initialization at the beginning then condition and steps at the last. But in for loop you have all three things at one place.
For loops are mostly used with arrays instead of while loop.
Switch statement is used when you have many statements to test but don't have much conditions so you can simply use expressions( say 1,2,3, etc.) to get task done.
Hope this will help,
😀
+ 1
switch(c) {
case 'a': case 'o': case 'e': case 'i':
case 'u': vowel = 1; break;
default: vowel = 0;}
for (int i = 2; i <= n; i++) {
if (isprime(i)) {
printf("%d\n", i);
}
}
do {
r = doaniteration();
} while(r != FINISHED);