#include int main() { // while loop = checks a condition, THEN executes a block of code if condition is true // do while loop = always executes a block of code once, THEN checks a condition int number = 0; int sum = 0; do{ printf("Enter a # above 0: "); scanf("%d", &number); if(number > 0) { sum += number; } }while(number > 0);
in my head i was thinking as long as the while was true the code executed but if false it didn't, not that if false it didnt REPEAT. aka it executes once regardless. Thanks again
im prolly abit late but when he entered the values (1, 2, 3, 4, 5) it all added up together because of the 'sum', basically each value got added which added up to 15 1+2+3+4+5 = 15
#include
int main()
{
// while loop = checks a condition, THEN executes a block of code if condition is true
// do while loop = always executes a block of code once, THEN checks a condition
int number = 0;
int sum = 0;
do{
printf("Enter a # above 0: ");
scanf("%d", &number);
if(number > 0)
{
sum += number;
}
}while(number > 0);
printf("sum: %d", sum);
return 0;
}
Why number is wrote as # ?
@@KaraSub2.0 idk if you're still looking for an answer but i guess it's a short form
You just gained a new sub... You're the best
i was trying to research how this worked for an hour and a half now until this video, thank you
in my head i was thinking as long as the while was true the code executed but if false it didn't, not that if false it didnt REPEAT. aka it executes once regardless. Thanks again
Thank you! ❤
i didnt understand this in css50 so i came here thanks
Can you do multiple whiles? Like 4 conditions?
just use && between conditions
@@ntsd7509 or || if you want "or"
Thx bro, came to my rescue once again.
Man I wish you were my professor.
Why do we need the if? You already said while i greater 0 so you could just put the equation anyways?
Because a negative number that would force exit the loop would affect sum's value, so you only want to sum the positive entries.
great
I dont get it again.... goddamnit
whatever anything before or after or I do or did
Tenkyu😈😈😈😈😈
Isn't condition (if(number >0)) redundant?
yes, but it's easier for begginers to understand
😍😍😍😍😍😍😍
why is the sum 15 at the end , can someone explain me pls..
im prolly abit late but when he entered the values (1, 2, 3, 4, 5) it all added up together because of the 'sum', basically each value got added which added up to 15
1+2+3+4+5 = 15
@@danny0.o Can you explain sum +=number;
@@SuryaSurya-sc1riit is basically same with sum=sum + number (it is just shorter form)
I love you
what is printf?
input
'printf' is to output to the terminal window...basically its like displaying something to the user
How did we get 15 at the end?
Oh yea yea i got it xd
1+2+3+4+5 = 15 sum value
ОД ДУШИ БРАТ
❤🩹
خ
ğ
@@SpicaSuzuki what?
@@lalmi6150 My goals are beyond your understanding.
And üİşçöı
@@SpicaSuzuki you are cool girl, i liked your type
@@lalmi6150 thanks :)