C Programming Tutorial - 29 - break

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

LOL i put max to 1 million and wrote loop
It took 88 seconds

atsura
Автор

Thank you much, these tutorials are extremely helpful.
Question: what would I do if I absolutely wanted the user to type a number under 10? Could I include another IF statement under FOR loop specifying that:

if (howmany > 10)
{
printf ("Invalid entry. Please enter a value less than 10.\n");
}

Is this correct?

ZenMasterT
Автор

You are far better than any one BUCKY...
STUPID PROGARAMME 👍👍👍👍

neerajmahapatra
Автор

So we can Also use Letters instead of Number
i had Made one and it Works

#include <stdio.h>
#include <stdlib.h>

int main()
{
char a;
char howMany;
char maxAmount = 'z';

printf("How many times you wanted this loop to loop? ");
scanf(" %c", &howMany);

for (a='a';a<=maxAmount;a++){
printf("%c\n", a);

if(a==howmany){
break;
}
}

return 0;
}

rohit_veer
Автор

is it really necassary to have a variable as maxamount=10? cant you just have the for loop be:
for (a=1 ; a=10 ; a++){

vestraya
Автор

can i also do this?

int main ()
{

int a;
int z;

printf("your number: ");
scanf("%d", &z);

for (a=1;a<=z;a++)
{
printf("%d", a);
}


return 0;
}

xaveriuskevin
Автор

so if i want an error check, maybe like if they put something greater than 10, can i make an if statement saying
if (howmany>10) {printf("please input number between 0 and 1");}

TheJasonBoi
Автор

so you can also do
for(a = 1;a <= howMany ; a++){

printf("Loop: %d", a);
if(a == maxAmount){
break;
}
now im just confusing myself.

thestalks
Автор

bro i wanted to create a program which calculates points from every questio.like the question have options: a, b, c, d and each option have different points, and at the end of the process, it totals out and prints the outcome according to diffrernt points....i am really confused on this....can you give me some tips??

lizanpradhanang
Автор

same program without break
#include <stdio.h>
#include <conio.h>'
#include <ctype.h>
#include <string.h>
#include <math.h>
int main()
{
int times;
int a;
printf("how many times do you want to loop:");
scanf("%d", &times);
for(a=1;a<=times; a++){
printf("%d\n", a);
}

return 0;
}

borntechy
Автор

Why can't I initialize a variable inside of the forloop like > for(int i = 0; i < 5; i++) {} .... It was possible in CS50 ide so I am quite confused atm, does it depend on the ide or what?

michalpitr
Автор

how come you don't need an ampersand right before the a? he did it for howmany, but not a and I don't get why not.

qwerty
Автор

Good one but still this doesn't show how many you can loop, its basically loop until the number

hence
Автор

Why not just write howMany in for instead of maxAmount?

alyskeia
Автор

what will happen if you use a break in a nested for loop... will it kill both loops or just the first?

kzarmair
Автор

For some reason, my code doesn't work. I'm pretty sure it's correct: 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()
{
    int a;
    int howMany;
    int maxAmount = 10;

    printf("How many rows do you want to print? (up to 10) ");
    scanf(" %d", &howMany);

    for(a=1; a<=howMany; a++){
        printf("%d\n", a);
        if(a==maxAmount){
            break;
        }
    }

    return 0;
}

anthonyzhou
Автор

3:48
Why didn't you just set "Maxamount" to whatever the user was entering, then you wouldn't need a break...

tchgszdok
Автор

can we use **break** inside **IF** statement?

pranjalidwivedi
Автор

int a;
int howmany;
int max;

printf("HOW MANY TIMES LOOPS ?\n");
scanf( "%d", &howmany);

printf("What is maximum number ?\n");
scanf( "%d", &max);

for (a = 1; a <= max; a++){
printf( "%d\n", a);

if (a==max){
break;
}
}

mehmetsahin
Автор

Why don't we just use this way ? : 
int main()
{
  int a, i;
  printf("How many loops?: ");
  scanf("%d", &a);
  for(i=0;i<=a;i++){
    printf("%d", i);
  }
}

vasiletonofa