Here's the code I made with this: #include #include #include #include #include int main(){ int answers[] = {12,23,43,22,66,45,65,777,4985,2,487,59}; for(int i = 0; i < sizeof(answers)/sizeof(answers[0]); i++){ printf("The answer for question #%d is %d ", i + 1,answers[i]); } }
Hi, whats the difference between your for loop and this? for(int i = 0; i < prices[ i ]; i++) { printf("%.2lf ", prices[ i ]); } it still prints all the values in an array.
So here's how your for loop works: Here is the array : prices[ ] = { 50, 60 , 50 , 30, 60 }; After the first iteration, the value of *price[ i ]* is *greater than *i* , so the condition is true and the loop continues, however if you print the value of price[ 5 ] , you get zero, because it has no value assigned to it in the initial array. So what happens? The loop exits(condition becomes false), since now the *i* variable is *greater* than *prices[ ]*. Now that we understand this , we can see why it's different from Bro code's code: sample Array 2 : prices[ ] = { 50, 60 , 50 , 30, 60, -67 }; The code will not print -67, because the loop will exit since -67 is less than 4, which is the value of *i* when it iterates for the fourth time before reaching -67. So your code will never print 0 or negative numbers. if the first element was negative the loop would exit before starting since the i < prices[ i ] condition would be false. TLDR; The loop would exit when an element in negative or zero, since i < prices[ i ] would be false. Also from chatGPT( Use it , use whatever resource you have for learning please) : It's worth noting that the behavior of this loop depends on the values stored in the prices array. If the values in the prices array are not arranged in a way that makes the loop condition false at some point, the loop may continue indefinitely, leading to an infinite loop. Therefore, it's important to ensure that the loop condition is appropriate for the specific task.
"i" is just a variable name, you can call it whatever you want. It pretty much reads as: "do this loop while variable called 'i' is still less than the condition (in this case the condition is the amount of stuff inside the prices) - and while doing so, keep adding 1 to 'i's value after every time." The 'for loop' will keep on going until "i" is no longer less than the amount inside the prices, it starts at 0 and then increases by 1 everytime because of 'i++'.
#include
int main()
{
double prices[] = {5.0, 10.0, 15.0, 25.0, 20.0, 30.0};
//printf("%d bytes
", sizeof(prices));
for(int i = 0; i < sizeof(prices)/sizeof(prices[0]); i++)
{
printf("$%.2lf
", prices[i]);
}
return 0;
}
thanks you are the best :)
Thanks for tutorial. Please up more C tutorial. I already learn Java from your video but C make it more mindful.
You’re the best,Thank you 🤍
Here's the code I made with this:
#include
#include
#include
#include
#include
int main(){
int answers[] = {12,23,43,22,66,45,65,777,4985,2,487,59};
for(int i = 0; i < sizeof(answers)/sizeof(answers[0]); i++){
printf("The answer for question #%d is %d
", i + 1,answers[i]);
}
}
Hi,
whats the difference between your for loop and this?
for(int i = 0; i < prices[ i ]; i++)
{
printf("%.2lf
", prices[ i ]);
}
it still prints all the values in an array.
So here's how your for loop works:
Here is the array : prices[ ] = { 50, 60 , 50 , 30, 60 };
After the first iteration, the value of *price[ i ]* is *greater than *i* , so the condition is true and the loop continues, however if you print the value of price[ 5 ] , you get zero, because it has no value assigned to it in the initial array. So what happens? The loop exits(condition becomes false), since now the *i* variable is *greater* than *prices[ ]*.
Now that we understand this , we can see why it's different from Bro code's code:
sample Array 2 : prices[ ] = { 50, 60 , 50 , 30, 60, -67 };
The code will not print -67, because the loop will exit since -67 is less than 4, which is the value of *i* when it iterates for the fourth time before reaching -67. So your code will never print 0 or negative numbers. if the first element was negative the loop would exit before starting since the i < prices[ i ] condition would be false.
TLDR; The loop would exit when an element in negative or zero, since i < prices[ i ] would be false.
Also from chatGPT( Use it , use whatever resource you have for learning please) :
It's worth noting that the behavior of this loop depends on the values stored in the prices array. If the values in the prices array are not arranged in a way that makes the loop condition false at some point, the loop may continue indefinitely, leading to an infinite loop. Therefore, it's important to ensure that the loop condition is appropriate for the specific task.
This is what i looking for . Thank you .
Very useful! Thanks Bro!
Why would we need to divide the overall Array Size by the size of the first array element? Significance in that?
I am 4 months late. I hope you found your answer by now but if you didn't, it's to calculate the length of the array.
great teaching.
You are the Ultimate Bro!. thank you for your videos
Thanks bro
Thanks bro!!!
😊😊😊
thx
Why didn't you use len of array instead of sizeof?
Because size matters
@@Baka-mx4yc 💀💀💀
@@Baka-mx4yc
I'm saying it, I miss JavaScript
JS doesn't miss you
❤❤DONE❤❤
You didn't explain why you used i. I don't understand. I'm trying it, it's not working.
indicator of the index
"i" is just a variable name, you can call it whatever you want.
It pretty much reads as: "do this loop while variable called 'i' is still less than the condition (in this case the condition is the amount of stuff inside the prices) - and while doing so, keep adding 1 to 'i's value after every time."
The 'for loop' will keep on going until "i" is no longer less than the amount inside the prices, it starts at 0 and then increases by 1 everytime because of 'i++'.