#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 # ?
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
i didnt understand this in css50 so i came here thanks
Man I wish you were my professor.
Can you do multiple whiles? Like 4 conditions?
just use && between conditions
@@ntsd7509 or || if you want "or"
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.
I dont get it again.... goddamnit
great
Isn't condition (if(number >0)) redundant?
yes, but it's easier for begginers to understand
😍😍😍😍😍😍😍
whatever anything before or after or I do or did
How did we get 15 at the end?
Oh yea yea i got it xd
1+2+3+4+5 = 15 sum value
what is printf?
input
'printf' is to output to the terminal window...basically its like displaying something to the user
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
ОД ДУШИ БРАТ
❤🩹
خ
ğ
@@SpicaSuzuki what?
@@lalmi6150 My goals are beyond your understanding.
And üİşçöı
@@SpicaSuzuki you are cool girl, i liked your type
@@lalmi6150 thanks :)